Skip to content

Instantly share code, notes, and snippets.

@vnermolaev
Created September 18, 2019 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vnermolaev/fd19adcf92704ff7e79571c4bdcbdb77 to your computer and use it in GitHub Desktop.
Save vnermolaev/fd19adcf92704ff7e79571c4bdcbdb77 to your computer and use it in GitHub Desktop.
use failure::Error;
use futures::{Async, Future, Poll};
use std::time::{Duration, Instant};
use tokio::runtime::Runtime;
use tokio::timer::Delay;
#[macro_use]
extern crate futures;
struct Node {
pub value: u32,
pub delay: Delay,
}
impl Node {
fn new() -> Node {
Node {
value: 0,
delay: Delay::new(Instant::now() + Duration::from_secs(1)),
}
}
fn inc(&mut self) {
self.value += 1;
println!("{}", self.value);
}
}
impl Future for Node {
type Item = ();
type Error = Error;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
self.inc();
self.delay = Delay::new(Instant::now() + Duration::from_secs(1));
let _ = try_ready!(self.delay.poll());
Ok(Async::NotReady)
}
}
fn main() {
let mut rt = Runtime::new().unwrap();
let node = Node::new();
rt.block_on(node).expect("Something went terribly wrong");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment