1
0
mirror of https://github.com/fafhrd91/actix-net synced 2025-01-31 16:10:08 +01:00
actix-net/actix-utils/src/cloneable.rs

53 lines
1.1 KiB
Rust
Raw Normal View History

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