Crate rtactor_macros

Crate rtactor_macros 

Source
Expand description

Proc macros for the rtactor library.

§Generate a Response enum from a Request enum with derive(ResponseEnum)

#[derive(ResponseEnum)]
pub enum Request {
    SetValue{val: i32},
    
    #[response_val(i32)]
    GetValue{},
}

Will generate:

pub enum Response
{
    SetValue(),
    GetValue(i32)
}

§Generate a synchronous access trait from a Notification enum with SyncNotifier

#[derive(SyncNotifier)]
pub enum Notification {
    TemperatureChanged{temp: float}
}

Will generate:

pub trait SyncNotifier : ::rtactor::SyncAccessData
{
 temperature_changed(&mut self, temp: float) -> Result<(), ::rtactor::Error> {[...]}
}

A structure can add the generated methods by deriving SyncNotifier and implementing the methods of SyncAccessData. The macro define_sync_accessor!() found in create rtactor can be used to generate a struct that allows easy access with its internal ActiveMailbox:

define_sync_accessor!(MyNotifSyncAccessor, SyncNotifier)


fn test(addr: rtactor::Addr)
{
    let accessor = MyNotifSyncAccessor::new(&addr);
    accessor.temperature_changed(13.2f32).unwrap();
}

§Generate a synchronous access trait from a Request enum with SyncRequester

#[derive(ResponseEnum, SyncRequester)]
pub enum Request {
    SetValue{val: i32},
    
    #[response_val(i32)]
    GetValue{},
}

Will generate for the SyncRequester part:

pub trait SyncRequester: ::rtactor::SyncAccessor
{
    fn set_value(&mut self, val: i32) -> Result<(), ::rtactor::Error> {[...]}
    fn get_value(&mut self) -> Result<i32, ::rtactor::Error> {[...]}
}

A structure can add the generated methods by deriving SyncRequester and implementing the methods of SyncAccessor. The macro define_sync_notifier!() found in create rtactor can be used to generate a struct that allows easy access with its internal ActiveMailbox:

define_sync_accessor!(MySyncAccessor, SyncNotifier, SyncRequester)

fn test(addr: rtactor::Addr)
{
    let accessor = MyNotifSyncAccessor::new(&addr);
    accessor.temperature_changed(13.2f32).unwrap();
    accessor.set_value(72).unwrap();
    assert!(accessor.get_value().unwrap() == 72);
}

Derive Macros§

AsyncRequester
ResponseEnum
SyncNotifier
SyncRequester