Skip to main content

actix_utils/future/
ready.rs

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