Skip to content

Does Server have to be 'static? #2144

@djc

Description

@djc

I'm wondering if it could be okay for Server to grow a lifetime parameter, reflecting the inner MakeServiceRef. I can't see why this is not allowed:

pub async fn run<'a, A>(addr: &'a SocketAddr, app: &'a A) -> Result<(), hyper::Error>
    where
        A: Application<RequestBody = Body, ResponseBody = Body> + Send + Sync,
    {
        Server::bind(addr)
            .serve(make_service_fn(move |_| {
                async {
                    Ok::<_, Infallible>(service_fn(move |req| {
                        let cx = Context::new(app, req);
                        A::handle(cx).map(Ok::<_, Infallible>)
                    }))
                }
            }))
            .await
    }

errors with:

error: cannot infer an appropriate lifetime
  --> mendes/src/lib.rs:37:51
   |
37 |     pub async fn run<'a, A>(addr: &'a SocketAddr, app: &'a A) -> Result<(), hyper::Error>
   |                                                   ^^^ ...but this borrow...
...
42 |             .serve(make_service_fn(move |_| {
   |              ----- this return type evaluates to the `'static` lifetime...
   |
note: ...can't outlive the lifetime `'a` as defined on the function body at 37:22
  --> mendes/src/lib.rs:37:22
   |
37 |     pub async fn run<'a, A>(addr: &'a SocketAddr, app: &'a A) -> Result<(), hyper::Error>
   |                      ^^

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions