This route, which simply sets a key-value in Redis, compiles and runs successfully:
#[post("/thing", data = "<msg>")]
async fn post_thing(mut redis_conn: Connection<Redis>, msg: Json<Thing<'_>>) -> Value {
redis::cmd("SET")
.arg(&[msg.name, msg.description])
.query_async::<_, ()>(&mut *redis_conn)
.await
.unwrap();
json!({ "name": msg.name, "description": msg.description })
}
I am attempting to implement FromRequest to expose my User type to certain routes. This involves retrieving from Redis the user id associated to a given session id. Inside the FromRequest implementation, state is accessed slightly differently. Rather mut redis_conn: Connection<Redis>
being a function parameter, it is retrieved via request.rocket().state::<Connection<Redis>>()
:
let redis_conn = match request.rocket().state::<Connection<Redis>>() {
Some(x) => x,
None => {
return rocket::outcome::Outcome::Error((
http::Status::InternalServerError,
UserGuardError::FailedToGetDatabaseConnection,
))
}
};
The problem is this — request.rocket().state::<Connection<Redis>>()
gives me an immutable reference to the state (a &Connection<Redis>
) rather than a copy of it. This prevents me from calling redis::cmd("GET").arg(...).query_async::<_, String>(&mut *redis_conn)
:
`redis_conn` is a `&` reference, so the data it refers to cannot be borrowed as mutable
I tried to copy Rocket State via *
but it does not implement Copy
. I expected this to work because my post_thing
route demonstrates copying such state.
come-at-me-wolf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.