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
use std::marker::PhantomData;
use std::sync::Arc;

use futures::IntoFuture;
use tokio_service::{NewService, Service};

/// Create a `Service` from a function.
pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R>
where
    F: Fn(R) -> S,
    S: IntoFuture,
{
    ServiceFn {
        f: f,
        _req: PhantomData,
    }
}

/// Create a `NewService` by sharing references of `service.
pub fn const_service<S>(service: S) -> ConstService<S> {
    ConstService {
        svc: Arc::new(service),
    }
}

#[derive(Debug)]
pub struct ServiceFn<F, R> {
    f: F,
    _req: PhantomData<fn() -> R>,
}

impl<F, R, S> Service for ServiceFn<F, R>
where
    F: Fn(R) -> S,
    S: IntoFuture,
{
    type Request = R;
    type Response = S::Item;
    type Error = S::Error;
    type Future = S::Future;

    fn call(&self, req: Self::Request) -> Self::Future {
        (self.f)(req).into_future()
    }
}

#[derive(Debug)]
pub struct ConstService<S> {
    svc: Arc<S>,
}

impl<S> NewService for ConstService<S>
where
    S: Service,
{
    type Request = S::Request;
    type Response = S::Response;
    type Error = S::Error;
    type Instance = Arc<S>;

    fn new_service(&self) -> ::std::io::Result<Self::Instance> {
        Ok(self.svc.clone())
    }
}