How can I deep copy an Arc?


In an application I’m developing, I have a recursive enum, with the child of each enum “node” being wrapped in an Arc. To parse that enum, I “peel” it, removing the layers of an enum - so:

TheEnum {
Arc {
TheEnum {
Arc {
TheEnum {
data : 20,
would become (after 1 “peeling”):
TheEnum {
Arc {
TheEnum {
data : 20,

Obviously, the above is an extreme simplification of what I’m doing - TheEnum contains more information than just the arc of it’s child. Apparently Box does not work (I’ll try again, but I don’t think it does, I’ve tried it before). However, I have to “peel” the enum to different levels many times (for example, if the user inputs “2”, the outer 2 layers are removed, if the user inputs 3, the outer 3 are removed, etc). Repeatedly peeling to different degrees requires that you reset the enum that you’re going to peel back to the original (if you’ve already peeled the enum 3 times and the user requests it peeled only 2 times, you’re screwed, because each “peeling” already removes the information in the enum above it). However, trying to clone() the enum only increases the refcount, which then makes the try_unwrap function for Arc fail. Any ideas on how to deep clone a recursive enum containing an Arc?

If the above was confusing, yeah, it is, I couldn’t find a good way to explain it. If you’re confused just ask, I’ll do my best to answer.



If you have an arc: Arc<T> where T: Clone then you can do T::clone(&arc) to do a deep clone. If T: Copy simply dereference the arc like so *arc.


Note that the example contained an Arc inside, too. So I’d call that doing an inner clone, not a deep one.


Arc::make_mut() will clone if necessary.


Will it increase the refcount?


Or even better, is there a way to avoid cloneing the structure in the first place (for example, “peel” the enum without actually making any modifications to the original enum)?


Will the clone automatically clone the whole recursive datastructure (so that every single Arc underneath the node has a refcount of 1)?


make_mut gives you Arc with refcount == 1. It’ll clone and make a new Arc if refcount was > 1, or do nothing if recount was == 1.


Rust’s Clone is generally shallow, but it depends on data types. Clone of Arc only increases refcount. Clone of enum runs (shallow) clone on its content.


Is there any way to explicitly and always clone and return an Arc with refcount ==1?



You could, but that’s equivalent of not having Arc at all. If you don’t want refcount greater than 1, then use Box.


Aah, yes. Thank you! Probably has better performance, anyways.

If I do use box (I think it will work), is there a way to avoid actually cloning the enum each time I “peel” it to a different level?


Yes, if you dereference a Box you get the value in the Box.

fn main() {
    struct Foo;
    let boxed_thing = Box::new(Foo);
    let unboxed_thing: Foo = *boxed_thing; // type annotation is not necessary
    println!("{}", unboxed_thing);

Which works by compiler magic surrounding Box.


Yeah, the compiler magic has always annoyed me lmao.


Just remember that in pre 1.0 Rust, box was a special sigil. It’s always had magic treatment.


Using “box” is going along quite well right now, however, there’s one issue - is there an equivalent to a Weak pointer for Box? Right now I’m just returning a reference to the Box (I used to return a weak pointer to the arc), which is causing lifetime issues.


What’s supposed to happen when you upgrade this weak pointer?


er… that’s the issue lmao


Wow, it’s amazing how many errors occur if you switch from Arc to Box (at NO POINT does any Arc I use have more than 1 strong reference, yet it is NOT going smootly)


I wonder why do you place so much importance on keeping refcount == 1. Bumping of refcount is quite cheap — very likely much much cheaper than full clones of the content. There’s no extra cost to having higher refcount. Arc doesn’t even have into_inner(), so it shouldn’t make any difference to you.

And there’s no weak pointer for Box. If you need backreferences, then you’ll need Arc. Box can be referenced with regular Rust references, but these are strictly “strong”.