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

macro generic-new

A derive macro which generates an ergonomic constructor with shortcuts for certain types

2 unstable releases

0.2.0 Apr 7, 2022
0.1.0 Mar 31, 2022

#21 in #shortcuts

43 downloads per month
Used in you-must-conform

MIT license

24KB
540 lines

generic-new

crates-io docs-rs github

A derive macro which generates an ergonomic constructor with shortcuts for certain types.

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,
);

License: MIT

Dependencies

~4–5.5MB
~99K SLoC