How can I deep copy an Arc?

Raw pointers would be another option but of course those come with their own caveats.

I keep refcount == 1 for quite a few reasons (I forget them lmao, I've made sure to keep refcount == 1 and didn't run in to any problems). I have the whole application working perfectly now, but there is a little performance penalty from make_mut's clone. Any way to avoid that (for example, "peel" the enum without modifying it?)

Maybe you want to traverse the data structure by reference, non-destructively? That's not difficult. Here's an algorithm that gets an arbitrary item out of an (infinite) linked list:

enum TheEnum {
    Node { data: i32, next: Arc<TheEnum> },
}

impl TheEnum {
    fn nth(&self, n: usize) -> i32 {
        let mut current = self;
        for _ in 0..n {
            let TheEnum::Node { next, .. } = current;
            current = &next;
        }
        let TheEnum::Node { data, .. } = *current;
        data
    }
}

Making it work for your data structures and not recurse forever is left as an exercise for the reader.

If this is close to what you're doing, I recommend Learning Rust With Entirely Too Many Linked Lists. Even if it's not that similar, I suggest reading it anyway, if you haven't already.

1 Like

facepalm
Yes, that is precisely what I want to do. Making it terminate at the "end" is quite easy, I have a "node" variant and an "end" variant of the Enum.

Thanks!