While learning Rust, and trying to work through an example wherein a database Db
is required to be shared across threads and functions.
I have wrapped the database (a struct) in an Arc
, and within the struct, protected the member with a Mutex
:
pub struct Db{
pub db: Mutex<BTreeMap<String, String>>,
}
However, I get the error when attempting to update Db
via an associated method:
help: trait
DerefMut
is required to modify through a dereference, but it is not implemented forArc<Db>
I have seen this SO post, but instead of Arc::new(Mutex::new(Db))
, I only pass Arc::new(Db)
with the understanding that inner member of the database is mutex-protected.
Question How do I indicate to the compiler that inner data member of Db
is access protected in order to successfully build Link To Minimal Example ? If this were possible, would this be a recommended approach?
Other Information
rustc 1.65.0 (897e37553 2022-11-02)