How do you destructure a reference to Arc?


#1

Hi everyone,

I have a problem trying to destructure a reference to an Arc:

fn thread_method (queue: &Arc<(Mutex<VecDeque<u8>>, Condvar)>) {
    let &(ref l, ref cv) = &*queue;

The compiler gives me the following error:

src/main.rs:51:10: 51:25 error: mismatched types:
 expected `alloc::arc::Arc<(std::sync::mutex::Mutex<collections::vec_deque::VecDeque<u8>>, std::sync::condvar::Condvar)>`,
    found `(_, _)`
(expected struct `alloc::arc::Arc`,
    found tuple) [E0308]
src/main.rs:51     let &(ref l, ref cv) = &*queue;

Do you know how I have to destructure the contents of the Arc?

Thank you very much


#2

Nevermind, I’ve found it: &**queue. I didn’t know you had double pointers in Rust.


#3

For that, the first dereference (*) is turning &Arc into Arc, and the second dereference is turning Arc into (Mutex<VecDeque<u8>>, Condvar). It isn’t exactly a ‘double pointer’ per se, but rather a reference to something which implements Deref<>.


#4

Ok, thank you very much for the clarification.


#5

FYI, you don’t need the &. You should be able to say:

let (ref l, ref cv) = **queue;

#6

Good! Thanks. I had taken as example the code from https://doc.rust-lang.org/stable/std/sync/struct.Condvar.html