2018-11-30 03:56:15 +01:00
|
|
|
use std::marker::PhantomData;
|
2018-11-30 20:55:30 +01:00
|
|
|
use std::rc::Rc;
|
2018-11-30 03:56:15 +01:00
|
|
|
|
2018-09-18 06:46:02 +02:00
|
|
|
use futures::Poll;
|
|
|
|
|
2018-10-03 07:18:07 +02:00
|
|
|
use super::cell::Cell;
|
2018-09-18 06:46:02 +02:00
|
|
|
use super::service::Service;
|
|
|
|
|
|
|
|
/// Service that allows to turn non-clone service to a service with `Clone` impl
|
2018-11-30 20:55:30 +01:00
|
|
|
pub struct CloneableService<T: 'static> {
|
|
|
|
service: Cell<T>,
|
|
|
|
_t: PhantomData<Rc<()>>,
|
2018-09-18 06:46:02 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 20:55:30 +01:00
|
|
|
impl<T: 'static> CloneableService<T> {
|
|
|
|
pub fn new<Request>(service: T) -> Self
|
|
|
|
where
|
|
|
|
T: Service<Request>,
|
|
|
|
{
|
2018-09-18 06:46:02 +02:00
|
|
|
Self {
|
2018-10-03 07:18:07 +02:00
|
|
|
service: Cell::new(service),
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData,
|
2018-09-18 06:46:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:55:30 +01:00
|
|
|
impl<T: 'static> Clone for CloneableService<T> {
|
2018-09-18 06:46:02 +02:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self {
|
|
|
|
service: self.service.clone(),
|
2018-11-30 03:56:15 +01:00
|
|
|
_t: PhantomData,
|
2018-09-18 06:46:02 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:55:30 +01:00
|
|
|
impl<T: 'static, Request> Service<Request> for CloneableService<T>
|
|
|
|
where
|
|
|
|
T: Service<Request>,
|
|
|
|
{
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
2018-09-18 06:46:02 +02:00
|
|
|
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
|
|
self.service.borrow_mut().poll_ready()
|
|
|
|
}
|
|
|
|
|
2018-11-30 20:55:30 +01:00
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
2018-09-18 06:46:02 +02:00
|
|
|
self.service.borrow_mut().call(req)
|
|
|
|
}
|
|
|
|
}
|