Skip to main content

PreparedActor

Struct PreparedActor 

Source
pub struct PreparedActor<A: Actor> { /* private fields */ }
Expand description

A PreparedActor represents an actor that has been initialized and is ready to be either run in the current task or spawned into a new task.

The PreparedActor provides access to the actor’s ActorRef for interacting with the actor before it starts running. It allows for flexible execution, either by running the actor synchronously in the current task or spawning it in a separate task or thread.

Implementations§

Source§

impl<A: Actor> PreparedActor<A>

Source

pub fn new( (mailbox_tx, mailbox_rx): (MailboxSender<A>, MailboxReceiver<A>), ) -> Self

Creates a new prepared actor with a specific mailbox configuration, allowing access to its ActorRef before spawning.

This function allows you to explicitly specify a mailbox when preparing an actor. Use this when you need custom mailbox behavior or capacity.

This is typically created though Actor::prepare and Actor::prepare_with_mailbox.

Source

pub fn actor_ref(&self) -> &ActorRef<A>

Returns a reference to the ActorRef, which can be used to send messages to the actor.

The ActorRef can be used for interaction before the actor starts processing its event loop.

Note: if you intend to configure a default reply timeout, do so before cloning the ref out, as the value is read off the clone at the time it’s made.

Source

pub fn reply_timeout(self, duration: Duration) -> Self

Sets a default reply timeout applied to every ask request that doesn’t specify its own.

An ask whose handler doesn’t reply within this duration resolves with a timeout error instead of waiting forever. A timeout set at the call site with AskRequest::reply_timeout always takes precedence over this default. The default applies to async ask requests; the blocking variants are unaffected.

Configure this before cloning the ActorRef out (see PreparedActor::actor_ref), otherwise the clone won’t observe the default.

Source

pub async fn run( self, args: A::Args, ) -> Result<(A, ActorStopReason), PanicError>

Runs the actor in the current context without spawning a separate task, until the actor is stopped.

This is useful when you need to run an actor synchronously in the current context, without background execution, and when the actor is expected to be short-lived.

Note that the actor’s mailbox may already contain messages before run is called. In this case, the actor will process all pending messages in the mailbox before completing.

§Example

let prepared_actor = MyActor::prepare();
// Send it a message before it runs
prepared_actor.actor_ref().tell("hello!").await?;
prepared_actor.run(MyActor).await;
Source

pub fn spawn( self, args: A::Args, ) -> JoinHandle<Result<(A, ActorStopReason), PanicError>>

Spawns the actor in a new background tokio task, returning the JoinHandle.

See Spawn::spawn for more information.

Source

pub fn spawn_in_thread( self, args: A::Args, ) -> JoinHandle<Result<(A, ActorStopReason), PanicError>>

Spawns the actor in a new background thread, returning the JoinHandle.

See Spawn::spawn_in_thread for more information.

Trait Implementations§

Source§

impl<A: Actor> Reply for PreparedActor<A>

Source§

type Ok = PreparedActor<A>

The success type in the reply.
Source§

type Error = Infallible

The error type in the reply.
Source§

type Value = PreparedActor<A>

The type sent back to the receiver. Read more
Source§

fn to_result(self) -> Result<Self, Infallible>

Converts a reply to a Result.
Source§

fn into_any_err(self) -> Option<Box<dyn ReplyError>>

Converts the reply into a Box<any::Any + Send> if it’s an Err, otherwise None.
Source§

fn into_value(self) -> Self::Value

Converts the type to Self::Reply. Read more
Source§

fn downcast_ok(ok: Box<dyn Any>) -> Self::Ok

Downcasts a Box<dyn Any> into the Self::Ok type.
Source§

fn downcast_err<M: 'static>(err: BoxSendError) -> SendError<M, Self::Error>

Downcasts a Box<dyn Any> into a Self::Error type.

Auto Trait Implementations§

§

impl<A> !RefUnwindSafe for PreparedActor<A>

§

impl<A> !Sync for PreparedActor<A>

§

impl<A> !UnwindSafe for PreparedActor<A>

§

impl<A> Freeze for PreparedActor<A>

§

impl<A> Send for PreparedActor<A>

§

impl<A> Unpin for PreparedActor<A>

§

impl<A> UnsafeUnpin for PreparedActor<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<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Converts Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Converts Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Converts &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Converts &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSend for T
where T: Any + Send,

Source§

fn into_any_send(self: Box<T>) -> Box<dyn Any + Send>

Converts Box<Trait> (where Trait: DowncastSend) to Box<dyn Any + Send>, which can then be downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

impl<A, T> DynMessage<A> for T
where A: Actor + Message<T>, T: Send + 'static,

Source§

fn handle_dyn<'a>( self: Box<T>, state: &'a mut A, actor_ref: ActorRef<A>, tx: Option<Sender<Result<Box<dyn Any + Send>, SendError<Box<dyn Any + Send>, Box<dyn Any + Send>>>>>, stop: &'a mut bool, ) -> Pin<Box<dyn Future<Output = Result<(), Box<dyn ReplyError>>> + Send + 'a>>

Handles the dyn message with the provided actor state, ref, and reply sender.
Source§

fn as_any(self: Box<T>) -> Box<dyn Any + Send>

Casts the type to a Box<dyn Any + Send>.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FutureExt for T

Source§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Source§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more