Skip to content

Commit 4e6d88d

Browse files
committed
improve boxed service docs
1 parent ef206f4 commit 4e6d88d

File tree

4 files changed

+44
-39
lines changed

4 files changed

+44
-39
lines changed

actix-service/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ path = "src/lib.rs"
2222

2323
[dependencies]
2424
futures-core = { version = "0.3.7", default-features = false }
25+
paste = "1"
2526
pin-project-lite = "0.2"
2627

2728
[dev-dependencies]
2829
actix-rt = "2.0.0"
30+
actix-utils = "3.0.0-beta.4"
2931
futures-util = { version = "0.3.7", default-features = false }

actix-service/src/boxed.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,30 @@
33
use alloc::{boxed::Box, rc::Rc};
44
use core::{future::Future, pin::Pin};
55

6+
use paste::paste;
7+
68
use crate::{Service, ServiceFactory};
79

8-
/// A boxed future without a Send bound or lifetime parameters.
10+
/// A boxed future with no send bound or lifetime parameters.
911
pub type BoxFuture<T> = Pin<Box<dyn Future<Output = T>>>;
1012

1113
macro_rules! service_object {
1214
($name: ident, $type: tt, $fn_name: ident) => {
13-
/// Type alias for service trait object.
14-
pub type $name<Req, Res, Err> = $type<
15-
dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>,
16-
>;
17-
18-
/// Create service trait object.
19-
pub fn $fn_name<S, Req>(service: S) -> $name<Req, S::Response, S::Error>
20-
where
21-
S: Service<Req> + 'static,
22-
Req: 'static,
23-
S::Future: 'static,
24-
{
25-
$type::new(ServiceWrapper::new(service))
15+
paste! {
16+
#[doc = "Type alias for service trait object using `" $type "`."]
17+
pub type $name<Req, Res, Err> = $type<
18+
dyn Service<Req, Response = Res, Error = Err, Future = BoxFuture<Result<Res, Err>>>,
19+
>;
20+
21+
#[doc = "Wraps service as a trait object using [`" $name "`]."]
22+
pub fn $fn_name<S, Req>(service: S) -> $name<Req, S::Response, S::Error>
23+
where
24+
S: Service<Req> + 'static,
25+
Req: 'static,
26+
S::Future: 'static,
27+
{
28+
$type::new(ServiceWrapper::new(service))
29+
}
2630
}
2731
};
2832
}
@@ -56,10 +60,10 @@ where
5660
}
5761
}
5862

59-
/// Wrapper for a service factory trait object that will produce a boxed trait object service.
63+
/// Wrapper for a service factory that will map it's services to boxed trait object services.
6064
pub struct BoxServiceFactory<Cfg, Req, Res, Err, InitErr>(Inner<Cfg, Req, Res, Err, InitErr>);
6165

62-
/// Create service factory trait object.
66+
/// Wraps a service factory that returns service trait objects.
6367
pub fn factory<SF, Req>(
6468
factory: SF,
6569
) -> BoxServiceFactory<SF::Config, Req, SF::Response, SF::Error, SF::InitError>

actix-service/src/lib.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use self::ready::{err, ok, ready, Ready};
7373
/// impl Service<u8> for MyService {
7474
/// type Response = u64;
7575
/// type Error = MyError;
76-
/// type Future = Pin<Box<Future<Output=Result<Self::Response, Self::Error>>>>;
76+
/// type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
7777
///
7878
/// fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { ... }
7979
///
@@ -82,7 +82,7 @@ use self::ready::{err, ok, ready, Ready};
8282
/// ```
8383
///
8484
/// Sometimes it is not necessary to implement the Service trait. For example, the above service
85-
/// could be rewritten as a simple function and passed to [fn_service](fn_service()).
85+
/// could be rewritten as a simple function and passed to [`fn_service`](fn_service()).
8686
///
8787
/// ```ignore
8888
/// async fn my_service(req: u8) -> Result<u64, MyError>;
@@ -102,13 +102,12 @@ pub trait Service<Req> {
102102

103103
/// Returns `Ready` when the service is able to process requests.
104104
///
105-
/// If the service is at capacity, then `Pending` is returned and the task
106-
/// is notified when the service becomes ready again. This function is
107-
/// expected to be called while on a task.
105+
/// If the service is at capacity, then `Pending` is returned and the task is notified when the
106+
/// service becomes ready again. This function is expected to be called while on a task.
108107
///
109-
/// This is a **best effort** implementation. False positives are permitted.
110-
/// It is permitted for the service to return `Ready` from a `poll_ready`
111-
/// call and the next invocation of `call` results in an error.
108+
/// This is a best effort implementation. False positives are permitted. It is permitted for
109+
/// the service to return `Ready` from a `poll_ready` call and the next invocation of `call`
110+
/// results in an error.
112111
///
113112
/// # Notes
114113
/// 1. `poll_ready` might be called on a different task to `call`.
@@ -117,25 +116,26 @@ pub trait Service<Req> {
117116

118117
/// Process the request and return the response asynchronously.
119118
///
120-
/// This function is expected to be callable off task. As such,
121-
/// implementations should take care to not call `poll_ready`. If the
122-
/// service is at capacity and the request is unable to be handled, the
123-
/// returned `Future` should resolve to an error.
119+
/// This function is expected to be callable off-task. As such, implementations of `call` should
120+
/// take care to not call `poll_ready`. If the service is at capacity and the request is unable
121+
/// to be handled, the returned `Future` should resolve to an error.
124122
///
125-
/// Calling `call` without calling `poll_ready` is permitted. The
126-
/// implementation must be resilient to this fact.
123+
/// Invoking `call` without first invoking `poll_ready` is permitted. Implementations must be
124+
/// resilient to this fact.
127125
fn call(&self, req: Req) -> Self::Future;
128126
}
129127

130128
/// Factory for creating `Service`s.
131129
///
132-
/// Acts as a service factory. This is useful for cases where new `Service`s
133-
/// must be produced. One case is a TCP server listener. The listener
134-
/// accepts new TCP streams, obtains a new `Service` using the
135-
/// `ServiceFactory` trait, and uses the new `Service` to process inbound
136-
/// requests on that new TCP stream.
130+
/// This is useful for cases where new `Service`s must be produced. One case is a TCP
131+
/// server listener: a listener accepts new connections, constructs a new `Service` for each using
132+
/// the `ServiceFactory` trait, and uses the new `Service` to process inbound requests on that new
133+
/// connection.
137134
///
138135
/// `Config` is a service factory configuration type.
136+
///
137+
/// Simple factories may be able to use [`fn_factory`] or [`fn_factory_with_config`] to
138+
/// reduce boilerplate.
139139
pub trait ServiceFactory<Req> {
140140
/// Responses given by the created services.
141141
type Response;

actix-service/src/transform.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,9 @@ where
222222

223223
#[cfg(test)]
224224
mod tests {
225-
use core::{
226-
future::{ready, Ready},
227-
time::Duration,
228-
};
225+
use core::time::Duration;
226+
227+
use actix_utils::future::{ready, Ready};
229228

230229
use super::*;
231230
use crate::Service;

0 commit comments

Comments
 (0)