pub struct Context<M, RT> { /* private fields */ }
Expand description
The context in which an actor is executed.
This context can be used for a number of things including receiving messages and getting access to the runtime.
The actor::Context
comes in two flavours:
ThreadLocal
(default) is the optimised version, but doesn’t allow the actor to move between threads. Actor started withRuntimeRef::try_spawn_local
will get this flavour of context.ThreadSafe
is the flavour that allows the actor to be moved between threads. Actor started withRuntimeRef::try_spawn
will get this flavour of context.
Implementations
sourceimpl<M, RT> Context<M, RT>
impl<M, RT> Context<M, RT>
sourcepub fn try_receive_next(&mut self) -> Result<M, RecvError>
pub fn try_receive_next(&mut self) -> Result<M, RecvError>
Attempt to receive the next message.
This will attempt to receive next message if one is available. If the
actor wants to wait until a message is received receive_next
can be
used, which returns a Future<Output = M>
.
Examples
An actor that receives a name to greet, or greets the entire world.
use heph::actor;
use heph_rt::ThreadLocal;
async fn greeter_actor(mut ctx: actor::Context<String, ThreadLocal>) {
if let Ok(name) = ctx.try_receive_next() {
println!("Hello: {}", name);
} else {
println!("Hello world");
}
}
sourcepub fn receive_next<'ctx>(&'ctx mut self) -> ReceiveMessage<'ctx, M>ⓘNotable traits for ReceiveMessage<'ctx, M>impl<'ctx, M> Future for ReceiveMessage<'ctx, M> type Output = Result<M, NoMessages>;
pub fn receive_next<'ctx>(&'ctx mut self) -> ReceiveMessage<'ctx, M>ⓘNotable traits for ReceiveMessage<'ctx, M>impl<'ctx, M> Future for ReceiveMessage<'ctx, M> type Output = Result<M, NoMessages>;
Receive the next message.
This returns a Future
that will complete once a message is ready.
Examples
An actor that await a message and prints it.
use heph::actor;
use heph_rt::ThreadLocal;
async fn print_actor(mut ctx: actor::Context<String, ThreadLocal>) {
if let Ok(msg) = ctx.receive_next().await {
println!("Got a message: {}", msg);
}
}
Same as the example above, but this actor will only wait for a limited amount of time.
use std::time::Duration;
use heph::actor;
use heph_rt::util::either;
use heph_rt::ThreadLocal;
use heph_rt::timer::Timer;
async fn print_actor(mut ctx: actor::Context<String, ThreadLocal>) {
// Create a timer, this will be ready once the timeout has
// passed.
let timeout = Timer::after(&mut ctx, Duration::from_millis(100));
// Create a future to receive a message.
let msg_future = ctx.receive_next();
// Now let them race!
match either(msg_future, timeout).await {
Ok(Ok(msg)) => println!("Got a message: {}", msg),
Ok(Err(_)) => println!("No message"),
Err(_) => println!("Timed out receiving message"),
}
}
sourcepub fn runtime(&mut self) -> &mut RT
pub fn runtime(&mut self) -> &mut RT
Get mutable access to the runtime this actor is running in.
sourcepub const fn runtime_ref(&self) -> &RT
pub const fn runtime_ref(&self) -> &RT
Get access to the runtime this actor is running in.
Trait Implementations
Auto Trait Implementations
impl<M, RT> !RefUnwindSafe for Context<M, RT>
impl<M, RT> Send for Context<M, RT> where
M: Send,
RT: Send,
impl<M, RT> Sync for Context<M, RT> where
RT: Sync,
impl<M, RT> Unpin for Context<M, RT> where
RT: Unpin,
impl<M, RT> !UnwindSafe for Context<M, RT>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more