diff --git a/src/framed.rs b/src/framed.rs index 5f1904d7..69cd6bd6 100644 --- a/src/framed.rs +++ b/src/framed.rs @@ -412,3 +412,78 @@ where } } } + +pub struct IntoFramed +where + T: AsyncRead + AsyncWrite, + F: Fn() -> U + Send + Clone + 'static, + U: Encoder + Decoder, +{ + factory: F, + _t: PhantomData<(T,)>, +} + +impl IntoFramed +where + T: AsyncRead + AsyncWrite, + F: Fn() -> U + Send + Clone + 'static, + U: Encoder + Decoder, +{ + pub fn new(factory: F) -> Self { + IntoFramed { + factory, + _t: PhantomData, + } + } +} + +impl NewService for IntoFramed +where + T: AsyncRead + AsyncWrite, + F: Fn() -> U + Send + Clone + 'static, + U: Encoder + Decoder, +{ + type Request = T; + type Response = Framed; + type Error = (); + type InitError = (); + type Service = IntoFramedService; + type Future = FutureResult; + + fn new_service(&self) -> Self::Future { + ok(IntoFramedService { + factory: self.factory.clone(), + _t: PhantomData, + }) + } +} + +pub struct IntoFramedService +where + T: AsyncRead + AsyncWrite, + F: Fn() -> U + Send + Clone + 'static, + U: Encoder + Decoder, +{ + factory: F, + _t: PhantomData<(T,)>, +} + +impl Service for IntoFramedService +where + T: AsyncRead + AsyncWrite, + F: Fn() -> U + Send + Clone + 'static, + U: Encoder + Decoder, +{ + type Request = T; + type Response = Framed; + type Error = (); + type Future = FutureResult; + + fn poll_ready(&mut self) -> Poll<(), Self::Error> { + Ok(Async::Ready(())) + } + + fn call(&mut self, req: Self::Request) -> Self::Future { + ok(Framed::new(req, (self.factory)())) + } +}