1 use clock::now;
2 use timer::{Entry, HandlePriv};
3 use Error;
4 
5 use futures::Poll;
6 
7 use std::sync::Arc;
8 use std::time::{Duration, Instant};
9 
10 /// Registration with a timer.
11 ///
12 /// The association between a `Delay` instance and a timer is done lazily in
13 /// `poll`
14 #[derive(Debug)]
15 pub(crate) struct Registration {
16     entry: Arc<Entry>,
17 }
18 
19 impl Registration {
new(deadline: Instant, duration: Duration) -> Registration20     pub fn new(deadline: Instant, duration: Duration) -> Registration {
21         fn is_send<T: Send + Sync>() {}
22         is_send::<Registration>();
23 
24         Registration {
25             entry: Arc::new(Entry::new(deadline, duration)),
26         }
27     }
28 
deadline(&self) -> Instant29     pub fn deadline(&self) -> Instant {
30         self.entry.time_ref().deadline
31     }
32 
register(&mut self)33     pub fn register(&mut self) {
34         if !self.entry.is_registered() {
35             Entry::register(&mut self.entry)
36         }
37     }
38 
register_with(&mut self, handle: HandlePriv)39     pub fn register_with(&mut self, handle: HandlePriv) {
40         Entry::register_with(&mut self.entry, handle)
41     }
42 
reset(&mut self, deadline: Instant)43     pub fn reset(&mut self, deadline: Instant) {
44         self.entry.time_mut().deadline = deadline;
45         Entry::reset(&mut self.entry);
46     }
47 
reset_timeout(&mut self)48     pub fn reset_timeout(&mut self) {
49         let deadline = now() + self.entry.time_ref().duration;
50         self.entry.time_mut().deadline = deadline;
51         Entry::reset(&mut self.entry);
52     }
53 
is_elapsed(&self) -> bool54     pub fn is_elapsed(&self) -> bool {
55         self.entry.is_elapsed()
56     }
57 
poll_elapsed(&self) -> Poll<(), Error>58     pub fn poll_elapsed(&self) -> Poll<(), Error> {
59         self.entry.poll_elapsed()
60     }
61 }
62 
63 impl Drop for Registration {
drop(&mut self)64     fn drop(&mut self) {
65         Entry::cancel(&self.entry);
66     }
67 }
68