Lifetime cannot outlive the anonymous lifetime #1

impl ChunkedStreamingShuttle {


    pub fn get_service(&self) -> Resource {
        web::resource("/")
            .route(web::get().to(move |req| { self.outbound(req) })) // <--- #1
    }

    fn outbound(&self, req: HttpRequest) -> impl Responder {
        // ...
    }

}

I understand self's lifetime is not available within anonymous function. But I need outbound to be fired later.

Please how can I solve this issue?

Perhaps the following? impl items are usually elided to be 'static:

fn outbound<'a>(&'a self, req: HttpRequest) -> impl Responder + 'a {
    // ...
}

Thanks, but it does not work, same error.
As I understand, when outbound is being fired, self is not in that scope. Fixing the life time of return value might not work.

Could you point me in the direction of the name of the library you're using?

actix-web :slight_smile: actix = "0.8.3"

If I change self to 'static, it solves the compliation error. But get_service cannot be easily called.

pub fn get_service(&'static self) -> Resource {
        web::resource("/")
            .route(web::get().to(move |req| { self.outbound(req) })) // <--- #1
    }

Ah, okay. Here's essentially what's happening:

web::resource returns a Resource, which we can then .route with a Route. We get the Route from web::get and then call .to on it. The problem occurs because of the signature of .to:

pub fn to<F, T, R>(self, handler: F) -> Route
where 
    F: Factory<T, R> + 'static,
    T: FromRequest + 'static,
    R: Responder + 'static,

Therefore the error occurs because we try to essentially do the following:

pub fn get_service<'a>(&'a self) -> Resource {
    web::resource("/")
        .route(
            web::get().to::<[closure @ source.rs] + 'a, _, _>( move ... ))
        )
}

Which doesn't work, because the closure we pass in (Which I called [closure @ source.rs] + 'a in the simplification above) is not 'static like it is specified.

Unfortunately that's about as far as I can take from the docs without reading into actix-web examples and other similar issues.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.