diff options
| author | tinaun <tinagma@gmail.com> | 2018-06-08 23:24:52 -0400 |
|---|---|---|
| committer | tinaun <tinagma@gmail.com> | 2018-06-09 00:38:11 -0400 |
| commit | fb507cadf32e1eacccc07c1c3511636fd6378f7b (patch) | |
| tree | 845ac44091cd890ef5b452797fccb243d96aac18 | |
| parent | 49eb754cc0108d8546eae70cdcebf81aaddbece3 (diff) | |
| download | rust-fb507cadf32e1eacccc07c1c3511636fd6378f7b.tar.gz rust-fb507cadf32e1eacccc07c1c3511636fd6378f7b.zip | |
add inherent methods to Poll
| -rw-r--r-- | src/libcore/task.rs | 49 | ||||
| -rw-r--r-- | src/libstd/panic.rs | 2 |
2 files changed, 50 insertions, 1 deletions
diff --git a/src/libcore/task.rs b/src/libcore/task.rs index ab1c1da5790..bef6d3677d0 100644 --- a/src/libcore/task.rs +++ b/src/libcore/task.rs @@ -32,6 +32,55 @@ pub enum Poll<T> { Pending, } +impl<T> Poll<T> { + /// Change the ready value of this `Poll` with the closure provided + pub fn map<U, F>(self, f: F) -> Poll<U> + where F: FnOnce(T) -> U + { + match self { + Poll::Ready(t) => Poll::Ready(f(t)), + Poll::Pending => Poll::Pending, + } + } + + /// Returns whether this is `Poll::Ready` + pub fn is_ready(&self) -> bool { + match *self { + Poll::Ready(_) => true, + Poll::Pending => false, + } + } + + /// Returns whether this is `Poll::Pending` + pub fn is_pending(&self) -> bool { + !self.is_ready() + } +} + +impl<T, E> Poll<Result<T, E>> { + /// Change the success value of this `Poll` with the closure provided + pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> + where F: FnOnce(T) -> U + { + match self { + Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))), + Poll::Ready(Err(e)) => Poll::Ready(Err(e)), + Poll::Pending => Poll::Pending, + } + } + + /// Change the error value of this `Poll` with the closure provided + pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>> + where F: FnOnce(E) -> U + { + match self { + Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)), + Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))), + Poll::Pending => Poll::Pending, + } + } +} + impl<T> From<T> for Poll<T> { fn from(t: T) -> Poll<T> { Poll::Ready(t) diff --git a/src/libstd/panic.rs b/src/libstd/panic.rs index 8aee15b5eda..4b5a063ea73 100644 --- a/src/libstd/panic.rs +++ b/src/libstd/panic.rs @@ -327,7 +327,7 @@ impl<'a, F: Future> Future for AssertUnwindSafe<F> { let pinned_field = PinMut::new_unchecked( &mut PinMut::get_mut(self.reborrow()).0 ); - + pinned_field.poll(cx) } } |
