Struct actix::prelude::Context

source ·
pub struct Context<A>
where A: Actor<Context = Context<A>>,
{ /* private fields */ }
Expand description

An actor execution context.

Implementations§

source§

impl<A> Context<A>
where A: Actor<Context = Self>,

source

pub fn new() -> Self

Create a context without spawning it.

The context can be spawned into an actor using its run method.

struct Actor1 {
    actor2_addr: Addr<Actor2>,
}

struct Actor2 {
    actor1_addr: Addr<Actor1>,
}

let ctx1 = Context::<Actor1>::new();
let ctx2 = Context::<Actor2>::new();

let actor1 = Actor1 { actor2_addr: ctx2.address() };
let actor2 = Actor2 { actor1_addr: ctx1.address() };

ctx1.run(actor1);
ctx2.run(actor2);
source

pub fn with_receiver(rx: AddressReceiver<A>) -> Self

source

pub fn run(self, act: A) -> Addr<A>

source

pub fn into_future(self, act: A) -> ContextFut<A, Self>

source

pub fn handle(&self) -> SpawnHandle

Returns a handle to the running future.

This is the handle returned by the AsyncContext::spawn() method.

source

pub fn set_mailbox_capacity(&mut self, cap: usize)

Sets the mailbox capacity.

The default mailbox capacity is 16 messages. #Examples

struct MyActor;
impl Actor for MyActor {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Self::Context) {
        ctx.set_mailbox_capacity(1);
        System::current().stop();
    }
}

let addr = sys.block_on(async { MyActor.start() });
sys.run();
source

pub fn connected(&self) -> bool

Returns whether any addresses are still connected.

Trait Implementations§

source§

impl<A> ActorContext for Context<A>
where A: Actor<Context = Self>,

source§

fn stop(&mut self)

Immediately stop processing incoming messages and switch to a stopping state. This only affects actors that are currently running. Future attempts to queue messages will fail.
source§

fn terminate(&mut self)

Terminate actor execution unconditionally. This sets the actor into the stopped state. This causes future attempts to queue messages to fail.
source§

fn state(&self) -> ActorState

Retrieve the current Actor execution state.
source§

impl<A> AsyncContext<A> for Context<A>
where A: Actor<Context = Self>,

source§

fn spawn<F>(&mut self, fut: F) -> SpawnHandle
where F: ActorFuture<A, Output = ()> + 'static,

Spawns a future into the context. Read more
source§

fn wait<F>(&mut self, fut: F)
where F: ActorFuture<A, Output = ()> + 'static,

Spawns a future into the context, waiting for it to resolve. Read more
source§

fn waiting(&self) -> bool

Checks if the context is paused (waiting for future completion or stopping).
source§

fn cancel_future(&mut self, handle: SpawnHandle) -> bool

Cancels a spawned future. Read more
source§

fn address(&self) -> Addr<A>

Returns the address of the context.
source§

fn add_stream<S>(&mut self, fut: S) -> SpawnHandle
where S: Stream + 'static, A: StreamHandler<S::Item>,

Registers a stream with the context. Read more
source§

fn add_message_stream<S>(&mut self, fut: S)
where S: Stream + 'static, S::Item: Message, A: Handler<S::Item>,

Registers a stream with the context, ignoring errors. Read more
source§

fn notify<M>(&mut self, msg: M)
where A: Handler<M>, M: Message + 'static,

Sends the message msg to self. This bypasses the mailbox capacity, and will always queue the message. If the actor is in the stopped state, an error will be raised.
source§

fn notify_later<M>(&mut self, msg: M, after: Duration) -> SpawnHandle
where A: Handler<M>, M: Message + 'static,

Sends the message msg to self after a specified period of time. Read more
source§

fn run_later<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where F: FnOnce(&mut A, &mut A::Context) + 'static,

Executes a closure after a specified period of time. Read more
source§

fn run_interval<F>(&mut self, dur: Duration, f: F) -> SpawnHandle
where F: FnMut(&mut A, &mut A::Context) + 'static,

Spawns a job to execute the given closure periodically, at a specified fixed interval.
source§

fn run_interval_at<F>( &mut self, start: Instant, interval: Duration, task: F, ) -> SpawnHandle
where F: FnMut(&mut A, &mut A::Context) + 'static,

Spawns a periodic task function to begin executing at the given start time, and with the given interval duration.
source§

impl<A> AsyncContextParts<A> for Context<A>
where A: Actor<Context = Self>,

source§

fn parts(&mut self) -> &mut ContextParts<A>

source§

impl<A: Actor<Context = Context<A>>> Debug for Context<A>

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<A> Default for Context<A>
where A: Actor<Context = Self>,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<A, M> ToEnvelope<A, M> for Context<A>
where A: Actor<Context = Context<A>> + Handler<M>, M: Message + Send + 'static, M::Result: Send,

source§

fn pack(msg: M, tx: Option<Sender<M::Result>>) -> Envelope<A>

Pack message into suitable envelope

Auto Trait Implementations§

§

impl<A> Freeze for Context<A>

§

impl<A> !RefUnwindSafe for Context<A>

§

impl<A> !Send for Context<A>

§

impl<A> !Sync for Context<A>

§

impl<A> Unpin for Context<A>

§

impl<A> !UnwindSafe for Context<A>

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> 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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.