From 601c8a4ee69e031832fb831e55377fa6b454b505 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 17 Sep 2018 21:46:02 -0700 Subject: [PATCH] add CloneableService --- src/cloneable.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 43 insertions(+) create mode 100644 src/cloneable.rs diff --git a/src/cloneable.rs b/src/cloneable.rs new file mode 100644 index 00000000..d94e2953 --- /dev/null +++ b/src/cloneable.rs @@ -0,0 +1,42 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use futures::Poll; + +use super::service::Service; + +/// Service that allows to turn non-clone service to a service with `Clone` impl +pub struct CloneableService { + service: Rc>, +} + +impl CloneableService { + pub fn new(service: S) -> Self { + Self { + service: Rc::new(RefCell::new(service)), + } + } +} + +impl Clone for CloneableService { + fn clone(&self) -> Self { + Self { + service: self.service.clone(), + } + } +} + +impl Service for CloneableService { + type Request = S::Request; + 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() + } + + fn call(&mut self, req: Self::Request) -> Self::Future { + self.service.borrow_mut().call(req) + } +} diff --git a/src/lib.rs b/src/lib.rs index 8a82a0b9..d4c592ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,6 +55,7 @@ extern crate webpki; #[cfg(feature = "rust-tls")] extern crate webpki_roots; +pub mod cloneable; pub mod connector; pub mod counter; pub mod framed;