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

53 lines
1.0 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
2019-04-04 19:04:19 +02:00
pub struct CloneableService<T> {
2018-11-30 20:55:30 +01:00
service: Cell<T>,
_t: PhantomData<Rc<()>>,
2018-09-18 06:46:02 +02:00
}
2019-04-04 19:04:19 +02:00
impl<T> CloneableService<T> {
2019-03-09 16:27:35 +01:00
pub fn new(service: T) -> Self
2018-11-30 20:55:30 +01:00
where
2019-03-09 16:27:35 +01:00
T: Service,
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
}
}
}
2019-04-04 19:04:19 +02:00
impl<T> 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-09 16:27:35 +01:00
impl<T> Service for CloneableService<T>
2018-11-30 20:55:30 +01:00
where
2019-04-04 19:04:19 +02:00
T: Service,
2018-11-30 20:55:30 +01:00
{
2019-03-09 16:27:35 +01:00
type Request = T::Request;
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-09 16:27:35 +01:00
fn call(&mut self, req: T::Request) -> Self::Future {
2019-01-26 22:15:17 +01:00
self.service.get_mut().call(req)
2018-09-18 06:46:02 +02:00
}
}