1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-12-01 05:10:06 +01:00
actix-net/actix-utils/src/cloneable.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

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-12-09 19:15:49 +01:00
use actix_service::Service;
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
/// 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> {
2019-03-05 16:35:26 +01:00
pub fn new<R>(service: T) -> Self
2018-11-30 20:55:30 +01:00
where
2019-03-05 16:35:26 +01:00
T: Service<R>,
2018-11-30 20:55:30 +01:00
{
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
}
}
}
2019-03-05 16:35:26 +01:00
impl<T, R> Service<R> for CloneableService<T>
2018-11-30 20:55:30 +01:00
where
2019-03-05 16:35:26 +01:00
T: Service<R> + 'static,
2018-11-30 20:55:30 +01:00
{
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> {
2019-01-26 22:15:17 +01:00
self.service.get_mut().poll_ready()
2018-09-18 06:46:02 +02:00
}
2019-03-05 16:35:26 +01:00
fn call(&mut self, req: R) -> Self::Future {
2019-01-26 22:15:17 +01:00
self.service.get_mut().call(req)
2018-09-18 06:46:02 +02:00
}
}