Skip to content

Add a RefCell-free way to make cycles in Rc and Arc #14928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

gereeter
Copy link
Contributor

We currently tell people to use Weak to break cycles, but only provide two ways to do so, both unsavory: unsafe and RefCell. Additionally, there is nothing built into either of those mechanisms that prevents creating an Rc cycle. This PR fixes that.

Incidentally, I haven't seen any convincing uses for reference counted cycles, so I have no idea if this is the ideal API. However, it is definitely better than what is currently in place.

@thestinger
Copy link
Contributor

#14908 would provide the ability to mutate an Rc while it still has a single owner

@gereeter
Copy link
Contributor Author

That isn't sufficient - to create a Weak pointer to store cyclicly in a Rc pointer, you necessarily make the Rc pointer non-unique.

@alexcrichton
Copy link
Member

I believe that as-implemented this implementation will leak the FooInner structures if op fails. The weak reference count is 2, and there's only one actual weak reference anywhere, so when it gets destroyed via unwinding the reference count will drop to 1, not freeing the box. Certainly an problem which can be overcome, however! (make sure you add a test as well!)

@aturon
Copy link
Member

aturon commented Jun 23, 2014

@gereeter Slick.

This is probably obvious, but without using a RefCell somewhere, the cycle will be immutable. But this pattern might be useful for building persistent data structures with internal, cyclic aliasing.

@alexcrichton
Copy link
Member

Closing due to inactivity, but feel free to reopen with an update for failure!

@gereeter gereeter deleted the cyclic-rc branch December 17, 2015 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants