finchers_ext/
lift.rs

1use finchers_core::endpoint::{Context, Endpoint, IntoEndpoint};
2use finchers_core::task::{self, Task};
3use finchers_core::{Error, Poll, PollResult};
4
5pub fn new<E>(endpoint: E) -> Lift<E::Endpoint>
6where
7    E: IntoEndpoint,
8{
9    Lift {
10        endpoint: endpoint.into_endpoint(),
11    }
12}
13
14#[allow(missing_docs)]
15#[derive(Copy, Clone, Debug)]
16pub struct Lift<E> {
17    endpoint: E,
18}
19
20impl<E> Endpoint for Lift<E>
21where
22    E: Endpoint,
23{
24    type Output = Option<E::Output>;
25    type Task = LiftTask<E::Task>;
26
27    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
28        Some(LiftTask {
29            task: self.endpoint.apply(cx),
30        })
31    }
32}
33
34#[derive(Debug)]
35pub struct LiftTask<T> {
36    task: Option<T>,
37}
38
39impl<T> Task for LiftTask<T>
40where
41    T: Task,
42{
43    type Output = Option<T::Output>;
44
45    fn poll_task(&mut self, cx: &mut task::Context) -> PollResult<Self::Output, Error> {
46        match self.task {
47            Some(ref mut t) => t.poll_task(cx).map_ok(Some),
48            None => Poll::Ready(Ok(None)),
49        }
50    }
51}