#constructor #configurable #derive #macro #new #foo-bar #comments

fancy_constructor

为您的结构体派生高度可配置的构造函数

8 个稳定版本

1.3.0 2024 年 8 月 12 日
1.2.2 2023 年 10 月 28 日
1.1.0 2023 年 9 月 28 日

2507开发工具

Download history 2344/week @ 2024-04-22 2313/week @ 2024-04-29 2226/week @ 2024-05-06 2349/week @ 2024-05-13 2309/week @ 2024-05-20 2245/week @ 2024-05-27 2362/week @ 2024-06-03 2395/week @ 2024-06-10 2658/week @ 2024-06-17 2443/week @ 2024-06-24 2843/week @ 2024-07-01 2744/week @ 2024-07-08 2952/week @ 2024-07-15 3240/week @ 2024-07-22 3094/week @ 2024-07-29 2513/week @ 2024-08-05

12,023 每月下载量
用于 17 个软件包 (通过 indexed_db_futures)

Apache-2.0

26KB
427 代码行

为您的结构体派生高度可配置的构造函数

MASTER CI status crates.io badge docs.rs badge dependencies badge Coverage Status

示例

基本
use fancy_constructor::new;
#[derive(new, PartialEq, Eq, Debug)]
struct MyStruct {
  foo: String,
  bar: u8,
}

let a = MyStruct::new("#[derive(new)]".into(), 55);
let b = MyStruct { foo: "#[derive(new)]".into(), bar: 55 };
assert_eq!(a, b);

输出

impl MyStruct {
  pub fn new(foo: String, bar: u8) -> Self {
    Self { foo, bar }
  }
}
选项展示
#[derive(new, PartialEq, Eq, Debug)]
#[new(vis(pub(crate)), name(construct), comment("Foo"), bounds(T: Clone))]
struct MyStruct<T> {
  #[new(into)]
  a: T,

  #[new(val("Bar".into()))]
  b: String,

  #[new(clone)]
  c: Arc<Whatever>,

  #[new(default)]
  d: Vec<u8>,
}

let we = Arc::new(Whatever::default());
let a = MyStruct::<String>::construct("A", &we);
let b = MyStruct {a: "A".into(), b: "Bar".into(), c: we, d: vec![]};
assert_eq!(a, b);

输出

impl<T> MyStruct<T> {
  /// Foo
  pub(crate) fn construct(a: impl Into<T>, c: &Arc<Whatever>) -> Self where T: Clone {
    Self {
      a: a.into(),
      b: "Bar".into(),
      c: c.clone(),
      d: Default::default(),
    }
  }
}
私有 const 函数
#[derive(new, PartialEq, Eq, Debug)]
#[new(const_fn, vis())]
struct Foo(u8);

const FOO: Foo = Foo::new(128);
assert_eq!(FOO, Foo(128));

输出

impl Foo {
  const fn new(f1: u8) -> Self {
    Self(f1)
  }
}
计算值
#[derive(new)]
struct Foo {
  is_bar: bool,
  #[new(val(if is_bar { 100 } else { 5 }))]
  barness_level: u8,
}

assert_eq!(Foo::new(true).barness_level, 100);
assert_eq!(Foo::new(false).barness_level, 5);
自定义构造函数参数
#[derive(new)]
#[new(args(input_string: &str))]
struct Foo {
  #[new(val(input_string.to_lowercase()))]
  pub lowercase: String,

  #[new(val(input_string.to_uppercase()))]
  pub uppercase: String,
}

let foo = Foo::new("Foo");
assert_eq!(foo.lowercase.as_str(), "foo");
assert_eq!(foo.uppercase.as_str(), "FOO");
重命名构造函数参数
#[derive(new)]
struct MyNewtype(#[new(name(my_value))] u8);

输出

impl MyNewtype {
  pub fn new(my_value: u8) -> Self {
    Self(my_value)
  }
}
枚举
#[derive(new, Eq, PartialEq, Debug)]
enum MyEnum {
  #[new]
  Foo { #[new(into)] bar: u8 },
  Qux,
}

assert_eq!(MyEnum::new(5), MyEnum::Foo { bar: 5 });

输出

impl MyEnum {
  pub fn new(bar: Into<u8>) -> Self {
    Self::Foo { bar: bar.into() }
  }
}
无效输入
#[derive(fancy_constructor::new)]
enum Foo {
  Bar, // no variants marked with `#[new]`
}
#[derive(fancy_constructor::new)]
enum Foo {
  #[new] Bar, // multiple variants marked with `#[new]`
  #[new] Qux,
}
#[derive(fancy_constructor::new)]
union Foo { // Unions not supported
  bar: u8,
  qux: u8,
}

依赖

~305–750KB
~18K SLoC