pub struct Context<A> { /* private fields */ }
Expand description
An actor execution context.
Implementations
sourceimpl<A> Context<A>
impl<A> Context<A>
sourcepub fn stop_supervisor(&self, err: Option<Error>)
pub fn stop_supervisor(&self, err: Option<Error>)
Stop the supervisor.
this is ignored by normal actors
pub fn stopped(&self) -> bool
pub fn abort_intervals(&mut self)
pub fn abort_streams(&mut self)
sourcepub fn add_stream<S>(&mut self, stream: S) where
S: Stream + Unpin + Send + 'static,
S::Item: 'static + Send,
A: StreamHandler<S::Item>,
pub fn add_stream<S>(&mut self, stream: S) where
S: Stream + Unpin + Send + 'static,
S::Item: 'static + Send,
A: StreamHandler<S::Item>,
Create a stream handler for the actor.
Examples
use hannibal::*;
use futures::stream;
use std::time::Duration;
#[message(result = "i32")]
struct GetSum;
#[derive(Default)]
struct MyActor(i32);
#[async_trait::async_trait]
impl StreamHandler<i32> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>, msg: i32) {
self.0 += msg;
}
async fn started(&mut self, _ctx: &mut Context<Self>) {
println!("stream started");
}
async fn finished(&mut self, _ctx: &mut Context<Self>) {
println!("stream finished");
}
}
#[async_trait::async_trait]
impl Handler<GetSum> for MyActor {
async fn handle(&mut self, _ctx: &mut Context<Self>, _msg: GetSum) -> i32 {
self.0
}
}
#[async_trait::async_trait]
impl Actor for MyActor {
async fn started(&mut self, ctx: &mut Context<Self>) -> Result<()> {
let values = (0..100).collect::<Vec<_>>();
ctx.add_stream(stream::iter(values));
Ok(())
}
}
#[hannibal::main]
async fn main() -> Result<()> {
let mut addr = MyActor::start_default().await?;
sleep(Duration::from_secs(1)).await; // Wait for the stream to complete
let res = addr.call(GetSum).await?;
assert_eq!(res, (0..100).sum::<i32>());
Ok(())
}
sourcepub fn send_later<T>(&mut self, msg: T, after: Duration) where
A: Handler<T>,
T: Message<Result = ()>,
pub fn send_later<T>(&mut self, msg: T, after: Duration) where
A: Handler<T>,
T: Message<Result = ()>,
Sends the message msg
to self after a specified period of time.
We use Sender
instead of Addr
so that the interval doesn’t keep reference to address and prevent the actor from being dropped and stopped
sourcepub fn send_interval_with<T, F>(&mut self, f: F, dur: Duration) where
A: Handler<T>,
F: Fn() -> T + Sync + Send + 'static,
T: Message<Result = ()>,
pub fn send_interval_with<T, F>(&mut self, f: F, dur: Duration) where
A: Handler<T>,
F: Fn() -> T + Sync + Send + 'static,
T: Message<Result = ()>,
Sends the message to self, at a specified fixed interval.
The message is created each time using a closure f
.
sourcepub fn send_interval<T>(&mut self, msg: T, dur: Duration) where
A: Handler<T>,
T: Message<Result = ()> + Clone + Sync,
pub fn send_interval<T>(&mut self, msg: T, dur: Duration) where
A: Handler<T>,
T: Message<Result = ()> + Clone + Sync,
Sends the message msg
to self, at a specified fixed interval.
Auto Trait Implementations
impl<A> !RefUnwindSafe for Context<A>
impl<A> Send for Context<A>
impl<A> Sync for Context<A>
impl<A> Unpin for Context<A>
impl<A> !UnwindSafe for Context<A>
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