2 unstable releases

new 0.5.0 Feb 3, 2025
0.1.0 Nov 17, 2024

#97 in #actor-framework

Download history 103/week @ 2024-11-17 5/week @ 2024-11-24 2/week @ 2024-12-01 1/week @ 2024-12-08 109/week @ 2025-02-02

109 downloads per month
Used in rtactor

MIT license

18KB
220 lines

RTactor framework macros

These are the macros that are used in the RTactor framework. Please see rtactor for details.


lib.rs:

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 ActiveActor:

define_sync_accessor!(MyNotifSyncAccessor, SyncNotifier)


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

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 ActiveActor:

define_sync_accessor!(MySyncAccessor, SyncNotifier, SyncRequester)

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

Dependencies

~1.5MB
~40K SLoC