[][src]Macro structural::make_struct

macro_rules! make_struct {
    (
        $( #![$inner_attrs:meta] )*
        $(
            $( #[$field_attrs:meta] )*
            $field_name:ident $( : $field_value:expr )?
        ),*
        $(,)?
    ) => { ... };
}

Constructs an anonymous struct, which implements all the accessor traits for its fields.

Syntax

Here is the entire syntax for this macro:


make_struct!{
    // This is an inner attribute,which is applied to the struct declaration.
    // In this case it's deriving `Debug` for the struct.
    #![derive(Debug)]
     
    // This is an attribute on the first field.
    #[doc="you must put attributes for the first field after the inner attributes"]
    size_cm:0,
    place:"Anctartica",
    foo, // This initializes a `foo` field with the `foo` variable.
}

Example

use structural::{StructuralExt,make_struct,structural_alias,fp};

use std::fmt::Debug;

structural_alias!{
    pub trait Runner{
        name:String,
        stamina:u32,
    }
}

// Everything below could be on a separate crate (ignoring imports)


fn get_runner(this:&(impl Runner+Debug) ){
    assert_eq!( this.field_(fp!(name)).as_str(), "hello","{:?}",this);
    assert_eq!( this.field_(fp!(stamina)), &100,"{:?}",this);
}

get_runner(&make_struct!{
    #![derive(Debug)]
    name:"hello".into(),
    stamina:100,
});


fn ret_get_runner(name:String)->impl Runner+Clone{
    make_struct!{
        #![derive(Clone)]
        name,
        stamina:4_000_000_000,
    }
}

{
    let runner=ret_get_runner("hello".into());

    assert_eq!( runner.field_(fp!(name)).as_str(), "hello" );
    assert_eq!( runner.field_(fp!(stamina)), &4_000_000_000 );

    let (name,stamina) = runner.into_fields(fp!( name, stamina ));
    assert_eq!( name, "hello" );
    assert_eq!( stamina, 4_000_000_000 );
}


fn get_dyn_runner()->Box<dyn Runner>{
    Box::new(make_struct!{
       name:"hello".into(),
       stamina:4_000_000_000,
    })
}

{
    let runner=get_dyn_runner();

    assert_eq!( runner.field_(fp!(name)).as_str(), "hello" );
    assert_eq!( runner.field_(fp!(stamina)), &4_000_000_000 );

    assert_eq!(
        runner.into_fields(fp!( name, stamina )),
        ("hello".to_string(), 4_000_000_000),
    );
}