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
use futures::Poll;

use super::cell::Cell;
use super::service::Service;

/// Service that allows to turn non-clone service to a service with `Clone` impl
pub struct CloneableService<S: Service + 'static> {
    service: Cell<S>,
}

impl<S: Service + 'static> CloneableService<S> {
    pub fn new(service: S) -> Self {
        Self {
            service: Cell::new(service),
        }
    }
}

impl<S: Service + 'static> Clone for CloneableService<S> {
    fn clone(&self) -> Self {
        Self {
            service: self.service.clone(),
        }
    }
}

impl<S: Service + 'static> Service for CloneableService<S> {
    type Request = S::Request;
    type Response = S::Response;
    type Error = S::Error;
    type Future = S::Future;

    fn poll_ready(&mut self) -> Poll<(), Self::Error> {
        self.service.borrow_mut().poll_ready()
    }

    fn call(&mut self, req: Self::Request) -> Self::Future {
        self.service.borrow_mut().call(req)
    }
}