Skip to content

Commit ede24d2

Browse files
authored
docs(service): add HttpService documentation (#3869)
this commit introduces some additional documentation to the `HttpService` trait, and the `Service` trait. notably, this commit introduces some intradoc links, so that rustdoc will render links to types like `http::Request` and `http::Response`, or to the `Body` trait. additionally, mention of `hyper-util` is added to the `Service` trait, to direct users to the glue implementations that they will likely need to interact with e.g. `tower`. Signed-off-by: katelyn martin <[email protected]>
1 parent c449528 commit ede24d2

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

src/service/http.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,32 @@ use crate::body::Body;
55
use crate::service::service::Service;
66
use crate::{Request, Response};
77

8-
/// An asynchronous function from `Request` to `Response`.
8+
/// An asynchronous function from [`Request`] to [`Response`].
9+
///
10+
/// This is a *sealed* trait, meaning that it can not be implemented directly. Rather, it is an
11+
/// alias for [`Service`]s that accept a [`Request`] and return a [`Future`] that resolves to a
12+
/// [`Response`]. External callers should implement [`Service`] instead.
13+
///
14+
/// Rather than being generic over the request and response, this trait is generic across the
15+
/// request [`Body`] and response [`Body`].
16+
///
17+
/// See the crate-level [`service`][crate::service] documentation for more information.
18+
///
19+
/// See [`Service`] for more information.
920
pub trait HttpService<ReqBody>: sealed::Sealed<ReqBody> {
10-
/// The `Body` body of the `http::Response`.
21+
/// The [`Body`] body of the [`Response`].
1122
type ResBody: Body;
1223

13-
/// The error type that can occur within this `Service`.
24+
/// The error type that can occur within this [`Service`].
1425
///
15-
/// Note: Returning an `Error` to a hyper server will cause the connection
16-
/// to be abruptly aborted. In most cases, it is better to return a `Response`
17-
/// with a 4xx or 5xx status code.
26+
/// Note: Returning an `Error` to a hyper server, the behavior depends on the protocol. In
27+
/// most cases, hyper will cause the connection to be abruptly aborted. In most cases, it is
28+
/// better to return a `Response` with a 4xx or 5xx status code.
29+
///
30+
/// See [`Service::Error`] for more information.
1831
type Error: Into<Box<dyn StdError + Send + Sync>>;
1932

20-
/// The `Future` returned by this `Service`.
33+
/// The [`Future`] returned by this [`Service`].
2134
type Future: Future<Output = Result<Response<Self::ResBody>, Self::Error>>;
2235

2336
#[doc(hidden)]

src/service/service.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,26 @@ use std::future::Future;
99
/// # Functional
1010
///
1111
/// A `Service` is a function of a `Request`. It immediately returns a
12-
/// `Future` representing the eventual completion of processing the
12+
/// [`Future`] representing the eventual completion of processing the
1313
/// request. The actual request processing may happen at any time in the
1414
/// future, on any thread or executor. The processing may depend on calling
1515
/// other services. At some point in the future, the processing will complete,
16-
/// and the `Future` will resolve to a response or error.
16+
/// and the [`Future`] will resolve to a response or an error.
1717
///
1818
/// At a high level, the `Service::call` function represents an RPC request. The
1919
/// `Service` value can be a server or a client.
20+
///
21+
/// # Utilities
22+
///
23+
/// The [`hyper-util`][util] crate provides facilities to bridge this trait to
24+
/// other libraries, such as [`tower`][tower], which might provide their
25+
/// own `Service` variants.
26+
///
27+
/// See [`hyper_util::service`][util-service] for more information.
28+
///
29+
/// [tower]: https://docs.rs/tower
30+
/// [util]: https://docs.rs/hyper-util
31+
/// [util-service]: https://docs.rs/hyper-util/latest/hyper_util/service/index.html
2032
pub trait Service<Request> {
2133
/// Responses given by the service.
2234
type Response;
@@ -37,7 +49,7 @@ pub trait Service<Request> {
3749
/// - It prepares the way for async fn,
3850
/// since then the future only borrows `&self`, and thus a Service can concurrently handle
3951
/// multiple outstanding requests at once.
40-
/// - It's clearer that Services can likely be cloned
52+
/// - It's clearer that Services can likely be cloned.
4153
/// - To share state across clones, you generally need `Arc<Mutex<_>>`
4254
/// That means you're not really using the `&mut self` and could do with a `&self`.
4355
/// The discussion on this is here: <https://github.com/hyperium/hyper/issues/3040>

0 commit comments

Comments
 (0)