Skip to content

Commit

Permalink
feat(service): allow FnMut with service_fn
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Feb 14, 2019
1 parent 0c8f7d2 commit 877606d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/service/make_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ where
/// ```
pub fn make_service_fn<F, Ctx, Ret>(f: F) -> MakeServiceFn<F>
where
F: Fn(&Ctx) -> Ret,
F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture,
{
MakeServiceFn {
Expand All @@ -130,7 +130,7 @@ pub struct MakeServiceFn<F> {

impl<'c, F, Ctx, Ret, ReqBody, ResBody> MakeService<&'c Ctx> for MakeServiceFn<F>
where
F: Fn(&Ctx) -> Ret,
F: FnMut(&Ctx) -> Ret,
Ret: IntoFuture,
Ret::Item: Service<ReqBody=ReqBody, ResBody=ResBody>,
Ret::Error: Into<Box<StdError + Send + Sync>>,
Expand Down
29 changes: 25 additions & 4 deletions src/service/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub trait Service {
/// ```
pub fn service_fn<F, R, S>(f: F) -> ServiceFn<F, R>
where
F: Fn(Request<R>) -> S,
F: FnMut(Request<R>) -> S,
S: IntoFuture,
{
ServiceFn {
Expand All @@ -75,7 +75,7 @@ where
/// ```
pub fn service_fn_ok<F, R, S>(f: F) -> ServiceFnOk<F, R>
where
F: Fn(Request<R>) -> Response<S>,
F: FnMut(Request<R>) -> Response<S>,
S: Payload,
{
ServiceFnOk {
Expand All @@ -92,7 +92,7 @@ pub struct ServiceFn<F, R> {

impl<F, ReqBody, Ret, ResBody> Service for ServiceFn<F, ReqBody>
where
F: Fn(Request<ReqBody>) -> Ret,
F: FnMut(Request<ReqBody>) -> Ret,
ReqBody: Payload,
Ret: IntoFuture<Item=Response<ResBody>>,
Ret::Error: Into<Box<StdError + Send + Sync>>,
Expand Down Expand Up @@ -133,7 +133,7 @@ pub struct ServiceFnOk<F, R> {

impl<F, ReqBody, ResBody> Service for ServiceFnOk<F, ReqBody>
where
F: Fn(Request<ReqBody>) -> Response<ResBody>,
F: FnMut(Request<ReqBody>) -> Response<ResBody>,
ReqBody: Payload,
ResBody: Payload,
{
Expand Down Expand Up @@ -163,3 +163,24 @@ impl<F, R> fmt::Debug for ServiceFnOk<F, R> {
.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);
}

0 comments on commit 877606d

Please sign in to comment.