Derive Macro former::exposed::ComponentFrom

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

Macro to implement From for each component (field) of a structure. This macro simplifies the creation of From trait implementations for struct fields, enabling easy conversion from a struct reference to its field types.

§Features

  • Requires the derive_component_from feature to be enabled for use.
  • The ComponentFrom derive macro can be applied to structs to automatically generate From implementations for each field.

§Attributes

  • debug : Optional attribute to enable debug-level output during the macro expansion process.

§Examples

Assuming the derive_component_from feature is enabled in your Cargo.toml, you can use the macro as follows :

#[ derive( former::ComponentFrom ) ]
struct Person
{
  pub age : i32,
  pub name : String,
}

let my_struct = Person { age : 10, name : "Hello".into() };
let age : i32 = From::from( &my_struct );
let name : String = From::from( &my_struct );
dbg!( age );
dbg!( name );
// > age = 10
// > name = "Hello"