Skip to content

Commit d01acf7

Browse files
authored
Rollup merge of rust-lang#63512 - 95th:master, r=cramertj
Provide map_ok and map_err method for Poll<Option<Result<T, E>>> Currently `map_ok` and `map_err` methods are given for `Poll<Result<T, E>>`. This PR adds these methods for `Poll<Option<Result<T, E>>>` as they are helpful in stream building code.
2 parents 6651373 + 84cab92 commit d01acf7

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/libcore/task/poll.rs

+28
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,34 @@ impl<T, E> Poll<Result<T, E>> {
8181
}
8282
}
8383

84+
impl<T, E> Poll<Option<Result<T, E>>> {
85+
/// Changes the success value of this `Poll` with the closure provided.
86+
#[unstable(feature = "poll_map", issue = "63514")]
87+
pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
88+
where F: FnOnce(T) -> U
89+
{
90+
match self {
91+
Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
92+
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
93+
Poll::Ready(None) => Poll::Ready(None),
94+
Poll::Pending => Poll::Pending,
95+
}
96+
}
97+
98+
/// Changes the error value of this `Poll` with the closure provided.
99+
#[unstable(feature = "poll_map", issue = "63514")]
100+
pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
101+
where F: FnOnce(E) -> U
102+
{
103+
match self {
104+
Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
105+
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
106+
Poll::Ready(None) => Poll::Ready(None),
107+
Poll::Pending => Poll::Pending,
108+
}
109+
}
110+
}
111+
84112
#[stable(feature = "futures_api", since = "1.36.0")]
85113
impl<T> From<T> for Poll<T> {
86114
fn from(t: T) -> Poll<T> {

0 commit comments

Comments
 (0)