Derive Macro derive_constructors::From

source ·
#[derive(From)]
{
    // Attributes available to this derive:
    #[no_from]
}
Expand description

On structs it allows to Derive the From trait where a tuple of the fields are passed to the [From::from], for example:

#[derive(derive_constructors_proc::From, PartialEq, Debug)]
struct CharacterInfo{
    name: String,
    age: u8,
    #[no_from]
    times_appeared: u8,
    #[no_from(4)]
    years_studied: u8
}

let character_using_from = CharacterInfo::from(("Jorge".to_string(), 23));
let expected_character = CharacterInfo { name: "Jorge".to_string(), age: 23, times_appeared: 0, years_studied: 4};
assert_eq!(character_using_from, expected_character);



On enums it implement the From trait by creating a From::from function for each variant taking it’s values as parameters, for example:

#[derive(derive_constructors_proc::From, Debug, PartialEq)]
enum MyValue{
    StaticString(&'static str),
    Number(i32),
    Boolean(bool),
}

let scattered_values = vec![MyValue::from("Age "), MyValue::from(23), MyValue::from(", over age "), MyValue::from(true)];
let specified = vec![MyValue::StaticString("Age "), MyValue::Number(23), MyValue::StaticString(", over age "), MyValue::Boolean(true)];
assert_eq!(scattered_values, specified);