2018-09-20 00:05:14 +02:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
|
|
use futures::future::{ok, FutureResult};
|
2018-09-20 20:16:12 +02:00
|
|
|
use futures::{Async, Future, Poll};
|
2018-09-20 00:05:14 +02:00
|
|
|
use tokio_current_thread::spawn;
|
|
|
|
use tokio_timer::sleep;
|
|
|
|
|
2018-10-03 07:18:07 +02:00
|
|
|
use super::cell::Cell;
|
2018-09-20 20:16:12 +02:00
|
|
|
use super::service::{NewService, Service};
|
|
|
|
use super::Never;
|
2018-09-20 00:05:14 +02:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2018-10-29 23:48:56 +01:00
|
|
|
pub struct LowResTime(Cell<Inner>);
|
2018-09-20 00:05:14 +02:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Inner {
|
2018-10-09 00:38:52 +02:00
|
|
|
resolution: Duration,
|
2018-09-20 00:05:14 +02:00
|
|
|
current: Option<Instant>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Inner {
|
2018-10-09 00:38:52 +02:00
|
|
|
fn new(resolution: Duration) -> Self {
|
2018-09-20 00:05:14 +02:00
|
|
|
Inner {
|
2018-10-09 00:38:52 +02:00
|
|
|
resolution,
|
2018-09-20 20:16:12 +02:00
|
|
|
current: None,
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
impl LowResTime {
|
|
|
|
pub fn with(resolution: Duration) -> LowResTime {
|
|
|
|
LowResTime(Cell::new(Inner::new(resolution)))
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
2018-09-20 20:16:12 +02:00
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
pub fn timer(&self) -> LowResTimeService {
|
|
|
|
LowResTimeService(self.0.clone())
|
2018-09-20 20:16:12 +02:00
|
|
|
}
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
impl Default for LowResTime {
|
2018-09-20 00:05:14 +02:00
|
|
|
fn default() -> Self {
|
2018-10-29 23:48:56 +01:00
|
|
|
LowResTime(Cell::new(Inner::new(Duration::from_secs(1))))
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
impl NewService for LowResTime {
|
2018-09-20 00:05:14 +02:00
|
|
|
type Request = ();
|
|
|
|
type Response = Instant;
|
2018-09-20 20:16:12 +02:00
|
|
|
type Error = Never;
|
|
|
|
type InitError = Never;
|
2018-10-29 23:48:56 +01:00
|
|
|
type Service = LowResTimeService;
|
2018-09-20 00:05:14 +02:00
|
|
|
type Future = FutureResult<Self::Service, Self::InitError>;
|
|
|
|
|
|
|
|
fn new_service(&self) -> Self::Future {
|
2018-09-20 20:16:12 +02:00
|
|
|
ok(self.timer())
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
2018-10-29 23:48:56 +01:00
|
|
|
pub struct LowResTimeService(Cell<Inner>);
|
2018-09-20 00:05:14 +02:00
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
impl LowResTimeService {
|
|
|
|
pub fn with(resolution: Duration) -> LowResTimeService {
|
|
|
|
LowResTimeService(Cell::new(Inner::new(resolution)))
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
|
2018-09-20 20:16:12 +02:00
|
|
|
/// Get current time. This function has to be called from
|
|
|
|
/// future's poll method, otherwise it panics.
|
2018-10-29 23:40:10 +01:00
|
|
|
pub fn now(&self) -> Instant {
|
2018-09-20 00:05:14 +02:00
|
|
|
let cur = self.0.borrow().current.clone();
|
|
|
|
if let Some(cur) = cur {
|
2018-09-20 20:16:12 +02:00
|
|
|
cur
|
2018-09-20 00:05:14 +02:00
|
|
|
} else {
|
|
|
|
let now = Instant::now();
|
|
|
|
let inner = self.0.clone();
|
|
|
|
let interval = {
|
|
|
|
let mut b = inner.borrow_mut();
|
|
|
|
b.current = Some(now);
|
2018-10-09 00:38:52 +02:00
|
|
|
b.resolution
|
2018-09-20 00:05:14 +02:00
|
|
|
};
|
|
|
|
|
2018-09-20 20:16:12 +02:00
|
|
|
spawn(sleep(interval).map_err(|_| panic!()).and_then(move |_| {
|
|
|
|
inner.borrow_mut().current.take();
|
|
|
|
Ok(())
|
|
|
|
}));
|
|
|
|
now
|
2018-09-20 00:05:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-20 20:16:12 +02:00
|
|
|
|
2018-10-29 23:48:56 +01:00
|
|
|
impl Service for LowResTimeService {
|
2018-09-20 20:16:12 +02:00
|
|
|
type Request = ();
|
|
|
|
type Response = Instant;
|
|
|
|
type Error = Never;
|
|
|
|
type Future = FutureResult<Self::Response, Self::Error>;
|
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
Ok(Async::Ready(()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, _: ()) -> Self::Future {
|
|
|
|
ok(self.now())
|
|
|
|
}
|
|
|
|
}
|