actix_service/
ready.rs

1//! When MSRV is 1.48, replace with `core::future::Ready` and `core::future::ready()`.
2
3use core::{
4    future::Future,
5    pin::Pin,
6    task::{Context, Poll},
7};
8
9/// Future for the [`ready`](ready()) function.
10#[derive(Debug, Clone)]
11#[must_use = "futures do nothing unless you `.await` or poll them"]
12pub struct Ready<T> {
13    val: Option<T>,
14}
15
16impl<T> Ready<T> {
17    /// Unwraps the value from this immediately ready future.
18    #[inline]
19    pub fn into_inner(mut self) -> T {
20        self.val.take().unwrap()
21    }
22}
23
24impl<T> Unpin for Ready<T> {}
25
26impl<T> Future for Ready<T> {
27    type Output = T;
28
29    #[inline]
30    fn poll(mut self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<T> {
31        let val = self.val.take().expect("Ready can not be polled twice.");
32        Poll::Ready(val)
33    }
34}
35
36/// Creates a future that is immediately ready with a value.
37#[allow(dead_code)]
38pub(crate) fn ready<T>(val: T) -> Ready<T> {
39    Ready { val: Some(val) }
40}
41
42/// Create a future that is immediately ready with a success value.
43#[allow(dead_code)]
44pub(crate) fn ok<T, E>(val: T) -> Ready<Result<T, E>> {
45    Ready { val: Some(Ok(val)) }
46}
47
48/// Create a future that is immediately ready with an error value.
49#[allow(dead_code)]
50pub(crate) fn err<T, E>(err: E) -> Ready<Result<T, E>> {
51    Ready {
52        val: Some(Err(err)),
53    }
54}