mirror of
https://github.com/actix/actix-web.git
synced 2025-03-28 13:55:28 +00:00
51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
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: 'static> {
|
|
service: Cell<T>,
|
|
_t: PhantomData<Rc<()>>,
|
|
}
|
|
|
|
impl<T: 'static> CloneableService<T> {
|
|
pub fn new<Request>(service: T) -> Self
|
|
where
|
|
T: Service<Request>,
|
|
{
|
|
Self {
|
|
service: Cell::new(service),
|
|
_t: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<T: 'static> Clone for CloneableService<T> {
|
|
fn clone(&self) -> Self {
|
|
Self {
|
|
service: self.service.clone(),
|
|
_t: PhantomData,
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
|
|
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
|
self.service.borrow_mut().poll_ready()
|
|
}
|
|
|
|
fn call(&mut self, req: Request) -> Self::Future {
|
|
self.service.borrow_mut().call(req)
|
|
}
|
|
}
|