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 with RuntimeRef::try_spawn_local will get this flavour of context.
  • ThreadSafe is the flavour that allows the actor to be moved between threads. Actor started with RuntimeRef::try_spawn will get this flavour of context.

Implementations

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");
    }
}

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"),
    }
}

Returns a reference to this actor.

Get mutable access to the runtime this actor is running in.

Get access to the runtime this actor is running in.

Trait Implementations

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.