Struct heph::actor::SyncContext
source · [−]pub struct SyncContext<M, RT> { /* private fields */ }
Expand description
The context in which a synchronous actor is executed.
This context can be used for a number of things including receiving messages.
Implementations
sourceimpl<M, RT> SyncContext<M, RT>
impl<M, RT> SyncContext<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 the next message if one is available. If
the actor wants to wait until a message is received receive_next
can
be used, which blocks until a message is ready.
Examples
A synchronous actor that receives a name to greet, or greets the entire world.
use heph::actor::SyncContext;
fn greeter_actor<RT>(mut ctx: SyncContext<String, RT>) {
if let Ok(name) = ctx.try_receive_next() {
println!("Hello {}", name);
} else {
println!("Hello world");
}
}
sourcepub fn receive_next(&mut self) -> Result<M, NoMessages>
pub fn receive_next(&mut self) -> Result<M, NoMessages>
Receive the next message.
Returns the next message available. If no messages are currently available it will block until a message becomes available or until all actor references (that reference this actor) are dropped.
Examples
An actor that waits for a message and prints it.
use heph::actor::SyncContext;
fn print_actor<RT>(mut ctx: SyncContext<String, RT>) {
if let Ok(msg) = ctx.receive_next() {
println!("Got a message: {}", msg);
} else {
eprintln!("No message received");
}
}
sourcepub fn block_on<Fut>(&mut self, fut: Fut) -> Fut::Output where
Fut: Future,
pub fn block_on<Fut>(&mut self, fut: Fut) -> Fut::Output where
Fut: Future,
Block on a Future
waiting for it’s completion.
Limitations
Any Future
returned by a type that is bound to an actor (see Bound
trait of the heph-rt crate) cannot be used by this function. Those
types use specialised wake-up mechanisms bypassing the Future
’s
task
system.
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 SyncContext<M, RT>
impl<M, RT> Send for SyncContext<M, RT> where
M: Send,
RT: Send,
impl<M, RT> Sync for SyncContext<M, RT> where
RT: Sync,
impl<M, RT> Unpin for SyncContext<M, RT> where
RT: Unpin,
impl<M, RT> !UnwindSafe for SyncContext<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