Skip to content

Allow #[from] or #[from(ignore)] in regular structs #149

@Xion

Description

@Xion
#[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,

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions