@@ -26,7 +26,21 @@ pub enum Poll<T> {
26
26
}
27
27
28
28
impl < T > Poll < T > {
29
- /// Changes the ready value of this `Poll` with the closure provided.
29
+ /// Maps a `Poll<T>` to `Poll<U>` by applying a function to a contained value.
30
+ ///
31
+ /// # Examples
32
+ ///
33
+ /// Converts a `Poll<`[`String`]`>` into an `Poll<`[`usize`]`>`, consuming the original:
34
+ ///
35
+ /// [`String`]: ../../std/string/struct.String.html
36
+ /// ```
37
+ /// # use core::task::Poll;
38
+ /// let poll_some_string = Poll::Ready(String::from("Hello, World!"));
39
+ /// // `Poll::map` takes self *by value*, consuming `poll_some_string`
40
+ /// let poll_some_len = poll_some_string.map(|s| s.len());
41
+ ///
42
+ /// assert_eq!(poll_some_len, Poll::Ready(13));
43
+ /// ```
30
44
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
31
45
pub fn map < U , F > ( self , f : F ) -> Poll < U >
32
46
where
@@ -38,15 +52,39 @@ impl<T> Poll<T> {
38
52
}
39
53
}
40
54
41
- /// Returns `true` if this is `Poll::Ready`
55
+ /// Returns `true` if the poll is a [`Poll::Ready`] value.
56
+ ///
57
+ /// # Examples
58
+ ///
59
+ /// ```
60
+ /// # use core::task::Poll;
61
+ /// let x: Poll<u32> = Poll::Ready(2);
62
+ /// assert_eq!(x.is_ready(), true);
63
+ ///
64
+ /// let x: Poll<u32> = Poll::Pending;
65
+ /// assert_eq!(x.is_ready(), false);
66
+ /// ```
42
67
#[ inline]
43
68
#[ rustc_const_stable( feature = "const_poll" , since = "1.49.0" ) ]
44
69
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
45
70
pub const fn is_ready ( & self ) -> bool {
46
71
matches ! ( * self , Poll :: Ready ( _) )
47
72
}
48
73
49
- /// Returns `true` if this is `Poll::Pending`
74
+ /// Returns `true` if the poll is a [`Pending`] value.
75
+ ///
76
+ /// [`Pending`]: Poll::Pending
77
+ ///
78
+ /// # Examples
79
+ ///
80
+ /// ```
81
+ /// # use core::task::Poll;
82
+ /// let x: Poll<u32> = Poll::Ready(2);
83
+ /// assert_eq!(x.is_pending(), false);
84
+ ///
85
+ /// let x: Poll<u32> = Poll::Pending;
86
+ /// assert_eq!(x.is_pending(), true);
87
+ /// ```
50
88
#[ inline]
51
89
#[ rustc_const_stable( feature = "const_poll" , since = "1.49.0" ) ]
52
90
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
@@ -56,7 +94,20 @@ impl<T> Poll<T> {
56
94
}
57
95
58
96
impl < T , E > Poll < Result < T , E > > {
59
- /// Changes the success value of this `Poll` with the closure provided.
97
+ /// Maps a `Poll<Result<T, E>>` to `Poll<Result<U, E>>` by applying a
98
+ /// function to a contained `Poll::Ready(Ok)` value, leaving all other
99
+ /// variants untouched.
100
+ ///
101
+ /// This function can be used to compose the results of two functions.
102
+ ///
103
+ /// # Examples
104
+ ///
105
+ /// ```
106
+ /// # use core::task::Poll;
107
+ /// let res: Poll<Result<u8, _>> = Poll::Ready("12".parse());
108
+ /// let squared = res.map_ok(|n| n * n);
109
+ /// assert_eq!(squared, Poll::Ready(Ok(144)));
110
+ /// ```
60
111
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
61
112
pub fn map_ok < U , F > ( self , f : F ) -> Poll < Result < U , E > >
62
113
where
@@ -69,7 +120,21 @@ impl<T, E> Poll<Result<T, E>> {
69
120
}
70
121
}
71
122
72
- /// Changes the error value of this `Poll` with the closure provided.
123
+ /// Maps a `Poll::Ready<Result<T, E>>` to `Poll::Ready<Result<T, F>>` by
124
+ /// applying a function to a contained `Poll::Ready(Err)` value, leaving all other
125
+ /// variants untouched.
126
+ ///
127
+ /// This function can be used to pass through a successful result while handling
128
+ /// an error.
129
+ ///
130
+ /// # Examples
131
+ ///
132
+ /// ```
133
+ /// # use core::task::Poll;
134
+ /// let res: Poll<Result<u8, _>> = Poll::Ready("oops".parse());
135
+ /// let res = res.map_err(|_| 0_u8);
136
+ /// assert_eq!(res, Poll::Ready(Err(0)));
137
+ /// ```
73
138
#[ stable( feature = "futures_api" , since = "1.36.0" ) ]
74
139
pub fn map_err < U , F > ( self , f : F ) -> Poll < Result < T , U > >
75
140
where
@@ -84,7 +149,20 @@ impl<T, E> Poll<Result<T, E>> {
84
149
}
85
150
86
151
impl < T , E > Poll < Option < Result < T , E > > > {
87
- /// Changes the success value of this `Poll` with the closure provided.
152
+ /// Maps a `Poll<Option<Result<T, E>>>` to `Poll<Option<Result<U, E>>>` by
153
+ /// applying a function to a contained `Poll::Ready(Some(Ok))` value,
154
+ /// leaving all other variants untouched.
155
+ ///
156
+ /// This function can be used to compose the results of two functions.
157
+ ///
158
+ /// # Examples
159
+ ///
160
+ /// ```
161
+ /// # use core::task::Poll;
162
+ /// let res: Poll<Option<Result<u8, _>>> = Poll::Ready(Some("12".parse()));
163
+ /// let squared = res.map_ok(|n| n * n);
164
+ /// assert_eq!(squared, Poll::Ready(Some(Ok(144))));
165
+ /// ```
88
166
#[ stable( feature = "poll_map" , since = "1.51.0" ) ]
89
167
pub fn map_ok < U , F > ( self , f : F ) -> Poll < Option < Result < U , E > > >
90
168
where
@@ -98,7 +176,22 @@ impl<T, E> Poll<Option<Result<T, E>>> {
98
176
}
99
177
}
100
178
101
- /// Changes the error value of this `Poll` with the closure provided.
179
+ /// Maps a `Poll::Ready<Option<Result<T, E>>>` to
180
+ /// `Poll::Ready<Option<Result<T, F>>>` by applying a function to a
181
+ /// contained `Poll::Ready(Some(Err))` value, leaving all other variants
182
+ /// untouched.
183
+ ///
184
+ /// This function can be used to pass through a successful result while handling
185
+ /// an error.
186
+ ///
187
+ /// # Examples
188
+ ///
189
+ /// ```
190
+ /// # use core::task::Poll;
191
+ /// let res: Poll<Option<Result<u8, _>>> = Poll::Ready(Some("oops".parse()));
192
+ /// let res = res.map_err(|_| 0_u8);
193
+ /// assert_eq!(res, Poll::Ready(Some(Err(0))));
194
+ /// ```
102
195
#[ stable( feature = "poll_map" , since = "1.51.0" ) ]
103
196
pub fn map_err < U , F > ( self , f : F ) -> Poll < Option < Result < T , U > > >
104
197
where
0 commit comments