How can I get the inner type of an Arc<Mutex<T>>

i have a struct that looks like this

pub struct Dog {
    names: HashSet<String>,
}

impl Dog {
    pub async fn new() -> Self {
        let set = Arc::new(Mutex::new(HashSet::new()));
        
        // do some async ops
        // wait for async ops to finish
        
        Self {
            // How can i convert the local set variable to HashSet?
            names: ..
        }
    }
}

The Arc type provides a try_unwrap method, that will take out the value inside, however note that this can fail if there is more than one clone of the Arc. Once you have unwrapped the Arc, you can use Mutex::into_inner.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.