Skip to main content

AsyncActor

Trait AsyncActor 

Source
pub trait AsyncActor {
    type Message: Send + 'static;
    type Error: Display;

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

    // Required method
    async fn handle(
        &mut self,
        context: &BareContext<Self::Message>,
        message: Self::Message,
    ) -> Result<(), Self::Error>;

    // Provided methods
    fn name() -> &'static str { ... }
    fn priority(_message: &Self::Message) -> Priority { ... }
    async fn started(
        &mut self,
        _context: &BareContext<Self::Message>,
    ) -> Result<(), Self::Error> { ... }
    async fn stopped(
        &mut self,
        _context: &BareContext<Self::Message>,
    ) -> Result<(), Self::Error> { ... }
    fn addr() -> Addr<Self::Message> { ... }
    fn addr_with_capacity(capacity: impl Into<Capacity>) -> Addr<Self::Message> { ... }
}
Expand description

The actor trait - async variant.

Provided Associated Constants§

Source

const DEFAULT_CAPACITY_NORMAL: usize = 5

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

Source

const DEFAULT_CAPACITY_HIGH: usize = 5

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

Required Associated Types§

Source

type Message: Send + 'static

The expected type of a message to be received.

Source

type Error: Display

The type to return on error in the handle method.

Required Methods§

Source

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

The primary function of this trait, allowing an actor to handle incoming messages of a certain type. Note that the actor system still calls this serially for each message even for async actors; not even control messages (currently the request to stop the actor) are processed while the future returned by handle() is still pending.

Delegate work to an async task if you want to process messages and control events concurrently. This is recommended especially if the handle() can take extended/arbitrary time to fully resolve and you want the actor to stay responsive.

Provided Methods§

Source

fn name() -> &'static str

The name of the Actor. Used only for logging/debugging. Default implementation uses type_name().

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

async fn started( &mut self, _context: &BareContext<Self::Message>, ) -> Result<(), Self::Error>

An optional callback when the Actor has been started.

Source

async fn stopped( &mut self, _context: &BareContext<Self::Message>, ) -> Result<(), Self::Error>

An optional callback when the Actor has been stopped.

Source

fn addr() -> Addr<Self::Message>

Create address for this actor with default capacities.

Source

fn addr_with_capacity(capacity: impl Into<Capacity>) -> Addr<Self::Message>

Create address for this actor, specifying its inbox size. Accepts Capacity or usize.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§