[][src]Struct xactor::Context

pub struct Context<A> { /* fields omitted */ }

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>(&mut self, stream: S) where
    S: Stream + Unpin + Send + 'static,
    S::Item: 'static + Send,
    A: StreamHandler<S::Item>, 
[src]

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: &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(())
    }
}

#[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]

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]

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]

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]

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.