1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
use futures::{future, Future};

use context::Context;
use endpoint::{Endpoint, EndpointResult};


/// Equivalent to `e.map(f)`
pub fn map<E, F, R>(endpoint: E, f: F) -> Map<E, F>
where
    E: Endpoint,
    F: FnOnce(E::Item) -> R,
{
    Map { endpoint, f }
}


/// The return type of `map(e, f)`
#[derive(Debug)]
pub struct Map<E, F> {
    endpoint: E,
    f: F,
}

impl<E, F, R> Endpoint for Map<E, F>
where
    E: Endpoint,
    F: FnOnce(E::Item) -> R,
{
    type Item = R;
    type Error = E::Error;
    type Future = future::Map<E::Future, F>;

    fn apply(self, ctx: &mut Context) -> EndpointResult<Self::Future> {
        let Map { endpoint, f } = self;
        let fut = endpoint.apply(ctx)?;
        Ok(fut.map(f))
    }
}