Skip to content

Commit ef206f4

Browse files
committed
update ignored service docs to new traits
1 parent 8e98d91 commit ef206f4

File tree

2 files changed

+72
-21
lines changed

2 files changed

+72
-21
lines changed

actix-service/src/lib.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,14 @@ use self::ready::{err, ok, ready, Ready};
5353
/// async fn(Request) -> Result<Response, Err>
5454
/// ```
5555
///
56-
/// The `Service` trait just generalizes this form where each parameter is described as an
57-
/// associated type on the trait. Services can also have mutable state that influence computation.
56+
/// The `Service` trait just generalizes this form. Requests are defined as a generic type parameter
57+
/// and responses and other details are defined as associated types on the trait impl. Notice that
58+
/// this design means that services can receive many request types and converge them to a single
59+
/// response type.
60+
///
61+
/// Services can also have mutable state that influence computation by using a `Cell`, `RefCell`
62+
/// or `Mutex`. Services intentionally do not take `&mut self` to reduce over-head in the
63+
/// common cases.
5864
///
5965
/// `Service` provides a symmetric and uniform API; the same abstractions can be used to represent
6066
/// both clients and servers. Services describe only _transformation_ operations which encourage
@@ -64,8 +70,7 @@ use self::ready::{err, ok, ready, Ready};
6470
/// ```ignore
6571
/// struct MyService;
6672
///
67-
/// impl Service for MyService {
68-
/// type Request = u8;
73+
/// impl Service<u8> for MyService {
6974
/// type Response = u64;
7075
/// type Error = MyError;
7176
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
@@ -81,6 +86,9 @@ use self::ready::{err, ok, ready, Ready};
8186
///
8287
/// ```ignore
8388
/// async fn my_service(req: u8) -> Result<u64, MyError>;
89+
///
90+
/// let svc = fn_service(my_service)
91+
/// svc.call(123)
8492
/// ```
8593
pub trait Service<Req> {
8694
/// Responses given by the service.
@@ -144,7 +152,7 @@ pub trait ServiceFactory<Req> {
144152
/// Errors potentially raised while building a service.
145153
type InitError;
146154

147-
/// The future of the `Service` instance.
155+
/// The future of the `Service` instance.g
148156
type Future: Future<Output = Result<Self::Service, Self::InitError>>;
149157

150158
/// Create and return a new service asynchronously.

actix-service/src/transform.rs

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,15 @@ where
2727
/// Transform(middleware) wraps inner service and runs during inbound and/or outbound processing in
2828
/// the request/response lifecycle. It may modify request and/or response.
2929
///
30-
/// For example, timeout transform:
30+
/// For example, a timeout service wrapper:
3131
///
3232
/// ```ignore
3333
/// pub struct Timeout<S> {
3434
/// service: S,
3535
/// timeout: Duration,
3636
/// }
3737
///
38-
/// impl<S> Service for Timeout<S>
39-
/// where
40-
/// S: Service,
41-
/// {
42-
/// type Request = S::Request;
38+
/// impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
4339
/// type Response = S::Response;
4440
/// type Error = TimeoutError<S::Error>;
4541
/// type Future = TimeoutServiceResponse<S>;
@@ -55,34 +51,30 @@ where
5551
/// }
5652
/// ```
5753
///
58-
/// Timeout service in above example is decoupled from underlying service implementation and could
59-
/// be applied to any service.
54+
/// This wrapper service is decoupled from the underlying service implementation and could be
55+
/// applied to any service.
6056
///
61-
/// The `Transform` trait defines the interface of a Service factory. `Transform` is often
57+
/// The `Transform` trait defines the interface of a service wrapper. `Transform` is often
6258
/// implemented for middleware, defining how to construct a middleware Service. A Service that is
6359
/// constructed by the factory takes the Service that follows it during execution as a parameter,
6460
/// assuming ownership of the next Service.
6561
///
66-
/// Factory for `Timeout` middleware from the above example could look like this:
62+
/// A transform for the `Timeout` middleware could look like this:
6763
///
6864
/// ```ignore
6965
/// pub struct TimeoutTransform {
7066
/// timeout: Duration,
7167
/// }
7268
///
73-
/// impl<S> Transform<S> for TimeoutTransform
74-
/// where
75-
/// S: Service,
76-
/// {
77-
/// type Request = S::Request;
69+
/// impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
7870
/// type Response = S::Response;
7971
/// type Error = TimeoutError<S::Error>;
8072
/// type InitError = S::Error;
8173
/// type Transform = Timeout<S>;
8274
/// type Future = Ready<Result<Self::Transform, Self::InitError>>;
8375
///
8476
/// fn new_transform(&self, service: S) -> Self::Future {
85-
/// ready(Ok(TimeoutService {
77+
/// ready(Ok(Timeout {
8678
/// service,
8779
/// timeout: self.timeout,
8880
/// }))
@@ -227,3 +219,54 @@ where
227219
}
228220
}
229221
}
222+
223+
#[cfg(test)]
224+
mod tests {
225+
use core::{
226+
future::{ready, Ready},
227+
time::Duration,
228+
};
229+
230+
use super::*;
231+
use crate::Service;
232+
233+
// pseudo-doctest for Transform trait
234+
pub struct TimeoutTransform {
235+
timeout: Duration,
236+
}
237+
238+
// pseudo-doctest for Transform trait
239+
impl<S: Service<Req>, Req> Transform<S, Req> for TimeoutTransform {
240+
type Response = S::Response;
241+
type Error = S::Error;
242+
type InitError = S::Error;
243+
type Transform = Timeout<S>;
244+
type Future = Ready<Result<Self::Transform, Self::InitError>>;
245+
246+
fn new_transform(&self, service: S) -> Self::Future {
247+
ready(Ok(Timeout {
248+
service,
249+
_timeout: self.timeout,
250+
}))
251+
}
252+
}
253+
254+
// pseudo-doctest for Transform trait
255+
pub struct Timeout<S> {
256+
service: S,
257+
_timeout: Duration,
258+
}
259+
260+
// pseudo-doctest for Transform trait
261+
impl<S: Service<Req>, Req> Service<Req> for Timeout<S> {
262+
type Response = S::Response;
263+
type Error = S::Error;
264+
type Future = S::Future;
265+
266+
crate::forward_ready!(service);
267+
268+
fn call(&self, req: Req) -> Self::Future {
269+
self.service.call(req)
270+
}
271+
}
272+
}

0 commit comments

Comments
 (0)