-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement RcUninit (#112566) #140640
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
Open
kstrafe
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
kstrafe:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Implement RcUninit (#112566) #140640
+127
−1
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RcUninit was discussed in rust-lang/libs-team#90 https://internals.rust-lang.org/t/an-alternative-to-rc-new-cyclic/22849/6 rust-lang#112566 RcUninit allows the user to construct cyclic data structures without using `Rc::new_cyclic`, which allows cyclic constructions across await points. It also allows you to create long linked lists without overflowing the stack. This is an alternative to `UniqueRc`. While `UniqueRc` does allow for cyclic data structures to be created, it must be done by mutating the UniqueRc. Mutation is prone to creating reference cycles. Construction-only assignment of fields, without any mutation to "set" the struct afterwards cannot generate reference cycles. It's also more cumbersome to work with. For instance, if we have objects A, B, and C, and we want these to connect as `A => B => C -> A` (`=>` being strong, `->` being weak), then we must do something along the following lines. let mut a_uniq = UniqueRc::new(A::new()); let a_weak = UniqueRc::downgrade(&a_uniq); let c = Rc::new(C::new(a_weak)); let b = Rc::new(B::new(c)); a_uniq.set_b(b); let a = a_uniq.into_rc(); To implement `A::set_b`, the field `A::b` must either be - `Option<Rc<B>>`: Requiring unwrap/clone for each access. - `MaybeUninit<Rc<B>>`: Requiring unsafe. - `Weak<B>`: Requiring upgrade for every access. The above also makes it easier to make mistakes in more complex programs where we don't have the full picture. It is not hard to change the above into `Rc<RefCell<A>>`, and then provide this pointer to `C`, which would cause a reference cycle to be created once `a.borrow_mut().set_b(b)` gets called. On the other hand RcUninit doesn't have this problem, since initialization is deferred. The equivalent would look like the following. let a_uninit = RcUninit::new(); let b_uninit = RcUninit::new(); let c_uninit = RcUninit::new(); let c = c_uninit.init(C::new(a_uninit.weak())); let b = b_uninit.init(B::new(c)); let a = a_uninit.init(b); This creates the structure (A => B => C -> A)
The job Click to see the possible cause of the failure (guessed by this bot)
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
RcUninit allows the user to construct cyclic data structures without using
Rc::new_cyclic
, which allows cyclic constructions across await points. It also allows you to create long linked lists without overflowing the stack.This is an alternative to
UniqueRc
. WhileUniqueRc
does allow for cyclic data structures to be created, it must be done by mutating the UniqueRc. Mutation is prone to creating reference cycles. Construction-only assignment of fields, without any mutation to "set" the struct afterwards cannot generate reference cycles. It's also more cumbersome to work with.For instance, if we have objects A, B, and C, and we want these to connect as
A => B => C -> A
(=>
being strong,->
being weak), then we must do something along the following lines.To implement
A::set_b
, the fieldA::b
must either beOption<Rc<B>>
: Requiring unwrap/clone for each access.MaybeUninit<Rc<B>>
: Requiring unsafe.Weak<B>
: Requiring upgrade for every access.The above also makes it easier to make mistakes in more complex programs where we don't have the full picture. It is not hard to change the above into
Rc<RefCell<A>>
, and then provide this pointer toC
, which would cause a reference cycle to be created oncea.borrow_mut().set_b(b)
gets called.On the other hand RcUninit doesn't have this problem, since initialization is deferred. The equivalent would look like the following.
This creates the structure (A => B => C -> A)
Tracking Issue
#112566
RcUninit was discussed in
rust-lang/libs-team#90
https://internals.rust-lang.org/t/an-alternative-to-rc-new-cyclic/22849/6
#112566