How do I pass a mutable State<T> across functions? #2707
Replies: 1 comment
-
Without knowing what a Instead, all mutable sharing of data occurs through shared immutable references, which are read-only and thus allowed to alias across threads, which control access to mutable references to the underlying data. In other words, to get two or more Connecting this to Rocket: any route handler can be executed in any thread at any time, including in parallel and/or concurrently. As such, obtaining an use rocket::tokio::sync::Mutex;
type MyData = Mutex<Data>;
#[get("/")]
fn shared(state: &State<MyData>) {
let mut mutable = state.lock().await;
*mutable = new_data();
}
#[launch]
fn rocket() -> _ {
rocket::build()
.manage(MyData::new())
.mount("/", routes![shared])
} |
Beta Was this translation helpful? Give feedback.
-
I have this simple piece of code right here that I call which accepts the url of the game lobby, and 2 moves, p and a.
I've done unit tests and move_rocks works as it should, however the State does not update between these functions, neither may I pass state as mutable without it complaining. If I try to pass "shared" as mutable I get this:
So I guess mut for state is not implemented yet?
This is the one that doesn't work properly:
Here is the move_rocks function, not that important but included it just in case. I also translated the comments for clarity.
This is the unit test I used on move_rocks, it allowed me to throw in mut tho 🤔
Would love to get a proper way to do this, been struggling with this for a few days now.
Thank you in advance.
Beta Was this translation helpful? Give feedback.
All reactions