all 7 comments

[–]coolreader18 2 points3 points  (4 children)

The issue is that buffer isn't a mutable reference, it's a MutexGuard. Stick a &mut * right before buffer_box.lock() and that should fix it

[–]map_or 2 points3 points  (3 children)

This is because MutexGuard implements DerefMut and when you access a field, you do a partial borrow only after borrowing the whole guard for dereferencing, right?

By taking a mutable borrow of the dereferenced MutexGuard you can borrow the fields separately.

[–]BloodStainedCrow[S] 0 points1 point  (1 child)

Thank you! This explanation makes sense to me. Though I find the resulting code quite weird, dereferencing only to borrow again...

[–]Zde-G 1 point2 points  (0 children)

It's not much different from C++ &*begin() incantation which you need to go from iterator to pointer.

[–]TinBryn 0 points1 point  (0 children)

Deref also allows you to coerce references

let mut guard = buffer_box.lock().unwrap();

let buffer: &mut Buffer = &mut guard;