3

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?

Will Squire
  • 6,127
  • 7
  • 45
  • 57

2 Answers2

1

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.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Will Squire
  • 6,127
  • 7
  • 45
  • 57
0

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 ...
Alex Moore-Niemi
  • 2,913
  • 2
  • 24
  • 22