#shortcuts #generate #generic #constructor #derive #foo #type

generic-new

一个衍生宏,为某些类型生成具有快捷键的易用构造函数

2个不稳定版本

0.2.0 2022年4月7日
0.1.0 2022年3月31日

#21 in #shortcuts

Download history 39/week @ 2024-03-31 2/week @ 2024-04-07

每月 283 次下载
you-must-conform 中使用

MIT 许可证

24KB
540

generic-new

crates-io docs-rs github

一个衍生宏,为某些类型生成具有快捷键的易用构造函数。

use generic_new::GenericNew;

#[derive(GenericNew)]
struct Foo {
    s: String,      // -> impl AsRef<str>
    v: Vec<usize>,  // -> impl IntoIterator<Item = usize>
    i: Vec<String>, // -> impl IntoIterator<Item = impl AsRef<str>>
    p: PathBuf,     // -> impl AsRef<Path>
    #[generic_new(ignore)]
    o: String,      // Turn off magic conversion for some fields
    #[generic_new(ty = impl Into<usize>, converter = |u|Into::into(u))]
    u: usize,       // Custom converters are supported
}

Foo::new(
    "hello",
    [1, 2, 3],
    ["a", "b", "c"],
    "path/to/foo",
    String::from("world"),
    1u16,
);

许可证: MIT

依赖项

~4–13MB
~140K SLoC