futures_core/
poll.rs

1/// A macro for extracting the successful type of a `Poll<T, E>`.
2///
3/// This macro bakes in propagation of *both* errors and `Pending` signals by
4/// returning early.
5#[macro_export]
6macro_rules! try_ready {
7    ($e:expr) => (match $e {
8        Ok($crate::Async::Ready(t)) => t,
9        Ok($crate::Async::Pending) => return Ok($crate::Async::Pending),
10        Err(e) => return Err(From::from(e)),
11    })
12}
13
14/// A convenience wrapper for `Result<Async<T>, E>`.
15///
16/// `Poll` is the return type of the `poll` method on the `Future` trait.
17///
18/// * `Ok(Async::Ready(t))` means the future has successfully resolved.
19/// * `Ok(Async::Pending)` means the future is not able to fully resolve yet.
20///    The current task will be awoken when the future can make further
21///    progress.
22/// * `Err(e)` means that an error was encountered when attempting to complete
23///    the future. `Future`s which have returned errors are complete, and
24///    should not be polled again. However,`Stream`s that have returned errors
25///    may not be complete and should still be polled.
26pub type Poll<T, E> = Result<Async<T>, E>;
27
28/// Indicates whether a value is available, or if the current task has been
29/// scheduled for later wake-up instead.
30#[derive(Copy, Clone, Debug, PartialEq)]
31pub enum Async<T> {
32    /// Represents that a value is immediately ready.
33    Ready(T),
34
35    /// Represents that a value is not ready yet.
36    ///
37    /// When a function returns `Pending`, the function *must* also
38    /// ensure that the current task is scheduled to be awoken when
39    /// progress can be made.
40    Pending,
41}
42
43impl<T> Async<T> {
44    /// Change the success value of this `Async` with the closure provided
45    pub fn map<U, F>(self, f: F) -> Async<U>
46        where F: FnOnce(T) -> U
47    {
48        match self {
49            Async::Ready(t) => Async::Ready(f(t)),
50            Async::Pending => Async::Pending,
51        }
52    }
53
54    /// Returns whether this is `Async::Ready`
55    pub fn is_ready(&self) -> bool {
56        match *self {
57            Async::Ready(_) => true,
58            Async::Pending => false,
59        }
60    }
61
62    /// Returns whether this is `Async::Pending`
63    pub fn is_pending(&self) -> bool {
64        !self.is_ready()
65    }
66}
67
68impl<T> From<T> for Async<T> {
69    fn from(t: T) -> Async<T> {
70        Async::Ready(t)
71    }
72}