finchers_ext/option/
map_some.rs

1#![allow(missing_docs)]
2
3use finchers_core::endpoint::{Context, Endpoint};
4use finchers_core::task::{self, Task};
5use finchers_core::{Error, Poll};
6
7#[derive(Debug, Copy, Clone)]
8pub struct MapSome<E, F> {
9    endpoint: E,
10    f: F,
11}
12
13pub fn new<E, F, U, T>(endpoint: E, f: F) -> MapSome<E, F>
14where
15    E: Endpoint<Output = Option<T>>,
16    F: FnOnce(T) -> U + Clone + Send + Sync,
17{
18    MapSome { endpoint, f }
19}
20
21impl<E, F, T, U> Endpoint for MapSome<E, F>
22where
23    E: Endpoint<Output = Option<T>>,
24    F: FnOnce(T) -> U + Clone + Send + Sync,
25{
26    type Output = Option<U>;
27    type Task = MapSomeTask<E::Task, F>;
28
29    fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
30        Some(MapSomeTask {
31            task: self.endpoint.apply(cx)?,
32            f: Some(self.f.clone()),
33        })
34    }
35}
36
37#[derive(Debug)]
38pub struct MapSomeTask<T, F> {
39    task: T,
40    f: Option<F>,
41}
42
43impl<T, F, A, U> Task for MapSomeTask<T, F>
44where
45    T: Task<Output = Option<A>> + Send,
46    F: FnOnce(A) -> U + Send,
47{
48    type Output = Option<U>;
49
50    fn poll_task(&mut self, cx: &mut task::Context) -> Poll<Result<Self::Output, Error>> {
51        self.task.poll_task(cx).map_ok(|item| {
52            let f = self.f.take().expect("cannot resolve twice");
53            cx.input().enter_scope(|| item.map(f))
54        })
55    }
56}