Skip to content

Commit

Permalink
Rollup merge of rust-lang#63512 - 95th:master, r=cramertj
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Centril authored Aug 14, 2019
2 parents 6651373 + 84cab92 commit d01acf7
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/libcore/task/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@ impl<T, E> Poll<Result<T, E>> {
}
}

impl<T, E> Poll<Option<Result<T, E>>> {
/// Changes the success value of this `Poll` with the closure provided.
#[unstable(feature = "poll_map", issue = "63514")]
pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
where F: FnOnce(T) -> U
{
match self {
Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))),
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}

/// Changes the error value of this `Poll` with the closure provided.
#[unstable(feature = "poll_map", issue = "63514")]
pub fn map_err<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
where F: FnOnce(E) -> U
{
match self {
Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))),
Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))),
Poll::Ready(None) => Poll::Ready(None),
Poll::Pending => Poll::Pending,
}
}
}

#[stable(feature = "futures_api", since = "1.36.0")]
impl<T> From<T> for Poll<T> {
fn from(t: T) -> Poll<T> {
Expand Down

0 comments on commit d01acf7

Please sign in to comment.