|
| 1 | +use core::convert; |
| 2 | +use core::fmt; |
| 3 | +use core::ops::{ControlFlow, FromResidual, Try}; |
| 4 | +use core::task::Poll; |
| 5 | + |
1 | 6 | /// Extracts the successful type of a [`Poll<T>`].
|
2 | 7 | ///
|
3 | 8 | /// This macro bakes in propagation of [`Pending`] signals by returning early.
|
@@ -55,3 +60,55 @@ pub macro ready($e:expr) {
|
55 | 60 | }
|
56 | 61 | }
|
57 | 62 | }
|
| 63 | + |
| 64 | +/// Extracts the successful type of a [`Poll<T>`]. |
| 65 | +/// |
| 66 | +/// See [`Poll::ready`] for details. |
| 67 | +#[unstable(feature = "poll_ready", issue = "none")] |
| 68 | +pub struct Ready<T>(pub(crate) Poll<T>); |
| 69 | + |
| 70 | +#[unstable(feature = "poll_ready", issue = "none")] |
| 71 | +impl<T> Try for Ready<T> { |
| 72 | + type Output = T; |
| 73 | + type Residual = Ready<convert::Infallible>; |
| 74 | + |
| 75 | + #[inline] |
| 76 | + fn from_output(output: Self::Output) -> Self { |
| 77 | + Ready(Poll::Ready(output)) |
| 78 | + } |
| 79 | + |
| 80 | + #[inline] |
| 81 | + fn branch(self) -> ControlFlow<Self::Residual, Self::Output> { |
| 82 | + match self.0 { |
| 83 | + Poll::Ready(v) => ControlFlow::Continue(v), |
| 84 | + Poll::Pending => ControlFlow::Break(Ready(Poll::Pending)), |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[unstable(feature = "poll_ready", issue = "none")] |
| 90 | +impl<T> FromResidual for Ready<T> { |
| 91 | + #[inline] |
| 92 | + fn from_residual(residual: Ready<convert::Infallible>) -> Self { |
| 93 | + match residual.0 { |
| 94 | + Poll::Pending => Ready(Poll::Pending), |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[unstable(feature = "poll_ready", issue = "none")] |
| 100 | +impl<T> FromResidual<Ready<convert::Infallible>> for Poll<T> { |
| 101 | + #[inline] |
| 102 | + fn from_residual(residual: Ready<convert::Infallible>) -> Self { |
| 103 | + match residual.0 { |
| 104 | + Poll::Pending => Poll::Pending, |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +#[unstable(feature = "poll_ready", issue = "none")] |
| 110 | +impl<T> fmt::Debug for Ready<T> { |
| 111 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 112 | + f.debug_tuple("Ready").finish() |
| 113 | + } |
| 114 | +} |
0 commit comments