feat(service): allow FnMut with service_fn

This commit is contained in:
Sean McArthur
2019-02-14 11:31:09 -08:00
parent 0c8f7d2708
commit 877606d5c8
2 changed files with 27 additions and 6 deletions

View File

@@ -115,7 +115,7 @@ where
/// ``` /// ```
pub fn make_service_fn<F, Ctx, Ret>(f: F) -> MakeServiceFn<F> pub fn make_service_fn<F, Ctx, Ret>(f: F) -> MakeServiceFn<F>
where where
F: Fn(&Ctx) -> Ret, F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture, Ret: IntoFuture,
{ {
MakeServiceFn { MakeServiceFn {
@@ -130,7 +130,7 @@ pub struct MakeServiceFn<F> {
impl<'c, F, Ctx, Ret, ReqBody, ResBody> MakeService<&'c Ctx> for MakeServiceFn<F> impl<'c, F, Ctx, Ret, ReqBody, ResBody> MakeService<&'c Ctx> for MakeServiceFn<F>
where where
F: Fn(&Ctx) -> Ret, F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture, Ret: IntoFuture,
Ret::Item: Service<ReqBody=ReqBody, ResBody=ResBody>, Ret::Item: Service<ReqBody=ReqBody, ResBody=ResBody>,
Ret::Error: Into<Box<StdError + Send + Sync>>, Ret::Error: Into<Box<StdError + Send + Sync>>,

View File

@@ -51,7 +51,7 @@ pub trait Service {
/// ``` /// ```
pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R> pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R>
where where
F: Fn(Request<R>) -> S, F: FnMut(Request<R>) -> S,
S: IntoFuture, S: IntoFuture,
{ {
ServiceFn { ServiceFn {
@@ -75,7 +75,7 @@ where
/// ``` /// ```
pub fn service_fn_ok<F, R, S>(f: F) -> ServiceFnOk<F, R> pub fn service_fn_ok<F, R, S>(f: F) -> ServiceFnOk<F, R>
where where
F: Fn(Request<R>) -> Response<S>, F: FnMut(Request<R>) -> Response<S>,
S: Payload, S: Payload,
{ {
ServiceFnOk { ServiceFnOk {
@@ -92,7 +92,7 @@ pub struct ServiceFn<F, R> {
impl<F, ReqBody, Ret, ResBody> Service for ServiceFn<F, ReqBody> impl<F, ReqBody, Ret, ResBody> Service for ServiceFn<F, ReqBody>
where where
F: Fn(Request<ReqBody>) -> Ret, F: FnMut(Request<ReqBody>) -> Ret,
ReqBody: Payload, ReqBody: Payload,
Ret: IntoFuture<Item=Response<ResBody>>, Ret: IntoFuture<Item=Response<ResBody>>,
Ret::Error: Into<Box<StdError + Send + Sync>>, Ret::Error: Into<Box<StdError + Send + Sync>>,
@@ -133,7 +133,7 @@ pub struct ServiceFnOk<F, R> {
impl<F, ReqBody, ResBody> Service for ServiceFnOk<F, ReqBody> impl<F, ReqBody, ResBody> Service for ServiceFnOk<F, ReqBody>
where where
F: Fn(Request<ReqBody>) -> Response<ResBody>, F: FnMut(Request<ReqBody>) -> Response<ResBody>,
ReqBody: Payload, ReqBody: Payload,
ResBody: Payload, ResBody: Payload,
{ {
@@ -163,3 +163,24 @@ impl<F, R> fmt::Debug for ServiceFnOk<F, R> {
.finish() .finish()
} }
} }
//#[cfg(test)]
fn _assert_fn_mut() {
fn assert_service<T: Service>(_t: &T) {}
let mut val = 0;
let svc = service_fn(move |_req: Request<::Body>| {
val += 1;
future::ok::<_, Never>(Response::new(::Body::empty()))
});
assert_service(&svc);
let svc = service_fn_ok(move |_req: Request<::Body>| {
val += 1;
Response::new(::Body::empty())
});
assert_service(&svc);
}