Skip to main content

behavior

Attribute Macro behavior 

Source
#[behavior]
Expand description

Generate Behavior wiring for an inherent impl with exact &mut self methods. Invalid receivers are rejected at compile time.

use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};

struct Invalid;
#[behavior::behavior(
    addr = MailAddr,
    message = u8,
    sends = Vec<Delivery<MailAddr, Never>>,
    births = NoBirths,
    error = Never,
)]
impl Invalid {
    fn init(&self) -> behavior::Acted<MailAddr, Never, Vec<Delivery<MailAddr, Never>>, NoBirths, Never> {
        Ok(Actions::cont())
    }
    fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Delivery<MailAddr, Never>>, NoBirths, Never> {
        Ok(Actions::cont())
    }
}

Missing transition methods are rejected by the macro itself:

use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
struct Missing;
#[behavior::behavior(
    addr = MailAddr,
    message = u8,
    sends = Vec<Delivery<MailAddr, Never>>,
    births = NoBirths,
    error = Never,
)]
impl Missing {
    fn init(&mut self) -> behavior::Acted<MailAddr, Never, Vec<Delivery<MailAddr, Never>>, NoBirths, Never> {
        Ok(Actions::cont())
    }
}

Async behavior methods cannot introduce an erased or alternate execution path:

use behavior::{Actions, Delivery, MailAddr, Never, NoBirths};
struct Async;
#[behavior::behavior(
    addr = MailAddr,
    message = u8,
    sends = Vec<Delivery<MailAddr, Never>>,
    births = NoBirths,
    error = Never,
)]
impl Async {
    async fn init(&mut self) -> behavior::Acted<MailAddr, Never, Vec<Delivery<MailAddr, Never>>, NoBirths, Never> {
        Ok(Actions::cont())
    }
    fn receive(&mut self, _: MailAddr, _: u8) -> behavior::Acted<MailAddr, Never, Vec<Delivery<MailAddr, Never>>, NoBirths, Never> {
        Ok(Actions::cont())
    }
}

Generate the mechanical Behavior implementation for a normal inherent impl containing init(&mut self) and receive(&mut self, from, message). The original impl and methods are preserved unchanged.