diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-08-14 04:18:54 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-08-14 04:18:54 +0200 |
| commit | d5dd097ad38c327eadaf004d6ca2f03cc6b5a7a8 (patch) | |
| tree | bc0138e7285f92c29709a10cc0e84be4d67f0191 | |
| parent | 051598b29e944530e46ca9d40b40dd6ea36a0c90 (diff) | |
| parent | 84cab928db8526af7c42e1637e7253a009da215d (diff) | |
| download | rust-d5dd097ad38c327eadaf004d6ca2f03cc6b5a7a8.tar.gz rust-d5dd097ad38c327eadaf004d6ca2f03cc6b5a7a8.zip | |
Rollup merge of #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.
| -rw-r--r-- | src/libcore/task/poll.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/libcore/task/poll.rs b/src/libcore/task/poll.rs index 3db70d5e764..fec17c4d1a4 100644 --- a/src/libcore/task/poll.rs +++ b/src/libcore/task/poll.rs @@ -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> { |
