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>
impl<A: Actor> PreparedActor<A>
Sourcepub fn new(
(mailbox_tx, mailbox_rx): (MailboxSender<A>, MailboxReceiver<A>),
) -> Self
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.
Sourcepub fn actor_ref(&self) -> &ActorRef<A>
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.
Sourcepub fn reply_timeout(self, duration: Duration) -> Self
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.
Sourcepub async fn run(
self,
args: A::Args,
) -> Result<(A, ActorStopReason), PanicError>
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;Sourcepub fn spawn(
self,
args: A::Args,
) -> JoinHandle<Result<(A, ActorStopReason), PanicError>>
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.
Sourcepub fn spawn_in_thread(
self,
args: A::Args,
) -> JoinHandle<Result<(A, ActorStopReason), PanicError>>
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>
impl<A: Actor> Reply for PreparedActor<A>
Source§type Ok = PreparedActor<A>
type Ok = PreparedActor<A>
Source§type Error = Infallible
type Error = Infallible
Source§type Value = PreparedActor<A>
type Value = PreparedActor<A>
Source§fn to_result(self) -> Result<Self, Infallible>
fn to_result(self) -> Result<Self, Infallible>
Result.Source§fn into_any_err(self) -> Option<Box<dyn ReplyError>>
fn into_any_err(self) -> Option<Box<dyn ReplyError>>
Box<any::Any + Send> if it’s an Err, otherwise None.Source§fn into_value(self) -> Self::Value
fn into_value(self) -> Self::Value
Source§fn downcast_err<M: 'static>(err: BoxSendError) -> SendError<M, Self::Error>
fn downcast_err<M: 'static>(err: BoxSendError) -> SendError<M, Self::Error>
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<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
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)
fn as_any(&self) -> &(dyn Any + 'static)
&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)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&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
impl<T> DowncastSend for T
Source§impl<A, T> DynMessage<A> for T
impl<A, T> DynMessage<A> for T
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>>
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>>
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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