0

While learning Rust, and trying to work through an example wherein a database Db is required to be shared across threads and functions.

Link To Minimal Example

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 for Arc<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)

O oLo O
  • 23
  • 5
  • Well you need to `lock()`. Also, why do you use `try_lock()`? – Chayim Friedman Apr 22 '23 at 23:11
  • 1
    In the future, please embed your entire minimal example code into your question. – PitaJ Apr 22 '23 at 23:14
  • Please [edit] your question to include the [mre] *within* the question, only posting a link is not enough for various reasons amongst them is that the offline dumps of these questions are practically useless. – cafce25 Apr 23 '23 at 00:32

1 Answers1

1

Currently, you have

impl Db {
    pub fn add(&mut self, data: String) -> Option<String> { ... }
}

But, as you said, you protect the database inside a Mutex. So you don't need mut access:

impl Db {
    pub fn add(&self, data: String) -> Option<String> { ... }
}

See, &mut in Rust really means "exclusive" or "unique" access. Rust will enforce that &mut references are unique at compile time, which is why it gives you that error. Mutex only requires a "shared" (immutable) & reference, and enforces the same rules at runtime instead.

Removing that mut is literally all that's necessary to make your example work

PitaJ
  • 12,969
  • 6
  • 36
  • 55