1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
extern crate futures;
extern crate tokio_timer;
use std::error::Error;
use std::fmt;
use std::any::Any;
use std::time::Instant;
use futures::Future;
mod with_value;
mod with_cancel;
mod with_deadline;
pub use with_value::{WithValue, with_value};
pub use with_cancel::{WithCancel, with_cancel};
pub use with_deadline::{WithDeadline, with_deadline, with_timeout};
#[derive(Debug, PartialEq)]
pub enum ContextError {
Canceled,
DeadlineExceeded,
DeadlineTooLong,
}
impl fmt::Display for ContextError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ContextError: {}", self.description())
}
}
impl Error for ContextError {
fn description(&self) -> &str {
match *self {
ContextError::Canceled => "context has been canceled",
ContextError::DeadlineExceeded => "deadline has been exceeded",
ContextError::DeadlineTooLong => "requested deadline too long",
}
}
}
pub trait Context: Future<Item = (), Error = ContextError> {
fn deadline(&self) -> Option<Instant> {
None
}
fn value<T>(&self) -> Option<&T>
where T: Any
{
None
}
}
mod background {
use {Context, ContextError};
use futures::{Future, Poll, Async};
pub struct Background {}
impl Context for Background {}
impl Future for Background {
type Item = ();
type Error = ContextError;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
Ok(Async::NotReady)
}
}
pub const BACKGROUND: Background = Background {};
}
pub fn background() -> background::Background {
background::BACKGROUND
}