pub trait Coroutine<IN>:
Sized
+ Actor
+ Clone {
type Result;
// Required method
fn calculate<'async_trait>(
self,
input: IN,
) -> Pin<Box<dyn Future<Output = Self::Result> + Send + 'async_trait>>
where Self: 'async_trait;
}
Available on crate features
runtime-tokio
or runtime-async-std
only.Expand description
Alternative to Handler
that allows parallel message processing.
By default, messages for an Actor
are processed sequentially, thus efficiently
will use a single core.
However, in some cases it makes more sense to allow parallel processing, if it’s some kind of caluclation or an access to a shared resource.
§Examples
This example assumes that messages
is used with rt-tokio
feature enabled.
#[derive(Clone)]
struct Sum;
#[async_trait]
impl Actor for Sum {}
#[async_trait]
impl Coroutine<(u8, u8)> for Sum {
type Result = u16;
// Note that in this impl the first argument is `self` rather than `&mut self`
// and there is no `Context` argument.
async fn calculate(self, (a, b): (u8, u8)) -> u16 {
(a as u16) + (b as u16)
}
}
#[tokio::main]
async fn main() {
let mut addr = Sum.spawn();
let result = addr.calculate((22, 20)).await.unwrap();
assert_eq!(result, 42);
}
Required Associated Types§
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.