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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! Extension for `Service`s.

mod and_then;
mod err_into;
mod join;
mod map;
mod map_err;

pub use self::{
    and_then::AndThen, //
    err_into::ErrInto,
    join::Join,
    map::Map,
    map_err::MapErr,
};

use {
    crate::Service, //
    futures::IntoFuture,
    std::marker::PhantomData,
};

/// A set of extensions for `Service`s.
pub trait ServiceExt<Req>: Service<Req> + Sized {
    /// Maps the response value returned from this service into a different type using the specified function.
    fn service_map<F, Res>(self, f: F) -> Map<Self, F>
    where
        F: Fn(Self::Response) -> Res + Clone,
    {
        Map { service: self, f }
    }

    /// Maps the error value produced by this service into a different type using the specified function.
    fn service_map_err<F, E>(self, f: F) -> MapErr<Self, F>
    where
        F: Fn(Self::Error) -> E + Clone,
    {
        MapErr { service: self, f }
    }

    /// Converts the error value produced by this service into a different type.
    fn service_err_into<E>(self) -> ErrInto<Self, E>
    where
        Self::Error: Into<E>,
    {
        ErrInto {
            service: self,
            _marker: PhantomData,
        }
    }

    /// Executes the future returned from the specified function when this service returns a response.
    fn service_and_then<F, R>(self, f: F) -> AndThen<Self, F>
    where
        F: Fn(Self::Response) -> R + Clone,
        R: IntoFuture<Error = Self::Error>,
    {
        AndThen { service: self, f }
    }

    /// Combines this service with the the specified one.
    ///
    /// The specified `Service` has the same request type as this service.
    fn service_join<S>(self, service: S) -> Join<Self, S>
    where
        S: Service<Req, Error = Self::Error>,
        Req: Clone,
    {
        Join {
            s1: self,
            s2: service,
        }
    }
}

impl<S, Req> ServiceExt<Req> for S where S: Service<Req> {}