Trait tonari_actor::Actor
source · pub trait Actor {
type Message: Send + 'static;
type Error: Display;
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,
) -> Result<(), Self::Error> { ... }
fn stopped(
&mut self,
_context: &mut Self::Context,
) -> Result<(), Self::Error> { ... }
fn deadline_passed(
&mut self,
_context: &mut Self::Context,
_deadline: Instant,
) -> Result<(), Self::Error> { ... }
}
Expand description
The base actor trait.
Required Associated Types§
sourcetype Context
type Context
What kind of context this actor accepts. Usually Context<Self::Message>
.
Provided Associated Constants§
sourceconst DEFAULT_CAPACITY_NORMAL: usize = 5usize
const DEFAULT_CAPACITY_NORMAL: usize = 5usize
Default capacity of actor’s normal-priority inbox unless overridden by .with_capacity()
.
sourceconst DEFAULT_CAPACITY_HIGH: usize = 5usize
const DEFAULT_CAPACITY_HIGH: usize = 5usize
Default capacity of actor’s high-priority inbox unless overridden by .with_capacity()
.
Required Methods§
Provided Methods§
sourcefn priority(_message: &Self::Message) -> Priority
fn priority(_message: &Self::Message) -> Priority
Determine priority of a message
before it is sent to this actor.
Default implementation returns Priority::Normal
.
sourcefn started(&mut self, _context: &mut Self::Context) -> Result<(), Self::Error>
fn started(&mut self, _context: &mut Self::Context) -> Result<(), Self::Error>
An optional callback when the Actor has been started.
sourcefn stopped(&mut self, _context: &mut Self::Context) -> Result<(), Self::Error>
fn stopped(&mut self, _context: &mut Self::Context) -> Result<(), Self::Error>
An optional callback when the Actor has been stopped.
sourcefn deadline_passed(
&mut self,
_context: &mut Self::Context,
_deadline: Instant,
) -> Result<(), Self::Error>
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<(), String> {
// 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(())
}
}