Skip to content

Commit 60e9f84

Browse files
author
Robin Seitz
committed
refactor(service): change Service::call to take &self
change Service::call to take &self instead of &mut self. Because of this change, the trait bound in the service::util::service_fn and the trait bound in the impl for the ServiceFn struct were changed from FnMut to Fn. This change was decided on for the following reasons: - It prepares the way for async fn, since then the future only borrows &self, and thus a Service can concurrently handle multiple outstanding requests at once. - It's clearer that Services can likely be cloned - To share state across clones you generally need Arc<Mutex<_>> that means you're not really using the &mut self and could do with a &self This commit closses issue: hyperium#3040 BREAKING CHANGE: The Service::call function no longer takes a mutable reference to self. The FnMut trait bound on the service::util::service_fn function and the trait bound on the impl for the ServiceFn struct were changed from FnMut to Fn
1 parent 16a921f commit 60e9f84

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

src/service/service.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,13 @@ pub trait Service<Request> {
2828
type Future: Future<Output = Result<Self::Response, Self::Error>>;
2929

3030
/// Process the request and return the response asynchronously.
31-
fn call(&mut self, req: Request) -> Self::Future;
31+
/// call takes a &self instead of a mut &self because:
32+
/// - It prepares the way for async fn,
33+
/// since then the future only borrows &self, and thus a Service can concurrently handle
34+
/// multiple outstanding requests at once.
35+
/// - It's clearer that Services can likely be cloned
36+
/// - To share state across clones you generally need Arc<Mutex<_>>
37+
/// that means you're not really using the &mut self and could do with a &self
38+
/// To see the discussion on this see: https://github.com/hyperium/hyper/issues/3040
39+
fn call(&self, req: Request) -> Self::Future;
3240
}

src/service/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{Request, Response};
2929
/// ```
3030
pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R>
3131
where
32-
F: FnMut(Request<R>) -> S,
32+
F: Fn(Request<R>) -> S,
3333
S: Future,
3434
{
3535
ServiceFn {
@@ -46,7 +46,7 @@ pub struct ServiceFn<F, R> {
4646

4747
impl<F, ReqBody, Ret, ResBody, E> Service<Request<ReqBody>> for ServiceFn<F, ReqBody>
4848
where
49-
F: FnMut(Request<ReqBody>) -> Ret,
49+
F: Fn(Request<ReqBody>) -> Ret,
5050
ReqBody: Body,
5151
Ret: Future<Output = Result<Response<ResBody>, E>>,
5252
E: Into<Box<dyn StdError + Send + Sync>>,
@@ -56,7 +56,7 @@ where
5656
type Error = E;
5757
type Future = Ret;
5858

59-
fn call(&mut self, req: Request<ReqBody>) -> Self::Future {
59+
fn call(&self, req: Request<ReqBody>) -> Self::Future {
6060
(self.f)(req)
6161
}
6262
}

0 commit comments

Comments
 (0)