2020-01-24 07:51:38 +02:00
|
|
|
use std::cell::RefCell;
|
2019-07-17 13:55:44 +06:00
|
|
|
use std::rc::Rc;
|
2019-11-15 15:54:11 +06:00
|
|
|
use std::task::{Context, Poll};
|
2019-07-17 13:55:44 +06:00
|
|
|
|
|
|
|
use actix_service::Service;
|
|
|
|
|
|
|
|
#[doc(hidden)]
|
|
|
|
/// Service that allows to turn non-clone service to a service with `Clone` impl
|
2020-01-25 01:05:25 +02:00
|
|
|
///
|
2020-01-24 07:51:38 +02:00
|
|
|
/// # Panics
|
|
|
|
/// CloneableService might panic with some creative use of thread local storage.
|
|
|
|
/// See https://github.com/actix/actix-web/issues/1295 for example
|
|
|
|
pub(crate) struct CloneableService<T: Service>(Rc<RefCell<T>>);
|
2019-07-17 13:55:44 +06:00
|
|
|
|
2020-01-10 11:36:59 +06:00
|
|
|
impl<T: Service> CloneableService<T> {
|
|
|
|
pub(crate) fn new(service: T) -> Self {
|
2020-01-24 07:51:38 +02:00
|
|
|
Self(Rc::new(RefCell::new(service)))
|
2019-07-17 13:55:44 +06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:36:59 +06:00
|
|
|
impl<T: Service> Clone for CloneableService<T> {
|
2019-07-17 13:55:44 +06:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self(self.0.clone())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 11:36:59 +06:00
|
|
|
impl<T: Service> Service for CloneableService<T> {
|
2019-07-17 13:55:44 +06:00
|
|
|
type Request = T::Request;
|
|
|
|
type Response = T::Response;
|
|
|
|
type Error = T::Error;
|
|
|
|
type Future = T::Future;
|
|
|
|
|
2019-12-08 00:46:51 +06:00
|
|
|
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-01-24 07:51:38 +02:00
|
|
|
self.0.borrow_mut().poll_ready(cx)
|
2019-07-17 13:55:44 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn call(&mut self, req: T::Request) -> Self::Future {
|
2020-01-24 07:51:38 +02:00
|
|
|
self.0.borrow_mut().call(req)
|
2019-07-17 13:55:44 +06:00
|
|
|
}
|
|
|
|
}
|