actix_utils/future/
ready.rs

1//! When `core::future::Ready` has a `into_inner()` method, this can be deprecated.
2
3use core::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8
9/// Future for the [`ready`] function.
10///
11/// Panic will occur if polled more than once.
12///
13/// # Examples
14/// ```
15/// use actix_utils::future::ready;
16///
17/// // async
18/// # async fn run() {
19/// let a = ready(1);
20/// assert_eq!(a.await, 1);
21/// # }
22///
23/// // sync
24/// let a = ready(1);
25/// assert_eq!(a.into_inner(), 1);
26/// ```
27#[derive(Debug, Clone)]
28#[must_use = "futures do nothing unless you `.await` or poll them"]
29pub struct Ready<T> {
30    val: Option<T>,
31}
32
33impl<T> Ready<T> {
34    /// Unwraps the value from this immediately ready future.
35    #[inline]
36    pub fn into_inner(mut self) -> T {
37        self.val.take().unwrap()
38    }
39}
40
41impl<T> Unpin for Ready<T> {}
42
43impl<T> Future for Ready<T> {
44    type Output = T;
45
46    #[inline]
47    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
48        let val = self.val.take().expect("Ready polled after completion");
49        Poll::Ready(val)
50    }
51}
52
53/// Creates a future that is immediately ready with a value.
54///
55/// # Examples
56/// ```no_run
57/// use actix_utils::future::ready;
58///
59/// # async fn run() {
60/// let a = ready(1);
61/// assert_eq!(a.await, 1);
62/// # }
63///
64/// // sync
65/// let a = ready(1);
66/// assert_eq!(a.into_inner(), 1);
67/// ```
68#[inline]
69pub fn ready<T>(val: T) -> Ready<T> {
70    Ready { val: Some(val) }
71}
72
73/// Creates a future that is immediately ready with a success value.
74///
75/// # Examples
76/// ```no_run
77/// use actix_utils::future::ok;
78///
79/// # async fn run() {
80/// let a = ok::<_, ()>(1);
81/// assert_eq!(a.await, Ok(1));
82/// # }
83/// ```
84#[inline]
85pub fn ok<T, E>(val: T) -> Ready<Result<T, E>> {
86    Ready { val: Some(Ok(val)) }
87}
88
89/// Creates a future that is immediately ready with an error value.
90///
91/// # Examples
92/// ```no_run
93/// use actix_utils::future::err;
94///
95/// # async fn run() {
96/// let a = err::<(), _>(1);
97/// assert_eq!(a.await, Err(1));
98/// # }
99/// ```
100#[inline]
101pub fn err<T, E>(err: E) -> Ready<Result<T, E>> {
102    Ready {
103        val: Some(Err(err)),
104    }
105}
106
107#[cfg(test)]
108mod tests {
109    use std::rc::Rc;
110
111    use futures_util::task::noop_waker;
112    use static_assertions::{assert_impl_all, assert_not_impl_any};
113
114    use super::*;
115
116    assert_impl_all!(Ready<()>: Send, Sync, Unpin, Clone);
117    assert_impl_all!(Ready<Rc<()>>: Unpin, Clone);
118    assert_not_impl_any!(Ready<Rc<()>>: Send, Sync);
119
120    #[test]
121    #[should_panic]
122    fn multiple_poll_panics() {
123        let waker = noop_waker();
124        let mut cx = Context::from_waker(&waker);
125
126        let mut ready = ready(1);
127        assert_eq!(Pin::new(&mut ready).poll(&mut cx), Poll::Ready(1));
128
129        // panic!
130        let _ = Pin::new(&mut ready).poll(&mut cx);
131    }
132}