Move in NewService
This commit is contained in:
parent
2bfa5ced0f
commit
72e13c5154
37
src/lib.rs
37
src/lib.rs
|
@ -8,11 +8,12 @@
|
||||||
|
|
||||||
extern crate futures;
|
extern crate futures;
|
||||||
|
|
||||||
|
use futures::{Async, Future, IntoFuture};
|
||||||
|
|
||||||
|
use std::io;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use futures::{Async, Future, IntoFuture};
|
|
||||||
|
|
||||||
/// An asynchronous function from `Request` to a `Response`.
|
/// An asynchronous function from `Request` to a `Response`.
|
||||||
///
|
///
|
||||||
/// The `Service` trait is a simplified interface making it easy to write
|
/// The `Service` trait is a simplified interface making it easy to write
|
||||||
|
@ -157,6 +158,25 @@ pub trait Service {
|
||||||
fn poll_ready(&self) -> Async<()>;
|
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<Request = Self::Request, Response = Self::Response, Error = Self::Error>;
|
||||||
|
|
||||||
|
/// Create and return a new service value.
|
||||||
|
fn new_service(&self) -> io::Result<Self::Item>;
|
||||||
|
}
|
||||||
|
|
||||||
/// A service implemented by a closure.
|
/// A service implemented by a closure.
|
||||||
pub struct SimpleService<F, R> {
|
pub struct SimpleService<F, R> {
|
||||||
f: Arc<F>,
|
f: Arc<F>,
|
||||||
|
@ -178,6 +198,19 @@ impl<F, R> SimpleService<F, R> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> 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<T> {
|
||||||
|
Ok(self.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<F, R, S> Service for SimpleService<F, R>
|
impl<F, R, S> Service for SimpleService<F, R>
|
||||||
where F: Fn(R) -> S + Sync + Send + 'static,
|
where F: Fn(R) -> S + Sync + Send + 'static,
|
||||||
R: Send + 'static,
|
R: Send + 'static,
|
||||||
|
|
Loading…
Reference in New Issue