Expand description
An actor execution context.
Implementations
sourceimpl<C, K, S> Context<C, K, S>
impl<C, K, S> Context<C, K, S>
sourcepub fn with<S1>(self, source: S1) -> Context<C, K, Combined<S, S1>>
pub fn with<S1>(self, source: S1) -> Context<C, K, Combined<S, S1>>
Transforms the context to another one with the provided source.
sourcepub fn set_status(&self, status: ActorStatus)
pub fn set_status(&self, status: ActorStatus)
Updates the actor’s status.
ctx.set_status(ActorStatus::ALARMING.with_details("something wrong"));
sourcepub fn close(&self) -> bool
pub fn close(&self) -> bool
Closes the mailbox, that leads to returning None
from recv()
and
try_recv()
after handling all available messages in the mailbox.
Returns true
if the mailbox has just been closed.
sourcepub async fn send<M: Message>(&self, message: M) -> Result<(), SendError<M>>
pub async fn send<M: Message>(&self, message: M) -> Result<(), SendError<M>>
Sends a message using the routing system.
Returns Err
if the message hasn’t reached any mailboxes.
Example
// Fire and forget.
let _ = ctx.send(SomethingHappened).await;
// Fire or fail.
ctx.send(SomethingHappened).await?;
// Fire or log.
if let Ok(err) = ctx.send(SomethingHappened).await {
warn!("...", error = err);
}
sourcepub fn try_send<M: Message>(&self, message: M) -> Result<(), TrySendError<M>>
pub fn try_send<M: Message>(&self, message: M) -> Result<(), TrySendError<M>>
Tries to send a message using the routing system.
Returns
Ok(())
if the message has been added to any mailbox.Err(Full(_))
if some mailboxes are full.Err(Closed(_))
otherwise.
Example
// Fire and forget.
let _ = ctx.try_send(SomethingHappened);
// Fire or fail.
ctx.try_send(SomethingHappened)?;
// Fire or log.
if let Err(err) = ctx.try_send(SomethingHappened) {
warn!("...", error = err);
}
sourcepub fn request<R: Request>(
&self,
request: R
) -> RequestBuilder<'_, C, K, S, R, Any>
pub fn request<R: Request>(
&self,
request: R
) -> RequestBuilder<'_, C, K, S, R, Any>
Returns a request builder.
Example
// Request and wait for a response.
let response = ctx.request(SomeCommand).resolve().await?;
// Request and wait for all responses.
for result in ctx.request(SomeCommand).all().resolve().await {
// ...
}
sourcepub fn request_to<R: Request>(
&self,
recipient: Addr,
request: R
) -> RequestBuilder<'_, C, K, S, R, Any>
pub fn request_to<R: Request>(
&self,
recipient: Addr,
request: R
) -> RequestBuilder<'_, C, K, S, R, Any>
Returns a request builder to the specified recipient.
Example
// Request and wait for a response.
let response = ctx.request_to(addr, SomeCommand).resolve().await?;
// Request and wait for all responses.
for result in ctx.request_to(addr, SomeCommand).all().resolve().await {
// ...
}
sourcepub async fn send_to<M: Message>(
&self,
recipient: Addr,
message: M
) -> Result<(), SendError<M>>
pub async fn send_to<M: Message>(
&self,
recipient: Addr,
message: M
) -> Result<(), SendError<M>>
Sends a message to the specified recipient.
Returns Err
if the message hasn’t reached any mailboxes.
Example
// Fire and forget.
let _ = ctx.send_to(addr, SomethingHappened).await;
// Fire or fail.
ctx.send_to(addr, SomethingHappened).await?;
// Fire or log.
if let Some(err) = ctx.send_to(addr, SomethingHappened).await {
warn!("...", error = err);
}
sourcepub fn try_send_to<M: Message>(
&self,
recipient: Addr,
message: M
) -> Result<(), TrySendError<M>>
pub fn try_send_to<M: Message>(
&self,
recipient: Addr,
message: M
) -> Result<(), TrySendError<M>>
Tries to send a message to the specified recipient.
Returns Err
if the message hasn’t reached mailboxes or they are full.
Example
// Fire and forget.
let _ = ctx.send(SomethingHappened).await;
// Fire or fail.
ctx.send(SomethingHappened).await?;
// Fire or log.
if let Some(err) = ctx.send(SomethingHappened).await {
warn!("...", error = err);
}
sourcepub fn respond<R: Request>(&self, token: ResponseToken<R>, message: R::Response)
pub fn respond<R: Request>(&self, token: ResponseToken<R>, message: R::Response)
Responds to the requester with the provided response.
The token can be used only once.
msg!(match envelope {
(SomeRequest, token) => {
ctx.respond(token, SomeResponse);
}
})
sourcepub fn try_recv(&mut self) -> Result<Envelope, TryRecvError> where
C: 'static,
pub fn try_recv(&mut self) -> Result<Envelope, TryRecvError> where
C: 'static,
sourcepub fn unpack_config<'c>(&self, config: &'c AnyConfig) -> &'c C where
C: for<'de> Deserialize<'de> + 'static,
pub fn unpack_config<'c>(&self, config: &'c AnyConfig) -> &'c C where
C: for<'de> Deserialize<'de> + 'static,
Used to get the typed config from ValidateConfig
.
msg!(match envelope {
(ValidateConfig { config, .. }, token) => {
let new_config = ctx.unpack_config(&config);
ctx.respond(token, Err("oops".into()));
}
})
Trait Implementations
Auto Trait Implementations
impl<C = (), K = Singleton, S = ()> !RefUnwindSafe for Context<C, K, S>
impl<C, K, S> Send for Context<C, K, S> where
C: Send + Sync,
K: Send,
S: Send,
impl<C, K, S> Sync for Context<C, K, S> where
C: Send + Sync,
K: Sync,
S: Sync,
impl<C, K, S> Unpin for Context<C, K, S> where
K: Unpin,
S: Unpin,
impl<C = (), K = Singleton, S = ()> !UnwindSafe for Context<C, K, S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcepub fn to_owned(&self) -> T
pub fn to_owned(&self) -> T
Creates owned data from borrowed data, usually by cloning. Read more
sourcepub fn clone_into(&self, target: &mut T)
pub fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
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
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more