Trait tonari_actor::Actor[][src]

pub trait Actor {
    type Message: Send + 'static;
    type Error: Debug;
    type Context;
    fn handle(
        &mut self,
        context: &mut Self::Context,
        message: Self::Message
    ) -> Result<(), Self::Error>;
fn name() -> &'static str; fn priority(_message: &Self::Message) -> Priority { ... }
fn started(&mut self, _context: &mut Self::Context) { ... }
fn stopped(&mut self, _context: &mut Self::Context) { ... }
fn deadline_passed(
        &mut self,
        _context: &mut Self::Context,
        _deadline: Instant
    ) -> Result<(), Self::Error> { ... } }
Expand description

The base actor trait.

Associated Types

The expected type of a message to be received.

The type to return on error in the handle method.

What kind of context this actor accepts. Usually Context<Self::Message>.

Required methods

The primary function of this trait, allowing an actor to handle incoming messages of a certain type.

The name of the Actor - used only for logging/debugging.

Provided methods

Determine priority of a message before it is sent to this actor. Default implementation returns Priority::Normal.

An optional callback when the Actor has been started.

An optional callback when the Actor has been stopped.

An optional callback when a deadline has passed.

The deadline has to be set via Context::set_deadline() or Context::set_timeout() first. The instant to which the deadline was originally set is passed via the deadline argument; it is normally close to Instant::now(), but can be later if the actor was busy.

Periodic tick example

impl Actor for TickingActor {
    // ...

    fn deadline_passed(&mut self, context: &mut Self::Context, deadline: Instant) -> Result<(), ()> {
        // do_periodic_housekeeping();

        // A: Schedule one second from now (even if delayed); drifting tick.
        context.set_timeout(Some(Duration::from_secs(1)));

        // B: Schedule one second from deadline; non-drifting tick.
        context.set_deadline(Some(deadline + Duration::from_secs(1)));

        // C: Schedule one second from deadline, but don't fire multiple times if delayed.
        context.set_deadline(Some(max(deadline + Duration::from_secs(1), Instant::now())));

        Ok(())
    }
}

Implementors