Introduce Timeout and deprecate Deadline. (#558)

This patch introduces `Timeout`. This new type allows setting a timeout
both using a duration and an instant. Given this overlap with
`Deadline`, `Deadline` is deprecated.

In addition to supporting future timeouts, the `Timeout` combinator is
able to provide timeout functionality to streams. It does this by
applying a duration based timeout to each item being yielded.

The main reason for introducing `Timeout` is that a deadline approach
does not work with streams. Since `Timeout` needed to be introduced
anyway, keeping `Deadline` around does not make sense.
This commit is contained in:
Carl Lerche
2018-08-22 20:39:46 -07:00
committed by GitHub
parent cf184eb326
commit 8bf2e9aeb0
9 changed files with 521 additions and 44 deletions
+4 -2
View File
@@ -11,7 +11,7 @@ use std::ptr;
use std::sync::{Arc, Weak};
use std::sync::atomic::AtomicBool;
use std::sync::atomic::Ordering::{SeqCst, Relaxed};
use std::time::Instant;
use std::time::{Instant, Duration};
use std::u64;
/// Internal state shared between a `Delay` instance and the timer.
@@ -95,6 +95,7 @@ pub(crate) struct Entry {
#[derive(Debug)]
pub(crate) struct Time {
pub(crate) deadline: Instant,
pub(crate) duration: Duration,
}
/// Flag indicating a timer entry has elapsed
@@ -106,10 +107,11 @@ const ERROR: u64 = u64::MAX;
// ===== impl Entry =====
impl Entry {
pub fn new(deadline: Instant) -> Entry {
pub fn new(deadline: Instant, duration: Duration) -> Entry {
Entry {
time: CachePadded::new(UnsafeCell::new(Time {
deadline,
duration,
})),
inner: None,
task: AtomicTask::new(),