actix_utils/future/
ready.rs1use core::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8
9#[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    #[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#[inline]
69pub fn ready<T>(val: T) -> Ready<T> {
70    Ready { val: Some(val) }
71}
72
73#[inline]
85pub fn ok<T, E>(val: T) -> Ready<Result<T, E>> {
86    Ready { val: Some(Ok(val)) }
87}
88
89#[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        let _ = Pin::new(&mut ready).poll(&mut cx);
131    }
132}