diff options
| author | Taylor Cramer <cramertj@google.com> | 2018-07-25 17:10:05 -0700 |
|---|---|---|
| committer | Taylor Cramer <cramertj@google.com> | 2018-07-25 17:26:12 -0700 |
| commit | bce8a91f34e63a4ed87a6d6351bf67a82306afbd (patch) | |
| tree | d861034f58c0a2f1b6e22d005ed777f6fb7c50c4 /src/libcore/task | |
| parent | fefe81605d6111faa8dbb3635ab2c51d59de740a (diff) | |
| download | rust-bce8a91f34e63a4ed87a6d6351bf67a82306afbd.tar.gz rust-bce8a91f34e63a4ed87a6d6351bf67a82306afbd.zip | |
std::ops::Try impl for std::task::Poll
Diffstat (limited to 'src/libcore/task')
| -rw-r--r-- | src/libcore/task/poll.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/libcore/task/poll.rs b/src/libcore/task/poll.rs index 10c954f0e80..fb027efc6dc 100644 --- a/src/libcore/task/poll.rs +++ b/src/libcore/task/poll.rs @@ -12,6 +12,9 @@ reason = "futures in libcore are unstable", issue = "50547")] +use ops::Try; +use result::Result; + /// Indicates whether a value is available or if the current task has been /// scheduled to receive a wakeup instead. #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] @@ -39,6 +42,7 @@ impl<T> Poll<T> { } /// Returns whether this is `Poll::Ready` + #[inline] pub fn is_ready(&self) -> bool { match *self { Poll::Ready(_) => true, @@ -47,6 +51,7 @@ impl<T> Poll<T> { } /// Returns whether this is `Poll::Pending` + #[inline] pub fn is_pending(&self) -> bool { !self.is_ready() } @@ -81,3 +86,52 @@ impl<T> From<T> for Poll<T> { Poll::Ready(t) } } + +impl<T, E> Try for Poll<Result<T, E>> { + type Ok = Poll<T>; + type Error = E; + + #[inline] + fn into_result(self) -> Result<Self::Ok, Self::Error> { + match self { + Poll::Ready(Ok(x)) => Ok(Poll::Ready(x)), + Poll::Ready(Err(e)) => Err(e), + Poll::Pending => Ok(Poll::Pending), + } + } + + #[inline] + fn from_error(e: Self::Error) -> Self { + Poll::Ready(Err(e)) + } + + #[inline] + fn from_ok(x: Self::Ok) -> Self { + x.map(Ok) + } +} + +impl<T, E> Try for Poll<Option<Result<T, E>>> { + type Ok = Poll<Option<T>>; + type Error = E; + + #[inline] + fn into_result(self) -> Result<Self::Ok, Self::Error> { + match self { + Poll::Ready(Some(Ok(x))) => Ok(Poll::Ready(Some(x))), + Poll::Ready(Some(Err(e))) => Err(e), + Poll::Ready(None) => Ok(Poll::Ready(None)), + Poll::Pending => Ok(Poll::Pending), + } + } + + #[inline] + fn from_error(e: Self::Error) -> Self { + Poll::Ready(Some(Err(e))) + } + + #[inline] + fn from_ok(x: Self::Ok) -> Self { + x.map(|x| x.map(Ok)) + } +} |
