diff options
| author | Scott McMurray <scottmcm@users.noreply.github.com> | 2021-04-11 04:44:45 -0700 |
|---|---|---|
| committer | Scott McMurray <scottmcm@users.noreply.github.com> | 2021-04-17 11:58:18 -0700 |
| commit | 1864970430d967699142b3e610f818d2f03b496e (patch) | |
| tree | 0b5db0c12498cf70b8e18d77b1aab8ae20fe4373 /library/core/src/task | |
| parent | 361bfce305b8829eda7f3d133fe82a118685de1f (diff) | |
Add the try_trait_v2 library basics
No compiler changes as part of this -- just new unstable traits and impls thereof. The goal here is to add the things that aren't going to break anything, to keep the feature implementation simpler in the next PR.
Diffstat (limited to 'library/core/src/task')
| -rw-r--r-- | library/core/src/task/poll.rs | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/library/core/src/task/poll.rs b/library/core/src/task/poll.rs index 42c9d9f0cc0..2765c21a46d 100644 --- a/library/core/src/task/poll.rs +++ b/library/core/src/task/poll.rs @@ -1,6 +1,7 @@ #![stable(feature = "futures_api", since = "1.36.0")] -use crate::ops::Try; +use crate::convert; +use crate::ops::{self, ControlFlow, Try}; use crate::result::Result; /// Indicates whether a value is available or if the current task has been @@ -152,6 +153,36 @@ impl<T, E> Try for Poll<Result<T, E>> { } } +#[unstable(feature = "try_trait_v2", issue = "84277")] +impl<T, E> ops::TryV2 for Poll<Result<T, E>> { + type Output = Poll<T>; + type Residual = Result<convert::Infallible, E>; + + #[inline] + fn from_output(c: Self::Output) -> Self { + c.map(Ok) + } + + #[inline] + fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { + match self { + Poll::Ready(Ok(x)) => ControlFlow::Continue(Poll::Ready(x)), + Poll::Ready(Err(e)) => ControlFlow::Break(Err(e)), + Poll::Pending => ControlFlow::Continue(Poll::Pending), + } + } +} + +#[unstable(feature = "try_trait_v2", issue = "84277")] +impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> for Poll<Result<T, F>> { + #[inline] + fn from_residual(x: Result<convert::Infallible, E>) -> Self { + match x { + Err(e) => Poll::Ready(Err(From::from(e))), + } + } +} + #[stable(feature = "futures_api", since = "1.36.0")] impl<T, E> Try for Poll<Option<Result<T, E>>> { type Ok = Poll<Option<T>>; @@ -177,3 +208,36 @@ impl<T, E> Try for Poll<Option<Result<T, E>>> { x.map(|x| x.map(Ok)) } } + +#[unstable(feature = "try_trait_v2", issue = "84277")] +impl<T, E> ops::TryV2 for Poll<Option<Result<T, E>>> { + type Output = Poll<Option<T>>; + type Residual = Result<convert::Infallible, E>; + + #[inline] + fn from_output(c: Self::Output) -> Self { + c.map(|x| x.map(Ok)) + } + + #[inline] + fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { + match self { + Poll::Ready(Some(Ok(x))) => ControlFlow::Continue(Poll::Ready(Some(x))), + Poll::Ready(Some(Err(e))) => ControlFlow::Break(Err(e)), + Poll::Ready(None) => ControlFlow::Continue(Poll::Ready(None)), + Poll::Pending => ControlFlow::Continue(Poll::Pending), + } + } +} + +#[unstable(feature = "try_trait_v2", issue = "84277")] +impl<T, E, F: From<E>> ops::FromResidual<Result<convert::Infallible, E>> + for Poll<Option<Result<T, F>>> +{ + #[inline] + fn from_residual(x: Result<convert::Infallible, E>) -> Self { + match x { + Err(e) => Poll::Ready(Some(Err(From::from(e)))), + } + } +} |
