#![stable(feature = "futures_api", since = "1.36.0")] use crate::ops::Try; use crate::result::Result; /// Indicates whether a value is available or if the current task has been /// scheduled to receive a wakeup instead. #[must_use = "this `Poll` may be a `Pending` variant, which should be handled"] #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] #[stable(feature = "futures_api", since = "1.36.0")] pub enum Poll { /// Represents that a value is immediately ready. #[stable(feature = "futures_api", since = "1.36.0")] Ready(#[stable(feature = "futures_api", since = "1.36.0")] T), /// Represents that a value is not ready yet. /// /// When a function returns `Pending`, the function *must* also /// ensure that the current task is scheduled to be awoken when /// progress can be made. #[stable(feature = "futures_api", since = "1.36.0")] Pending, } impl Poll { /// Changes the ready value of this `Poll` with the closure provided. #[stable(feature = "futures_api", since = "1.36.0")] pub fn map(self, f: F) -> Poll where F: FnOnce(T) -> U, { match self { Poll::Ready(t) => Poll::Ready(f(t)), Poll::Pending => Poll::Pending, } } /// Returns `true` if this is `Poll::Ready` #[inline] #[stable(feature = "futures_api", since = "1.36.0")] pub fn is_ready(&self) -> bool { matches!(*self, Poll::Ready(_)) } /// Returns `true` if this is `Poll::Pending` #[inline] #[stable(feature = "futures_api", since = "1.36.0")] pub fn is_pending(&self) -> bool { !self.is_ready() } } impl Poll> { /// Changes the success value of this `Poll` with the closure provided. #[stable(feature = "futures_api", since = "1.36.0")] pub fn map_ok(self, f: F) -> Poll> 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, } } /// Changes the error value of this `Poll` with the closure provided. #[stable(feature = "futures_api", since = "1.36.0")] pub fn map_err(self, f: F) -> Poll> 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 Poll>> { /// Changes the success value of this `Poll` with the closure provided. #[unstable(feature = "poll_map", issue = "63514")] pub fn map_ok(self, f: F) -> Poll>> where F: FnOnce(T) -> U, { match self { Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(f(t)))), Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(e))), Poll::Ready(None) => Poll::Ready(None), Poll::Pending => Poll::Pending, } } /// Changes the error value of this `Poll` with the closure provided. #[unstable(feature = "poll_map", issue = "63514")] pub fn map_err(self, f: F) -> Poll>> where F: FnOnce(E) -> U, { match self { Poll::Ready(Some(Ok(t))) => Poll::Ready(Some(Ok(t))), Poll::Ready(Some(Err(e))) => Poll::Ready(Some(Err(f(e)))), Poll::Ready(None) => Poll::Ready(None), Poll::Pending => Poll::Pending, } } } #[stable(feature = "futures_api", since = "1.36.0")] impl From for Poll { fn from(t: T) -> Poll { Poll::Ready(t) } } #[stable(feature = "futures_api", since = "1.36.0")] impl Try for Poll> { type Ok = Poll; type Error = E; #[inline] fn into_result(self) -> Result { 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) } } #[stable(feature = "futures_api", since = "1.36.0")] impl Try for Poll>> { type Ok = Poll>; type Error = E; #[inline] fn into_result(self) -> Result { 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)) } }