[−][src]Struct xactor::Context
An actor execution context.
Implementations
impl<A> Context<A>
[src]
pub fn address(&self) -> Addr<A>
[src]
Returns the address of the actor.
pub fn actor_id(&self) -> u64
[src]
Returns the id of the actor.
pub fn stop(&self, err: Option<Error>)
[src]
Stop the actor.
pub fn add_stream<S>(&self, stream: S) where
S: Stream + Unpin + Send + 'static,
S::Item: 'static + Send,
A: StreamHandler<S::Item>,
[src]
S: Stream + Unpin + Send + 'static,
S::Item: 'static + Send,
A: StreamHandler<S::Item>,
Create a stream handler for the actor.
Examples
use xactor::*; 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: &Context<Self>, msg: i32) { self.0 += msg; } async fn started(&mut self, _ctx: &Context<Self>) { println!("stream started"); } async fn finished(&mut self, _ctx: &Context<Self>) { println!("stream finished"); } } #[async_trait::async_trait] impl Handler<GetSum> for MyActor { async fn handle(&mut self, _ctx: &Context<Self>, _msg: GetSum) -> i32 { self.0 } } #[async_trait::async_trait] impl Actor for MyActor { async fn started(&mut self, ctx: &Context<Self>) -> Result<()> { let values = (0..100).collect::<Vec<_>>(); ctx.add_stream(stream::iter(values)); Ok(()) } } #[xactor::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(()) }
pub fn send_later<T>(&self, msg: T, after: Duration) where
A: Handler<T>,
T: Message<Result = ()>,
[src]
A: Handler<T>,
T: Message<Result = ()>,
Sends the message msg
to self after a specified period of time.
pub fn send_interval_with<T, F>(&self, f: F, dur: Duration) where
A: Handler<T>,
F: Fn() -> T + Sync + Send + 'static,
T: Message<Result = ()>,
[src]
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
.
pub fn send_interval<T>(&self, msg: T, dur: Duration) where
A: Handler<T>,
T: Message<Result = ()> + Clone + Sync,
[src]
A: Handler<T>,
T: Message<Result = ()> + Clone + Sync,
Sends the message msg
to self, at a specified fixed interval.
pub async fn subscribe<'_, T: Message<Result = ()>>(&'_ self) -> Result<()> where
A: Handler<T>,
[src]
A: Handler<T>,
Subscribes to a message of a specified type.
pub async fn unsubscribe<'_, T: Message<Result = ()>>(&'_ self) -> Result<()>
[src]
Unsubscribe to a message of a specified type.
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
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,