Skip to content

Instantly share code, notes, and snippets.

@apatrushev
Created March 8, 2018 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apatrushev/0b8b5d3de310650d33f1f5a70d4e8459 to your computer and use it in GitHub Desktop.
Save apatrushev/0b8b5d3de310650d33f1f5a70d4e8459 to your computer and use it in GitHub Desktop.
extern crate actix;
extern crate tokio_core;
extern crate futures;
use std::time::Duration;
use futures::Stream;
use tokio_core::reactor::Interval;
use actix::prelude::*;
use actix::{Actor, Context, StreamHandler, SpawnHandle, Arbiter};
struct ClosableActor {
source: SpawnHandle,
}
impl Actor for ClosableActor {
type Context = Context<Self>;
}
struct ClosablePacket;
impl<K> StreamHandler<ClosablePacket, K> for ClosableActor {
fn handle(&mut self, _: ClosablePacket, ctx: &mut Context<Self>) {
ctx.cancel_future(self.source);
}
}
fn main() {
let sys = actix::System::new("closable");
let _: () = ClosableActor::create(|ctx| {
ClosableActor {
source: ctx.add_stream(
Interval::new(
Duration::from_millis(1),
Arbiter::handle()
).unwrap().map(|_| ClosablePacket)
),
}
});
std::process::exit(sys.run());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment