derive_more
derive_more copied to clipboard
Allow #[from] or #[from(ignore)] in regular structs
#[derive(From)]
struct Example {
main_field: u32,
some_extra: bool,
}
Currently, the code above produces impl From<(u32, bool)>. There is no way, AFAIK, to make derive_more create an impl From<u32> instead, with some_extra being set to Default::default().
It would make sense to permit this by using either #[from] or #[from(ignore)]:
#[derive(From)]
struct Example {
#[from]
main_field: u32,
some_extra: bool,
}
#[derive(From)]
struct Example {
main_field: u32,
#[from(ignore)]
some_extra: bool,
}
For fields that were ignored this way, the From impl would use their default values when performing the conversion.
The requirement for those fields to implement Default doesn't seem particularly onerous but it could nevertheless be lifted as well, similarly to how serde does it:
fn true_() -> bool { true }
#[derive(From)]
struct Item {
data: String,
#[from(default = "true_")]
alive: bool,
I agree that both being able to ignore and being able to set defaults would be quite useful. PRs welcome.