Will Squire
Will Squire

Reputation: 6595

Rocket's State errors with "Attempted to retrieve unmanaged state" when using an explicit lifetime?

When using Rocket's State with omitted lifetimes then a request to the route is handled ok:

#[post("/foo")]
pub fn foo_handler(db: State<Db>) {
    // ...
}

However, if explicit lifetimes are provided then Rocket errors on requests with Attempted to retrieve unmanaged state!:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<&'a Db>) {
    // ...
}

There's either something the compiler isn't picking up here or Rocket avoids a safety check, as this compiles ok without any error or warnings. Any ideas?

Upvotes: 3

Views: 1657

Answers (2)

Alex Moore-Niemi
Alex Moore-Niemi

Reputation: 3382

I found this error resulted from failing to call unwrap() on the value I was initializing to be used in the State.

let index = load().unwrap(); // <-- without unwrap, compiled but failed on request
rocket::ignite()
  .manage(index) // normal mount and so on here
... etc ...

Upvotes: 0

Will Squire
Will Squire

Reputation: 6595

This seems to be the way to achieve the required result:

#[post("/foo")]
pub fn foo_handler<'a>(db: State<'a, Db>) {
  // ...
}

A example helped in Rocket's State docs. I'd expect an error to be thrown for the above implementations though, as it's valid syntax.

Upvotes: 1

Related Questions