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

45 lines
1.0 KiB
Rust
Raw Normal View History

2018-11-30 03:56:15 +01:00
use std::marker::PhantomData;
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
use super::service::Service;
/// Service that allows to turn non-clone service to a service with `Clone` impl
2018-11-30 03:56:15 +01:00
pub struct CloneableService<S: Service<R> + 'static, R> {
2018-10-03 07:18:07 +02:00
service: Cell<S>,
2018-11-30 03:56:15 +01:00
_t: PhantomData<R>,
2018-09-18 06:46:02 +02:00
}
2018-11-30 03:56:15 +01:00
impl<S: Service<R> + 'static, R> CloneableService<S, R> {
2018-09-18 06:46:02 +02:00
pub fn new(service: S) -> Self {
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 03:56:15 +01:00
impl<S: Service<R> + 'static, R> Clone for CloneableService<S, R> {
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
}
}
}
2018-11-30 03:56:15 +01:00
impl<S: Service<R> + 'static, R> Service<R> for CloneableService<S, R> {
2018-09-18 06:46:02 +02:00
type Response = S::Response;
type Error = S::Error;
type Future = S::Future;
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
self.service.borrow_mut().poll_ready()
}
2018-11-30 03:56:15 +01:00
fn call(&mut self, req: R) -> Self::Future {
2018-09-18 06:46:02 +02:00
self.service.borrow_mut().call(req)
}
}