finchers_ext/result/
map_ok.rs1#![allow(missing_docs)]
2
3use finchers_core::endpoint::{Context, Endpoint};
4use finchers_core::task::{self, Task};
5use finchers_core::{Error, PollResult};
6
7#[derive(Debug, Copy, Clone)]
8pub struct MapOk<E, F> {
9 endpoint: E,
10 f: F,
11}
12
13pub fn new<E, F, U, A, B>(endpoint: E, f: F) -> MapOk<E, F>
14where
15 E: Endpoint<Output = Result<A, B>>,
16 F: FnOnce(A) -> U + Clone + Send + Sync,
17{
18 MapOk { endpoint, f }
19}
20
21impl<E, F, A, B, U> Endpoint for MapOk<E, F>
22where
23 E: Endpoint<Output = Result<A, B>>,
24 F: FnOnce(A) -> U + Clone + Send + Sync,
25{
26 type Output = Result<U, B>;
27 type Task = MapOkTask<E::Task, F>;
28
29 fn apply(&self, cx: &mut Context) -> Option<Self::Task> {
30 Some(MapOkTask {
31 task: self.endpoint.apply(cx)?,
32 f: Some(self.f.clone()),
33 })
34 }
35}
36
37#[derive(Debug)]
38pub struct MapOkTask<T, F> {
39 task: T,
40 f: Option<F>,
41}
42
43impl<T, F, U, A, B> Task for MapOkTask<T, F>
44where
45 T: Task<Output = Result<A, B>> + Send,
46 F: FnOnce(A) -> U + Send,
47{
48 type Output = Result<U, B>;
49
50 fn poll_task(&mut self, cx: &mut task::Context) -> PollResult<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}