Hello there,
I had a function init_routes
which is quite long so I'll shortened it a bit:
pub fn init_routes() {
// POST /user
let create_user = warp::path("user")
.and(warp::path::end())
.and(warp::body::json())
.and_then(|user: User| {
user::create_user(&user)
.and_then(|token| Ok(warp::reply::json(&token)))
.or_else(|e| Err(warp::reject::custom(e.compat())))
});
let post_routes = warp::post2()
.and(create_user).recover(customize_error);
warp::serve(post_routes ).run(([127, 0, 0, 1], 3030));
}
// omiting customize_error
// main.rs
fn main() {
init_routes();
}
It works fine, now I'm introducing a parameter in init_routes
:
pub fn init_routes(db_client: &Client) {
// POST /user
let create_user = warp::path("user")
.and(warp::path::end())
.and(warp::body::json())
.and_then(|user: User| {
user::create_user(&user, &db_client)
.and_then(|token| Ok(warp::reply::json(&token)))
.or_else(|e| Err(warp::reject::custom(e.compat())))
});
let post_routes = warp::post2()
.and(create_user).recover(customize_error);
warp::serve(post_routes ).run(([127, 0, 0, 1], 3030));
}
// main.rs
fn main() {
let db_client = db::get_client(db::SERVER_ADDR);
init_routes(&db_client);
}
I'm getting:
error[E0621]: explicit lifetime required in the type of `db_client`
--> src\endpoints\routes.rs:225:5
|
16 | pub fn init_routes(db_client: &Client) {
| ------- help: add explicit lifetime `'static` to the type of `db_client`: `&'static redis::client::Client`
...
225 | warp::serve(routes).run(([127, 0, 0, 1], 3030));
| ^^^^^^^^^^^ lifetime `'static` required
error: aborting due to previous error
If I add 'static
:
error[E0597]: `db_client` does not live long enough
--> src\main.rs:13:17
|
13 | init_routes(&db_client);
| ------------^^^^^^^^^^-
| | |
| | borrowed value does not live long enough
| argument requires that `db_client` is borrowed for `'static`
14 | }
| - `db_client` dropped here while still borrowed
error[E0373]: closure may outlive the current function, but it borrows `db_client`, which is owned by the current function
--> src\endpoints\routes.rs:26:19
|
26 | .and_then(|user: User| {
| ^^^^^^^^^^^^ may outlive borrowed value `db_client`
27 | user::create_user(&user, &db_client)
| --------- `db_client` is borrowed here
|
note: function requires argument type to outlive `'static`
--> src\endpoints\routes.rs:23:23
|
23 | let create_user = warp::path("user")
| _______________________^
24 | | .and(warp::path::end())
25 | | .and(warp::body::json())
26 | | .and_then(|user: User| {
... |
29 | | .or_else(|e| Err(warp::reject::custom(e.compat())))
30 | | });
| |__________^
help: to force the closure to take ownership of `db_client` (and any other referenced variables), use the `move` keyword
|
26 | .and_then(move |user: User| {
| ^^^^^^^^^^^^^^^^^
I've fixed the second one by adding move
. But still have the first one, which is true, client is not of lifetime 'static
but I don't want it to be.
How can I solve this?