2019-08-17 23:34:25 -07:00
|
|
|
#![doc(html_root_url = "https://docs.rs/tokio-timer/0.3.0-alpha.2")]
|
2019-08-11 04:28:52 +09:00
|
|
|
#![warn(
|
|
|
|
|
missing_debug_implementations,
|
|
|
|
|
missing_docs,
|
|
|
|
|
rust_2018_idioms,
|
|
|
|
|
unreachable_pub
|
|
|
|
|
)]
|
2019-05-14 10:27:36 -07:00
|
|
|
#![doc(test(no_crate_inject, attr(deny(rust_2018_idioms))))]
|
2019-06-30 08:48:53 -07:00
|
|
|
#![feature(async_await)]
|
2018-08-24 08:58:26 -07:00
|
|
|
|
2018-08-22 20:39:46 -07:00
|
|
|
//! Utilities for tracking time.
|
2018-03-28 22:26:47 -07:00
|
|
|
//!
|
|
|
|
|
//! This crate provides a number of utilities for working with periods of time:
|
|
|
|
|
//!
|
2018-03-30 14:21:48 -07:00
|
|
|
//! * [`Delay`]: A future that completes at a specified instant in time.
|
2018-03-28 22:26:47 -07:00
|
|
|
//!
|
|
|
|
|
//! * [`Interval`] A stream that yields at fixed time intervals.
|
|
|
|
|
//!
|
2018-11-20 00:04:55 +01:00
|
|
|
//! * [`Throttle`]: Throttle down a stream by enforcing a fixed delay between items.
|
|
|
|
|
//!
|
2018-08-22 20:39:46 -07:00
|
|
|
//! * [`Timeout`]: Wraps a future or stream, setting an upper bound to the
|
|
|
|
|
//! amount of time it is allowed to execute. If the future or stream does not
|
2018-08-25 15:26:41 -04:00
|
|
|
//! complete in time, then it is canceled and an error is returned.
|
2018-03-28 22:26:47 -07:00
|
|
|
//!
|
2018-08-20 21:47:10 -07:00
|
|
|
//! * [`DelayQueue`]: A queue where items are returned once the requested delay
|
|
|
|
|
//! has expired.
|
|
|
|
|
//!
|
2018-03-28 22:26:47 -07:00
|
|
|
//! These three types are backed by a [`Timer`] instance. In order for
|
2018-08-22 20:39:46 -07:00
|
|
|
//! [`Delay`], [`Interval`], and [`Timeout`] to function, the associated
|
2018-03-28 22:26:47 -07:00
|
|
|
//! [`Timer`] instance must be running on some thread.
|
|
|
|
|
//!
|
2018-03-30 14:21:48 -07:00
|
|
|
//! [`Delay`]: struct.Delay.html
|
2019-01-12 19:06:55 +01:00
|
|
|
//! [`DelayQueue`]: struct.DelayQueue.html
|
2018-11-20 00:04:55 +01:00
|
|
|
//! [`Throttle`]: throttle/struct.Throttle.html
|
2018-08-22 20:39:46 -07:00
|
|
|
//! [`Timeout`]: struct.Timeout.html
|
2018-03-28 22:26:47 -07:00
|
|
|
//! [`Interval`]: struct.Interval.html
|
|
|
|
|
//! [`Timer`]: timer/struct.Timer.html
|
|
|
|
|
|
2018-06-06 16:04:39 -07:00
|
|
|
pub mod clock;
|
2018-08-20 21:47:10 -07:00
|
|
|
pub mod delay_queue;
|
2019-06-30 08:48:53 -07:00
|
|
|
#[cfg(feature = "async-traits")]
|
2018-11-20 00:04:55 +01:00
|
|
|
pub mod throttle;
|
2018-08-22 20:39:46 -07:00
|
|
|
pub mod timeout;
|
2018-03-28 22:26:47 -07:00
|
|
|
pub mod timer;
|
|
|
|
|
|
2018-03-30 14:21:48 -07:00
|
|
|
mod delay;
|
2018-03-28 22:26:47 -07:00
|
|
|
mod error;
|
|
|
|
|
mod interval;
|
2018-08-20 21:47:10 -07:00
|
|
|
mod wheel;
|
2018-05-11 12:16:08 -04:00
|
|
|
|
2019-05-14 10:27:36 -07:00
|
|
|
pub use delay::Delay;
|
2018-08-20 21:47:10 -07:00
|
|
|
#[doc(inline)]
|
2019-05-14 10:27:36 -07:00
|
|
|
pub use delay_queue::DelayQueue;
|
|
|
|
|
pub use error::Error;
|
|
|
|
|
pub use interval::Interval;
|
2018-08-20 21:47:10 -07:00
|
|
|
#[doc(inline)]
|
2019-05-14 10:27:36 -07:00
|
|
|
pub use timeout::Timeout;
|
2019-08-15 22:00:57 +02:00
|
|
|
pub use timer::{set_default, Timer};
|
2018-05-11 12:16:08 -04:00
|
|
|
|
2018-08-20 21:47:10 -07:00
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
|
2018-05-11 12:16:08 -04:00
|
|
|
/// Create a Future that completes in `duration` from now.
|
|
|
|
|
pub fn sleep(duration: Duration) -> Delay {
|
|
|
|
|
Delay::new(Instant::now() + duration)
|
|
|
|
|
}
|
2018-08-20 21:47:10 -07:00
|
|
|
|
|
|
|
|
// ===== Internal utils =====
|
|
|
|
|
|
|
|
|
|
enum Round {
|
|
|
|
|
Up,
|
|
|
|
|
Down,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Convert a `Duration` to milliseconds, rounding up and saturating at
|
|
|
|
|
/// `u64::MAX`.
|
|
|
|
|
///
|
|
|
|
|
/// The saturating is fine because `u64::MAX` milliseconds are still many
|
|
|
|
|
/// million years.
|
|
|
|
|
#[inline]
|
|
|
|
|
fn ms(duration: Duration, round: Round) -> u64 {
|
|
|
|
|
const NANOS_PER_MILLI: u32 = 1_000_000;
|
|
|
|
|
const MILLIS_PER_SEC: u64 = 1_000;
|
|
|
|
|
|
|
|
|
|
// Round up.
|
|
|
|
|
let millis = match round {
|
|
|
|
|
Round::Up => (duration.subsec_nanos() + NANOS_PER_MILLI - 1) / NANOS_PER_MILLI,
|
2019-07-26 03:47:14 +09:00
|
|
|
Round::Down => duration.subsec_millis(),
|
2018-08-20 21:47:10 -07:00
|
|
|
};
|
|
|
|
|
|
2019-02-21 11:56:15 -08:00
|
|
|
duration
|
|
|
|
|
.as_secs()
|
|
|
|
|
.saturating_mul(MILLIS_PER_SEC)
|
2019-07-26 03:47:14 +09:00
|
|
|
.saturating_add(u64::from(millis))
|
2018-08-20 21:47:10 -07:00
|
|
|
}
|