Create an Rc<T> from an Arc<T>?

I could not find anything skimming through Arc in std::sync - Rust

I have an Arc<T> . Without cloning the T itself, is there a way to produce an Rc<T> ?

EDIT: Some context. I have some single threaded functions that take Rc<T> as argument. I am now writing some multi threaded code, which is passing Arc<T>'s around.

If the Arc is unique you could move the value out and into an Rc.

if let Ok(inner) = Arc::try_unwrap(arc) {
    let rc = Rc::new(inner);
}

Definitely not unique. Multiple threads have Arcs to it.

If the Arc is not unique, the conversion is unsound, because an Rc's ref counts are not atomic. So when the Rc was cloned or dropped you would create a data race with other threads, even though the Rc itself did not cross a thread boundary.

4 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.