I'm having troubles with a managed State
with Rocket
. This state holds a Database connection and a collection of Cursors on that Database. Each one of theses have a reference on the Database.
One of the operations on that state require to create a new cursor on the Database and keep it for later use. Unfortunatly, I am stuck with a lifetime problem. Usually, I have no problem dealing with thoses, but right now, I'm out of ideas...
I have recreated the problem bellow in a short example.
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rocket::State;
struct Database;
impl Database {
fn create_cursor(&self) -> Cursor {
Cursor { database: self }
}
}
struct Cursor<'a> {
database: &'a Database
}
struct Controller<'a> {
database: Database,
cursors: Vec<Cursor<'a>>,
}
impl<'a> Controller<'a> {
fn do_work(&'a mut self) {
let cursor = self.database.create_cursor();
self.cursors.push(cursor)
}
}
fn main() {
let database = Database;
let controller = Controller { database, cursors: Vec::new() };
rocket::ignite()
.manage(controller)
.launch();
}
#[get("/")]
pub fn do_work_route(
mut controller: State<'static, Controller<'static>>
) -> &'static str {
controller.do_work();
"Something..."
}
error[E0621]: explicit lifetime required in the type of `__req`
--> src/main.rs:42:9
|
40 | #[get("/")]
| ----------- help: add explicit lifetime `'static` to the type of `__req`: `&'_b rocket::Request<'static>`
41 | pub fn do_work_route(
42 | mut controller: State<'static, Controller<'static>>
| ^^^^^^^^^^ lifetime `'static` required
Any lead would be appreciated. Meanwhile, I'll continue digging.
Thanks a lot!