Trait actix::StreamHandler[][src]

pub trait StreamHandler<I, E> where
    Self: Actor
{ fn handle(&mut self, item: Result<Option<I>, E>, ctx: &mut Self::Context); fn add_stream<S>(fut: S, ctx: &mut Self::Context) -> SpawnHandle
    where
        Self::Context: AsyncContext<Self>,
        S: Stream<Item = I, Error = E> + 'static,
        I: 'static,
        E: 'static
, { ... } }

Stream handler

This is helper trait that allows to handle Stream in a similar way as normal actor messages.

Required Methods

Method is called for every message received by this Actor

  • Ok(Some(t)) - new element from the stream
  • Ok(None) - end of stream
  • Err(e) - stream generated the given failure

Provided Methods

This method register stream to an actor context and allows to handle Stream in similar way as normal actor messages.

use actix::prelude::*;
use futures::stream::once;

#[derive(Message)]
struct Ping;

struct MyActor;

impl StreamHandler<Ping, io::Error> for MyActor {
    fn handle(&mut self, msg: io::Result<Option<Ping>>, ctx: &mut Context<MyActor>) {
        match msg {
            Ok(Some(_)) => println!("PING"),
            _ => println!("finished"),
        }
    }
}

impl Actor for MyActor {
    type Context = Context<Self>;

    fn started(&mut self, ctx: &mut Context<Self>) {
        // add stream
        Self::add_stream(once::<Ping, io::Error>(Ok(Ping)), ctx);
    }
}

Implementors