Created
September 18, 2019 13:07
-
-
Save vnermolaev/fd19adcf92704ff7e79571c4bdcbdb77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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