|
| 1 | +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +#![unstable(feature = "futures_api", |
| 12 | + reason = "futures in libcore are unstable", |
| 13 | + issue = "50547")] |
| 14 | + |
| 15 | +/// Indicates whether a value is available or if the current task has been |
| 16 | +/// scheduled to receive a wakeup instead. |
| 17 | +#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 18 | +pub enum Poll<T> { |
| 19 | + /// Represents that a value is immediately ready. |
| 20 | + Ready(T), |
| 21 | + |
| 22 | + /// Represents that a value is not ready yet. |
| 23 | + /// |
| 24 | + /// When a function returns `Pending`, the function *must* also |
| 25 | + /// ensure that the current task is scheduled to be awoken when |
| 26 | + /// progress can be made. |
| 27 | + Pending, |
| 28 | +} |
| 29 | + |
| 30 | +impl<T> Poll<T> { |
| 31 | + /// Change the ready value of this `Poll` with the closure provided |
| 32 | + pub fn map<U, F>(self, f: F) -> Poll<U> |
| 33 | + where F: FnOnce(T) -> U |
| 34 | + { |
| 35 | + match self { |
| 36 | + Poll::Ready(t) => Poll::Ready(f(t)), |
| 37 | + Poll::Pending => Poll::Pending, |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /// Returns whether this is `Poll::Ready` |
| 42 | + pub fn is_ready(&self) -> bool { |
| 43 | + match *self { |
| 44 | + Poll::Ready(_) => true, |
| 45 | + Poll::Pending => false, |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + /// Returns whether this is `Poll::Pending` |
| 50 | + pub fn is_pending(&self) -> bool { |
| 51 | + !self.is_ready() |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl<T, E> Poll<Result<T, E>> { |
| 56 | + /// Change the success value of this `Poll` with the closure provided |
| 57 | + pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>> |
| 58 | + where F: FnOnce(T) -> U |
| 59 | + { |
| 60 | + match self { |
| 61 | + Poll::Ready(Ok(t)) => Poll::Ready(Ok(f(t))), |
| 62 | + Poll::Ready(Err(e)) => Poll::Ready(Err(e)), |
| 63 | + Poll::Pending => Poll::Pending, |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /// Change the error value of this `Poll` with the closure provided |
| 68 | + pub fn map_err<U, F>(self, f: F) -> Poll<Result<T, U>> |
| 69 | + where F: FnOnce(E) -> U |
| 70 | + { |
| 71 | + match self { |
| 72 | + Poll::Ready(Ok(t)) => Poll::Ready(Ok(t)), |
| 73 | + Poll::Ready(Err(e)) => Poll::Ready(Err(f(e))), |
| 74 | + Poll::Pending => Poll::Pending, |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +impl<T> From<T> for Poll<T> { |
| 80 | + fn from(t: T) -> Poll<T> { |
| 81 | + Poll::Ready(t) |
| 82 | + } |
| 83 | +} |
0 commit comments