mirror of
https://github.com/fafhrd91/actix-net
synced 2024-11-27 18:02:58 +01:00
update ignored service docs to new traits
This commit is contained in:
parent
8e98d9168c
commit
ef206f40fb
@ -53,8 +53,14 @@ use self::ready::{err, ok, ready, Ready};
|
|||||||
/// async fn(Request) -> Result<Response, Err>
|
/// async fn(Request) -> Result<Response, Err>
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// The `Service` trait just generalizes this form where each parameter is described as an
|
/// The `Service` trait just generalizes this form. Requests are defined as a generic type parameter
|
||||||
/// associated type on the trait. Services can also have mutable state that influence computation.
|
/// and responses and other details are defined as associated types on the trait impl. Notice that
|
||||||
|
/// this design means that services can receive many request types and converge them to a single
|
||||||
|
/// response type.
|
||||||
|
///
|
||||||
|
/// Services can also have mutable state that influence computation by using a `Cell`, `RefCell`
|
||||||
|
/// or `Mutex`. Services intentionally do not take `&mut self` to reduce over-head in the
|
||||||
|
/// common cases.
|
||||||
///
|
///
|
||||||
/// `Service` provides a symmetric and uniform API; the same abstractions can be used to represent
|
/// `Service` provides a symmetric and uniform API; the same abstractions can be used to represent
|
||||||
/// both clients and servers. Services describe only _transformation_ operations which encourage
|
/// both clients and servers. Services describe only _transformation_ operations which encourage
|
||||||
@ -64,8 +70,7 @@ use self::ready::{err, ok, ready, Ready};
|
|||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// struct MyService;
|
/// struct MyService;
|
||||||
///
|
///
|
||||||
/// impl Service for MyService {
|
/// impl Service<u8> for MyService {
|
||||||
/// type Request = u8;
|
|
||||||
/// type Response = u64;
|
/// type Response = u64;
|
||||||
/// type Error = MyError;
|
/// type Error = MyError;
|
||||||
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
|
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
|
||||||
@ -81,6 +86,9 @@ use self::ready::{err, ok, ready, Ready};
|
|||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// async fn my_service(req: u8) -> Result<u64, MyError>;
|
/// async fn my_service(req: u8) -> Result<u64, MyError>;
|
||||||
|
///
|
||||||
|
/// let svc = fn_service(my_service)
|
||||||
|
/// svc.call(123)
|
||||||
/// ```
|
/// ```
|
||||||
pub trait Service<Req> {
|
pub trait Service<Req> {
|
||||||
/// Responses given by the service.
|
/// Responses given by the service.
|
||||||
@ -144,7 +152,7 @@ pub trait ServiceFactory<Req> {
|
|||||||
/// Errors potentially raised while building a service.
|
/// Errors potentially raised while building a service.
|
||||||
type InitError;
|
type InitError;
|
||||||
|
|
||||||
/// The future of the `Service` instance.
|
/// The future of the `Service` instance.g
|
||||||
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
|
||||||
|
|
||||||
/// Create and return a new service asynchronously.
|
/// Create and return a new service asynchronously.
|
||||||
|
@ -27,7 +27,7 @@ where
|
|||||||
/// Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in
|
/// Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in
|
||||||
/// the request/response lifecycle. It may modify request and/or response.
|
/// the request/response lifecycle. It may modify request and/or response.
|
||||||
///
|
///
|
||||||
/// For example, timeout transform:
|
/// For example, a timeout service wrapper:
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// pub struct Timeout<S> {
|
/// pub struct Timeout<S> {
|
||||||
@ -35,11 +35,7 @@ where
|
|||||||
/// timeout: Duration,
|
/// timeout: Duration,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl<S> Service for Timeout<S>
|
/// impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
|
||||||
/// where
|
|
||||||
/// S: Service,
|
|
||||||
/// {
|
|
||||||
/// type Request = S::Request;
|
|
||||||
/// type Response = S::Response;
|
/// type Response = S::Response;
|
||||||
/// type Error = TimeoutError<S::Error>;
|
/// type Error = TimeoutError<S::Error>;
|
||||||
/// type Future = TimeoutServiceResponse<S>;
|
/// type Future = TimeoutServiceResponse<S>;
|
||||||
@ -55,26 +51,22 @@ where
|
|||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// Timeout service in above example is decoupled from underlying service implementation and could
|
/// This wrapper service is decoupled from the underlying service implementation and could be
|
||||||
/// be applied to any service.
|
/// applied to any service.
|
||||||
///
|
///
|
||||||
/// The `Transform` trait defines the interface of a Service factory. `Transform` is often
|
/// The `Transform` trait defines the interface of a service wrapper. `Transform` is often
|
||||||
/// implemented for middleware, defining how to construct a middleware Service. A Service that is
|
/// implemented for middleware, defining how to construct a middleware Service. A Service that is
|
||||||
/// constructed by the factory takes the Service that follows it during execution as a parameter,
|
/// constructed by the factory takes the Service that follows it during execution as a parameter,
|
||||||
/// assuming ownership of the next Service.
|
/// assuming ownership of the next Service.
|
||||||
///
|
///
|
||||||
/// Factory for `Timeout` middleware from the above example could look like this:
|
/// A transform for the `Timeout` middleware could look like this:
|
||||||
///
|
///
|
||||||
/// ```ignore
|
/// ```ignore
|
||||||
/// pub struct TimeoutTransform {
|
/// pub struct TimeoutTransform {
|
||||||
/// timeout: Duration,
|
/// timeout: Duration,
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// impl<S> Transform<S> for TimeoutTransform
|
/// impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
|
||||||
/// where
|
|
||||||
/// S: Service,
|
|
||||||
/// {
|
|
||||||
/// type Request = S::Request;
|
|
||||||
/// type Response = S::Response;
|
/// type Response = S::Response;
|
||||||
/// type Error = TimeoutError<S::Error>;
|
/// type Error = TimeoutError<S::Error>;
|
||||||
/// type InitError = S::Error;
|
/// type InitError = S::Error;
|
||||||
@ -82,7 +74,7 @@ where
|
|||||||
/// type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
/// type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
///
|
///
|
||||||
/// fn new_transform(&self, service: S) -> Self::Future {
|
/// fn new_transform(&self, service: S) -> Self::Future {
|
||||||
/// ready(Ok(TimeoutService {
|
/// ready(Ok(Timeout {
|
||||||
/// service,
|
/// service,
|
||||||
/// timeout: self.timeout,
|
/// timeout: self.timeout,
|
||||||
/// }))
|
/// }))
|
||||||
@ -227,3 +219,54 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use core::{
|
||||||
|
future::{ready, Ready},
|
||||||
|
time::Duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::*;
|
||||||
|
use crate::Service;
|
||||||
|
|
||||||
|
// pseudo-doctest for Transform trait
|
||||||
|
pub struct TimeoutTransform {
|
||||||
|
timeout: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudo-doctest for Transform trait
|
||||||
|
impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type InitError = S::Error;
|
||||||
|
type Transform = Timeout<S>;
|
||||||
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
||||||
|
|
||||||
|
fn new_transform(&self, service: S) -> Self::Future {
|
||||||
|
ready(Ok(Timeout {
|
||||||
|
service,
|
||||||
|
_timeout: self.timeout,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudo-doctest for Transform trait
|
||||||
|
pub struct Timeout<S> {
|
||||||
|
service: S,
|
||||||
|
_timeout: Duration,
|
||||||
|
}
|
||||||
|
|
||||||
|
// pseudo-doctest for Transform trait
|
||||||
|
impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
|
||||||
|
type Response = S::Response;
|
||||||
|
type Error = S::Error;
|
||||||
|
type Future = S::Future;
|
||||||
|
|
||||||
|
crate::forward_ready!(service);
|
||||||
|
|
||||||
|
fn call(&self, req: Req) -> Self::Future {
|
||||||
|
self.service.call(req)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user