Skip to content

Commit b3d11f9

Browse files
committed
Auto merge of #86598 - yoshuawuyts:poll-method-docs, r=JohnTitor
Add examples to the various methods of `core::task::Poll` This improves the documentation of the various methods of [`core::task::Poll`](https://doc.rust-lang.org/std/task/enum.Poll.html). These currently have fairly simple docs with no examples. This PR changes these methods to be closer to `core::option::Option` and adds usage examples (and importantly: tests!) to `Poll`'s methods. cc/ `@rust-lang/wg-async-foundations` ## Screenshots <details> <summary>View generated rustdoc page</summary> <image src="https://user-images.githubusercontent.com/2467194/123286616-59ee9b00-d50e-11eb-9e02-40269070f904.png" alt="Poll in core::task"></details>
2 parents 23c652d + a06ee82 commit b3d11f9

File tree

1 file changed

+100
-7
lines changed

1 file changed

+100
-7
lines changed

library/core/src/task/poll.rs

+100-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,21 @@ pub enum Poll<T> {
2626
}
2727

2828
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+
/// ```
3044
#[stable(feature = "futures_api", since = "1.36.0")]
3145
pub fn map<U, F>(self, f: F) -> Poll<U>
3246
where
@@ -38,15 +52,39 @@ impl<T> Poll<T> {
3852
}
3953
}
4054

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+
/// ```
4267
#[inline]
4368
#[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
4469
#[stable(feature = "futures_api", since = "1.36.0")]
4570
pub const fn is_ready(&self) -> bool {
4671
matches!(*self, Poll::Ready(_))
4772
}
4873

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+
/// ```
5088
#[inline]
5189
#[rustc_const_stable(feature = "const_poll", since = "1.49.0")]
5290
#[stable(feature = "futures_api", since = "1.36.0")]
@@ -56,7 +94,20 @@ impl<T> Poll<T> {
5694
}
5795

5896
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+
/// ```
60111
#[stable(feature = "futures_api", since = "1.36.0")]
61112
pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
62113
where
@@ -69,7 +120,21 @@ impl<T, E> Poll<Result<T, E>> {
69120
}
70121
}
71122

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+
/// ```
73138
#[stable(feature = "futures_api", since = "1.36.0")]
74139
pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>>
75140
where
@@ -84,7 +149,20 @@ impl<T, E> Poll<Result<T, E>> {
84149
}
85150

86151
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+
/// ```
88166
#[stable(feature = "poll_map", since = "1.51.0")]
89167
pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
90168
where
@@ -98,7 +176,22 @@ impl<T, E> Poll<Option<Result<T, E>>> {
98176
}
99177
}
100178

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+
/// ```
102195
#[stable(feature = "poll_map", since = "1.51.0")]
103196
pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
104197
where

0 commit comments

Comments
 (0)