poll.rs.html -- source
Description: Source to the Rust file `library/core/src/task/poll.rs`.
Keywords: rust, rustlang, rust-lang

      1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169

    #![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<T> {
        /// Represents that a value is immediately ready.
        #[cfg_attr(not(bootstrap), lang = "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.
        #[cfg_attr(not(bootstrap), lang = "Pending")]
        #[stable(feature = "futures_api", since = "1.36.0")]
        Pending,
    }

    impl<T> Poll<T> {
        /// Changes the ready value of this `Poll` with the closure provided.
        #[stable(feature = "futures_api", since = "1.36.0")]
        pub fn map<U, F>(self, f: F) -> Poll<U>
        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<T, E> Poll<Result<T, E>> {
        /// Changes the success value of this `Poll` with the closure provided.
        #[stable(feature = "futures_api", since = "1.36.0")]
        pub fn map_ok<U, F>(self, f: F) -> Poll<Result<U, E>>
        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<U, F>(self, f: F) -> Poll<Result<T, U>>
        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<T, E> Poll<Option<Result<T, E>>> {
        /// Changes the success value of this `Poll` with the closure provided.
        #[unstable(feature = "poll_map", issue = "63514")]
        pub fn map_ok<U, F>(self, f: F) -> Poll<Option<Result<U, E>>>
        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<U, F>(self, f: F) -> Poll<Option<Result<T, U>>>
        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<T> From<T> for Poll<T> {
        fn from(t: T) -> Poll<T> {
            Poll::Ready(t)
        }
    }

    #[stable(feature = "futures_api", since = "1.36.0")]
    impl<T, E> Try for Poll<Result<T, E>> {
        type Ok = Poll<T>;
        type Error = E;

        #[inline]
        fn into_result(self) -> Result<Self::Ok, Self::Error> {
            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<T, E> Try for Poll<Option<Result<T, E>>> {
        type Ok = Poll<Option<T>>;
        type Error = E;

        #[inline]
        fn into_result(self) -> Result<Self::Ok, Self::Error> {
            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))
        }
    }