Attribute Macro extruct_from

Source
#[extruct_from]
Expand description

An attribute macro implements the std::convert::From trait for an annotated non-generic named struct using a “superstruct” specified as the attribute parameter as a source type and the ExtructedFrom marker trait that indicates that struct fields populated during conversion from superstruct all have same names in both structs.

extruct_from accepts a single parameter which is a non-generic type name referring to a named struct.

struct SuperStruct {
    one_field: String,
    another_field: u32,
    and_one_more: char,
}

#[extruct_from(SuperStruct)]
struct SubStruct {
    and_one_more: String,
}

fn convert_preserving_names<T, S>(sup: S) -> T
where
    T: ExtructedFrom<S>
{
    sup.into()
}

let sup = SuperStruct {
    one_field: "str".to_owned(),
    another_field: 1135,
    and_one_more: 'x',
};

let sub: SubStruct = convert_preserving_names(sup);