From 72e13c515451e5343974614802f4c2570b784971 Mon Sep 17 00:00:00 2001 From: Carl Lerche Date: Thu, 8 Sep 2016 22:43:52 -0700 Subject: [PATCH] Move in NewService --- src/lib.rs | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 475cc27..ae895d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,11 +8,12 @@ extern crate futures; +use futures::{Async, Future, IntoFuture}; + +use std::io; use std::marker::PhantomData; use std::sync::Arc; -use futures::{Async, Future, IntoFuture}; - /// An asynchronous function from `Request` to a `Response`. /// /// The `Service` trait is a simplified interface making it easy to write @@ -157,6 +158,25 @@ pub trait Service { fn poll_ready(&self) -> Async<()>; } +/// Creates new `Service` values. +pub trait NewService { + + /// Requests handled by the service + type Request; + + /// Responses given by the service + type Response; + + /// Errors produced by the service + type Error; + + /// The `Service` value created by this factory + type Item: Service; + + /// Create and return a new service value. + fn new_service(&self) -> io::Result; +} + /// A service implemented by a closure. pub struct SimpleService { f: Arc, @@ -178,6 +198,19 @@ impl SimpleService { } } +impl NewService for T + where T: Service + Clone, +{ + type Item = T; + type Request = T::Request; + type Response = T::Response; + type Error = T::Error; + + fn new_service(&self) -> io::Result { + Ok(self.clone()) + } +} + impl Service for SimpleService where F: Fn(R) -> S + Sync + Send + 'static, R: Send + 'static,