Thank you for the awesome crate!
Here is a feature suggestion, that is sorely missed in our projects. It has also been requested twice already (#43 and #62).
#[new(into)]
To make type conversion easier, #[new(into)] attribute changes the parameter type to impl Into<T>, and populates the field with value.into(). This is useful for containers like strings, accepting a String or a &str:
#[derive(new)]
struct Foo {
#[new(into)]
x: String,
}
let _ = Foo::new("Hello".to_string());
let _ = Foo::new(format!("Hello"));
let _ = Foo::new("Hello");
It is also useful for wrapping types like Box<T> and Rc<T>, without having to wrap at every callsite:
#[derive(new)]
struct Foo {
#[new(into)]
x: Rc<Bar>,
}
let _ = Foo::new(Rc::new(bar));
let _ = Foo::new(bar.into());
let _ = Foo::new(bar);
#[new(into_iter = "T")]
For iterators/collections, #[new(into_iter = "T")] attribute changes the parameter type to impl IntoIterator<Item = T>, and populates the field with value.into_iter().collect(). This allows initializing with a variety of types, like [T], Option<T>, Vec<T>, sets, maps, and even other iterators:
#[derive(new)]
struct Foo {
#[new(into_iter = "bool")]
x: Vec<bool>,
}
let _ = Foo::new(vec![true, false]);
let _ = Foo::new([true, false]);
let _ = Foo::new(Some(true));
About this PR
I split this into three parts to make reviewing easier:
Thank you for the awesome crate! Here is a feature suggestion, that is sorely missed in our projects. It has also been requested twice already (#43 and #62).
#[new(into)]
To make type conversion easier,
#[new(into)]
attribute changes the parameter type toimpl Into<T>
, and populates the field withvalue.into()
. This is useful for containers like strings, accepting aString
or a&str
:It is also useful for wrapping types like
Box<T>
andRc<T>
, without having to wrap at every callsite:#[new(into_iter = "T")]
For iterators/collections,
#[new(into_iter = "T")]
attribute changes the parameter type toimpl IntoIterator<Item = T>
, and populates the field withvalue.into_iter().collect()
. This allows initializing with a variety of types, like[T]
,Option<T>
,Vec<T>
, sets, maps, and even other iterators:About this PR
I split this into three parts to make reviewing easier:
cargo fmt
).README.md
andlib.rs
.Please let me know if I can do anything else.