1 /// A macro for extracting the successful type of a `Poll<T, E>`.
2 ///
3 /// This macro bakes propagation of both errors and `NotReady` signals by
4 /// returning early.
5 #[macro_export]
6 macro_rules! try_ready {
7     ($e:expr) => (match $e {
8         Ok($crate::Async::Ready(t)) => t,
9         Ok($crate::Async::NotReady) => return Ok($crate::Async::NotReady),
10         Err(e) => return Err(From::from(e)),
11     })
12 }
13 
14 /// Return type of the `Future::poll` method, indicates whether a future's value
15 /// is ready or not.
16 ///
17 /// * `Ok(Async::Ready(t))` means that a future has successfully resolved
18 /// * `Ok(Async::NotReady)` means that a future is not ready to complete yet
19 /// * `Err(e)` means that a future has completed with the given failure
20 pub type Poll<T, E> = Result<Async<T>, E>;
21 
22 /// Return type of future, indicating whether a value is ready or not.
23 #[derive(Copy, Clone, Debug, PartialEq)]
24 pub enum Async<T> {
25     /// Represents that a value is immediately ready.
26     Ready(T),
27 
28     /// Represents that a value is not ready yet, but may be so later.
29     NotReady,
30 }
31 
32 impl<T> Async<T> {
33     /// Change the success value of this `Async` with the closure provided
map<F, U>(self, f: F) -> Async<U> where F: FnOnce(T) -> U34     pub fn map<F, U>(self, f: F) -> Async<U>
35         where F: FnOnce(T) -> U
36     {
37         match self {
38             Async::Ready(t) => Async::Ready(f(t)),
39             Async::NotReady => Async::NotReady,
40         }
41     }
42 
43     /// Returns whether this is `Async::Ready`
is_ready(&self) -> bool44     pub fn is_ready(&self) -> bool {
45         match *self {
46             Async::Ready(_) => true,
47             Async::NotReady => false,
48         }
49     }
50 
51     /// Returns whether this is `Async::NotReady`
is_not_ready(&self) -> bool52     pub fn is_not_ready(&self) -> bool {
53         !self.is_ready()
54     }
55 }
56 
57 impl<T> From<T> for Async<T> {
from(t: T) -> Async<T>58     fn from(t: T) -> Async<T> {
59         Async::Ready(t)
60     }
61 }
62 
63 /// The result of an asynchronous attempt to send a value to a sink.
64 #[derive(Copy, Clone, Debug, PartialEq)]
65 pub enum AsyncSink<T> {
66     /// The `start_send` attempt succeeded, so the sending process has
67     /// *started*; you must use `Sink::poll_complete` to drive the send
68     /// to completion.
69     Ready,
70 
71     /// The `start_send` attempt failed due to the sink being full. The value
72     /// being sent is returned, and the current `Task` will be automatically
73     /// notified again once the sink has room.
74     NotReady(T),
75 }
76 
77 impl<T> AsyncSink<T> {
78     /// Change the NotReady value of this `AsyncSink` with the closure provided
map<F, U>(self, f: F) -> AsyncSink<U> where F: FnOnce(T) -> U,79     pub fn map<F, U>(self, f: F) -> AsyncSink<U>
80         where F: FnOnce(T) -> U,
81     {
82         match self {
83             AsyncSink::Ready => AsyncSink::Ready,
84             AsyncSink::NotReady(t) => AsyncSink::NotReady(f(t)),
85         }
86     }
87 
88     /// Returns whether this is `AsyncSink::Ready`
is_ready(&self) -> bool89     pub fn is_ready(&self) -> bool {
90         match *self {
91             AsyncSink::Ready => true,
92             AsyncSink::NotReady(_) => false,
93         }
94     }
95 
96     /// Returns whether this is `AsyncSink::NotReady`
is_not_ready(&self) -> bool97     pub fn is_not_ready(&self) -> bool {
98         !self.is_ready()
99     }
100 }
101 
102 
103 /// Return type of the `Sink::start_send` method, indicating the outcome of a
104 /// send attempt. See `AsyncSink` for more details.
105 pub type StartSend<T, E> = Result<AsyncSink<T>, E>;
106