-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwork.rs
52 lines (43 loc) · 1.47 KB
/
work.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use super::{Handle, HandleFuture};
use futures::{try_ready, Future, Poll};
use std::time::{Duration, Instant};
use tokio::timer::Delay;
/// This future simulates waiting for the shutdown signal,
/// in practice this would be select against some other future.
pub struct Thing {
pub handle: HandleFuture,
pub delay: Option<Delay>,
drop_handle: Option<Handle>,
}
impl Thing {
pub fn new(handle: Handle) -> Self {
Self {
handle: handle.into_future(),
delay: None,
drop_handle: None,
}
}
}
impl Future for Thing {
type Item = ();
type Error = ();
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
if let Some(delay) = &mut self.delay {
try_ready!(delay.poll().map_err(drop));
} else {
let handle = try_ready!(self.handle.poll());
// This is to show that we can drop the returned handle
// so if we select this we can get a value back we can use
// this is the same handle aka it does not register a new task.
self.drop_handle = Some(handle);
println!("shutting down");
let deadline = Instant::now() + Duration::from_secs(1);
self.delay = Some(Delay::new(deadline));
}
// lets let the shutdown future that this task is done
let drop_handle = self.drop_handle.take().unwrap();
println!("Dropping handle");
drop(drop_handle);
Ok(().into())
}
}