Macro type_level_values::construct [−][src]
macro_rules! construct { ($name:ty) => { ... }; ( $name:ty => $( $field_name:ty = $field_ty:ty ),* $(,)* ) => { ... }; }
Declares a ConstValue type,giving every field a type.
This macro ensures that every field is initialized, otherwise producing a compile-time error which mentions the fields that weren't initialized.
Usage
Usage of this type macro takes this form:
construct!( <constructor> =>
$(
<field_accessor> = <field_value> ,
)*
)
<constructor> is a type implementing type_level_values::initialization::InitializationValues.
Valid constructors are:
- structs:
Type/ _Uninit. - variants:
_Uninit.
<field_accessor> is the field accessor in the type_level_<deriving_type>::fields
submodule.
<field_value> is the value being assigned to the field.
Example 1:Constructing a struct with only public fields.
#[derive(TypeLevel)] #[typelevel(reexport(Struct))] pub struct Rectangle{ pub x:u32, pub y:u32, pub w:u32, pub h:u32, } use self::type_level_Rectangle::fields; fn main(){ let _:construct!{RectangleType=> fields::x=U0, fields::y=U0, fields::w=U0, fields::h=U0, }; }
Example 2:Constructing a struct with private fields.
#[derive(TypeLevel)] #[typelevel(reexport(Variants))] pub enum Player{ Player0, Player1, } #[derive(TypeLevel)] #[typelevel( reexport(Struct), //print_derive, )] pub struct Game{ points:u32, winner:Option<Player>, } use self::type_level_Game::fields; fn main(){ let _:construct!{Game_Uninit=> fields::points = U10, fields::winner = None_, }; }
Example 3:Constructing enum variants.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, TypeLevel)] #[typelevel(items(IntoConstType(NoImpls)), reexport(Variants,Discriminants, Traits),)] // #[typelevel(skip_derive)] // #[typelevel(print_derive)] pub enum Operation { Transfer { type_: () }, Loop { repetitions: u32, sequence: Vec<Operation>, }, Close, } use self::type_level_Operation::fields; fn main(){ let transfer:construct!{Transfer_Variant=> fields::type_ = u32, }=Transfer::MTVAL; let _:Transfer<u32>=transfer; let _:construct!{Loop_Variant=> fields::repetitions = U10, fields::sequence = tlist![ Transfer< () >, Transfer< Vec<u64> >, ], }; let _:construct!{ Close_Variant }; }