Files
tokio/tokio-timer/src/lib.rs
T

95 lines
2.6 KiB
Rust
Raw Normal View History

#![doc(html_root_url = "https://docs.rs/tokio-timer/0.3.0-alpha.2")]
#![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
//! 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.
//!
//! * [`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
//!
//! * [`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
//! [`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
//! [`Timeout`]: struct.Timeout.html
2018-03-28 22:26:47 -07:00
//! [`Interval`]: struct.Interval.html
//! [`Timer`]: timer/struct.Timer.html
pub mod clock;
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;
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;
mod wheel;
2018-05-11 12:16:08 -04:00
2019-05-14 10:27:36 -07:00
pub use delay::Delay;
#[doc(inline)]
2019-05-14 10:27:36 -07:00
pub use delay_queue::DelayQueue;
pub use error::Error;
pub use interval::Interval;
#[doc(inline)]
2019-05-14 10:27:36 -07:00
pub use timeout::Timeout;
pub use timer::{set_default, Timer};
2018-05-11 12:16:08 -04: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)
}
// ===== 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(),
};
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))
}