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
use std::marker::PhantomData;
use std::rc::Rc;

use actix_service::Service;
use futures::Poll;

use super::cell::Cell;

/// Service that allows to turn non-clone service to a service with `Clone` impl
pub struct CloneableService<T> {
    service: Cell<T>,
    _t: PhantomData<Rc<()>>,
}

impl<T> CloneableService<T> {
    pub fn new(service: T) -> Self
    where
        T: Service,
    {
        Self {
            service: Cell::new(service),
            _t: PhantomData,
        }
    }
}

impl<T> Clone for CloneableService<T> {
    fn clone(&self) -> Self {
        Self {
            service: self.service.clone(),
            _t: PhantomData,
        }
    }
}

impl<T> Service for CloneableService<T>
where
    T: Service,
{
    type Request = T::Request;
    type Response = T::Response;
    type Error = T::Error;
    type Future = T::Future;

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

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