The values in those HashMap are used as "shared, request-scoped memory" and then deleted (using .remove(k)).
QUESTIONs
Since I don't care if two or more requests write/read the same entries at the same time (very unlikely) should I still use Mutex?
// Like this?
let store = Arc::new(Mutex::new(Store::new()))
// Or this?
pub fn new() -> Self {
Self {
players: Mutex::new(HashMap::new()),
team: Mutex::new(HashMap::new()),
}
}
Since the store is created when the application starts and is never destroyed, would it be useful to create a 'static one thus avoiding the use of Arc too?
// Something like:
const store: &mut Store = &mut Store::new();
You can't "not care". Race conditions are undefined behavior in Rust, so you have to care about them. You wouldn't be able to compile your code in the first place if it were racy (since mutable references are neither Send nor copiable) if you only use safe code. So yes, you have to use Mutex if you want a simple and safe solution.
You can create a static, but for the aforementioned reasons, you still have to wrap the maps in a Mutex. Since a static is effectively global, it may be trivially accessed by multiple threads at the same time, making allstatic muts unsafe, and requiring a Mutex for storing non-thread-safe types in a static.
Also note that const isn't the same as static. What you want here is a static, definitely not a const. Or better yet: why don't you just declare the maps in a local variable and pass it wherever it's needed? Global mutable state is bad for code readability.