Skip to main content

Context

Struct Context 

Source
pub struct Context<E: Event> { /* private fields */ }
Expand description

Runtime-provided context for an actor to interact with the system.

Use it to:

  • send(event): emit events into the broker tagged with this actor’s name
  • stop(): request graceful shutdown of this actor (and trigger global cancel)
  • name(): retrieve the actor’s name for logging/identity
  • is_alive(): check whether the actor loop should continue running

Correlation:

  • send_with_correlation(event, id): emit an event linked to a specific correlation id.
  • send_child_event(event, meta): convenience to set correlation id to the parent meta.id().

See also: Envelope, Meta, crate::Supervisor.

Implementations§

Source§

impl<E: Event> Context<E>

Source

pub fn new( actor_id: ActorId, sender: Sender<Arc<Envelope<E>>>, alive: Arc<AtomicBool>, ) -> Self

Source

pub async fn send(&self, event: E) -> Result<()>

Send an event to the broker. The envelope will carry this actor’s name. This awaits channel capacity (backpressure) to avoid silent drops.

Source

pub async fn send_with_correlation( &self, event: E, correlation_id: EventId, ) -> Result<()>

Send an event with an explicit correlation id.

Source

pub async fn send_child_event(&self, event: E, meta: &Meta) -> Result<()>

Emit a child event correlated to the given parent Meta.

Source

pub async fn send_envelope(&self, envelope: Envelope<E>) -> Result<()>

Source

pub fn stop(&mut self)

Signal this actor to stop

Source

pub fn actor_id(&self) -> &ActorId

Source

pub fn actor_name(&self) -> &str

The actor’s name as registered with the supervisor.

Source

pub fn is_alive(&self) -> bool

Whether the actor is considered alive by the runtime.

Source

pub async fn pending(&self) -> Result<()>

Returns a future that never completes.

Note: This method is largely obsolete. The default step() implementation now returns StepAction::Never, which disables stepping entirely. You only need pending() if you want to block inside a custom step() implementation.

// Prefer this (default behavior):
async fn step(&mut self) -> Result<StepAction> {
    Ok(StepAction::Never)
}

// Or simply don't implement step() at all - it defaults to Never

Trait Implementations§

Source§

impl<E: Clone + Event> Clone for Context<E>

Source§

fn clone(&self) -> Context<E>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl<E> Freeze for Context<E>

§

impl<E> RefUnwindSafe for Context<E>

§

impl<E> Send for Context<E>

§

impl<E> Sync for Context<E>

§

impl<E> Unpin for Context<E>

§

impl<E> UnwindSafe for Context<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.