actix_utils/future/
ready.rs1#![allow(deprecated)]
4
5use core::{
6 future::Future,
7 pin::Pin,
8 task::{Context, Poll},
9};
10
11#[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 #[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#[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#[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#[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 let _ = Pin::new(&mut ready).poll(&mut cx);
142 }
143}