Attribute Macro init

Source
#[init]
Expand description

This macro takes an fn as the basis of an InitialState implementation for the state type that is returned by the fn (on success).

The fn is expected to build the state type specified as a Path attribute argument from a Vec<u8>, i.e. the signature should be compatible with TryFrom<Vec<u8>> for the state type given as argument to the macro.

Example:

pub struct A0 {
  data: u8,
}

#[hax_lib_protocol_macros::init(A0)]
fn init_a(prologue: Vec<u8>) -> ::hax_lib_protocol::ProtocolResult<A0> {
    if prologue.len() < 1 {
       return Err(::hax_lib_protocol::ProtocolError::InvalidPrologue);
    }
    Ok(A0 { data: prologue[0] })
}

// The following is generated by the macro:
#[hax_lib::exclude]
impl TryFrom<Vec<u8>> for A0 {
    type Error = ::hax_lib_protocol::ProtocolError;
    fn try_from(value: Vec<u8>) -> Result<Self, Self::Error> {
        init_a(value)
    }
}
#[hax_lib::exclude]
impl InitialState for A0 {
    fn init(prologue: Option<Vec<u8>>) -> ::hax_lib_protocol::ProtocolResult<Self> {
        if let Some(prologue) = prologue {
            prologue.try_into()
        } else {
            Err(::hax_lib_protocol::ProtocolError::InvalidPrologue)
        }
    }
}