Trait tonari_actor::Actor

source ·
pub trait Actor {
    type Message: Send + 'static;
    type Error: Debug;
    type Context;

    const DEFAULT_CAPACITY_NORMAL: usize = 5usize;
    const DEFAULT_CAPACITY_HIGH: usize = 5usize;

    // Required methods
    fn handle(
        &mut self,
        context: &mut Self::Context,
        message: Self::Message
    ) -> Result<(), Self::Error>;
    fn name() -> &'static str;

    // Provided methods
    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.

Required Associated Types§

source

type Message: Send + 'static

The expected type of a message to be received.

source

type Error: Debug

The type to return on error in the handle method.

source

type Context

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

Provided Associated Constants§

source

const DEFAULT_CAPACITY_NORMAL: usize = 5usize

Default capacity of actor’s normal-priority inbox unless overridden by .with_capacity().

source

const DEFAULT_CAPACITY_HIGH: usize = 5usize

Default capacity of actor’s high-priority inbox unless overridden by .with_capacity().

Required Methods§

source

fn handle( &mut self, context: &mut Self::Context, message: Self::Message ) -> Result<(), Self::Error>

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

source

fn name() -> &'static str

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

Provided Methods§

source

fn priority(_message: &Self::Message) -> Priority

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

source

fn started(&mut self, _context: &mut Self::Context)

An optional callback when the Actor has been started.

source

fn stopped(&mut self, _context: &mut Self::Context)

An optional callback when the Actor has been stopped.

source

fn deadline_passed( &mut self, _context: &mut Self::Context, _deadline: Instant ) -> Result<(), Self::Error>

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§

source§

impl<M: Send + 'static, A: Actor<Context = TimedContext<M>, Message = M>> Actor for Timed<A>

§

type Context = Context<<Timed<A> as Actor>::Message>

§

type Error = <A as Actor>::Error

§

type Message = TimedMessage<M>

source§

const DEFAULT_CAPACITY_HIGH: usize = A::DEFAULT_CAPACITY_HIGH

source§

const DEFAULT_CAPACITY_NORMAL: usize = A::DEFAULT_CAPACITY_NORMAL