Sane Handling of `Mutex>>>`

โš“ Rust    ๐Ÿ“… 2025-07-31    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 14      

surdeus

Warning

This post was published 125 days ago. The information described in this article may have changed.

Info

This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Sane Handling of `Mutex>>>`

Iโ€™m working with a bit of code where I want to store references between objects and weak back-references. At first I had it working with Rc and RefCell types, but now I realize Iโ€™m A) sending these objects across thread in the GUI, and B) going to eventually want to operate on the logical Vec<T> in parallel.

Enter Arc and Mutex, which Iโ€™ve effectively replaced for Rc and RefCell one-to-one.

Now Iโ€™m wondering if thereโ€™s a better way to do this while preserving the ability to mutate the Vec and also mutate the inner T objects. Iโ€™m sorta guessing Iโ€™m stuck with this, which if true, leaves me wondering if there are patterns I can use that make this less of a mess of unwrap or PoisonError handling code.

For example this Rc<RefCell> code:

assert_eq!(lot.records.borrow()[0].borrow().uuid, record.borrow().uuid); 

Turns into:

assert_eq!(
    lot.records.lock().unwrap()[0].lock().unwrap().uuid,
    record.lock().unwrap().uuid
); 

Iโ€™ve decided for the moment that propagating panics is probably the best choice, but Iโ€™m open to suggestions.

5 posts - 3 participants

Read full topic

๐Ÿท๏ธ Rust_feed