usesuper::assert_future; use core::mem; use core::pin::Pin; use futures_core::future::{FusedFuture, Future}; use futures_core::ready; use futures_core::task::{Context, Poll};
/// A future that may have completed. /// /// This is created by the [`maybe_done()`] function. #[derive(Debug)] pubenum MaybeDone<Fut: Future> { /// A not-yet-completed future
Future(/* #[pin] */ Fut), /// The output of the completed future
Done(Fut::Output), /// The empty variant after the result of a [`MaybeDone`] has been /// taken using the [`take_output`](MaybeDone::take_output) method.
Gone,
}
impl<Fut: Future + Unpin> Unpin for MaybeDone<Fut> {}
/// Wraps a future into a `MaybeDone` /// /// # Examples /// /// ``` /// # futures::executor::block_on(async { /// use futures::future; /// use futures::pin_mut; /// /// let future = future::maybe_done(async { 5 }); /// pin_mut!(future); /// assert_eq!(future.as_mut().take_output(), None); /// let () = future.as_mut().await; /// assert_eq!(future.as_mut().take_output(), Some(5)); /// assert_eq!(future.as_mut().take_output(), None); /// # }); /// ``` pubfn maybe_done<Fut: Future>(future: Fut) -> MaybeDone<Fut> {
assert_future::<(), _>(MaybeDone::Future(future))
}
impl<Fut: Future> MaybeDone<Fut> { /// Returns an [`Option`] containing a mutable reference to the output of the future. /// The output of this method will be [`Some`] if and only if the inner /// future has been completed and [`take_output`](MaybeDone::take_output) /// has not yet been called. #[inline] pubfn output_mut(self: Pin<&mutSelf>) -> Option<&mut Fut::Output> { unsafe { matchself.get_unchecked_mut() {
MaybeDone::Done(res) => Some(res),
_ => None,
}
}
}
/// Attempt to take the output of a `MaybeDone` without driving it /// towards completion. #[inline] pubfn take_output(self: Pin<&mutSelf>) -> Option<Fut::Output> { match &*self { Self::Done(_) => {} Self::Future(_) | Self::Gone => return None,
} unsafe { match mem::replace(self.get_unchecked_mut(), Self::Gone) {
MaybeDone::Done(output) => Some(output),
_ => unreachable!(),
}
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.