]> git.lizzy.rs Git - rust.git/blob - src/libcore/task/poll.rs
Fix rebase fail
[rust.git] / src / libcore / task / poll.rs
1 #![unstable(feature = "futures_api",
2             reason = "futures in libcore are unstable",
3             issue = "50547")]
4
5 use ops::Try;
6 use result::Result;
7
8 /// Indicates whether a value is available or if the current task has been
9 /// scheduled to receive a wakeup instead.
10 #[must_use = "this `Poll` may be a `Pending` variant, which should be handled"]
11 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
12 pub enum Poll<T> {
13     /// Represents that a value is immediately ready.
14     Ready(T),
15
16     /// Represents that a value is not ready yet.
17     ///
18     /// When a function returns `Pending`, the function *must* also
19     /// ensure that the current task is scheduled to be awoken when
20     /// progress can be made.
21     Pending,
22 }
23
24 impl<T> Poll<T> {
25     /// Change the ready value of this `Poll` with the closure provided
26     pub fn map<U, F>(self, f: F) -> Poll<U>
27         where F: FnOnce(T) -> U
28     {
29         match self {
30             Poll::Ready(t) => Poll::Ready(f(t)),
31             Poll::Pending => Poll::Pending,
32         }
33     }
34
35     /// Returns whether this is `Poll::Ready`
36     #[inline]
37     pub fn is_ready(&self) -> bool {
38         match *self {
39             Poll::Ready(_) => true,
40             Poll::Pending => false,
41         }
42     }
43
44     /// Returns whether this is `Poll::Pending`
45     #[inline]
46     pub fn is_pending(&self) -> bool {
47         !self.is_ready()
48     }
49 }
50
51 impl<T, E> Poll<Result<T, E>> {
52     /// Change the success value of this `Poll` with the closure provided
53     pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
54         where F: FnOnce(T) -> U
55     {
56         match self {
57             Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))),
58             Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
59             Poll::Pending => Poll::Pending,
60         }
61     }
62
63     /// Change the error value of this `Poll` with the closure provided
64     pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
65         where F: FnOnce(E) -> U
66     {
67         match self {
68             Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)),
69             Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))),
70             Poll::Pending => Poll::Pending,
71         }
72     }
73 }
74
75 impl<T> From<T> for Poll<T> {
76     fn from(t: T) -> Poll<T> {
77         Poll::Ready(t)
78     }
79 }
80
81 impl<T, E> Try for Poll<Result<T, E>> {
82     type Ok = Poll<T>;
83     type Error = E;
84
85     #[inline]
86     fn into_result(self) -> Result<Self::Ok, Self::Error> {
87         match self {
88             Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)),
89             Poll::Ready(Err(e)) => Err(e),
90             Poll::Pending => Ok(Poll::Pending),
91         }
92     }
93
94     #[inline]
95     fn from_error(e: Self::Error) -> Self {
96         Poll::Ready(Err(e))
97     }
98
99     #[inline]
100     fn from_ok(x: Self::Ok) -> Self {
101         x.map(Ok)
102     }
103 }
104
105 impl<T, E> Try for Poll<Option<Result<T, E>>> {
106     type Ok = Poll<Option<T>>;
107     type Error = E;
108
109     #[inline]
110     fn into_result(self) -> Result<Self::Ok, Self::Error> {
111         match self {
112             Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))),
113             Poll::Ready(Some(Err(e))) => Err(e),
114             Poll::Ready(None) => Ok(Poll::Ready(None)),
115             Poll::Pending => Ok(Poll::Pending),
116         }
117     }
118
119     #[inline]
120     fn from_error(e: Self::Error) -> Self {
121         Poll::Ready(Some(Err(e)))
122     }
123
124     #[inline]
125     fn from_ok(x: Self::Ok) -> Self {
126         x.map(|x| x.map(Ok))
127     }
128 }