-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add alloc::rc::UniqueRc
#111849
Add alloc::rc::UniqueRc
#111849
Conversation
(rustbot has picked a reviewer for you, use r? to override) |
This comment has been minimized.
This comment has been minimized.
@rustbot author There are a few issues that need fixed before this is ready for review. |
This comment has been minimized.
This comment has been minimized.
to me, something named what you have here seems more like a |
With the |
That's indeed what I meant in rust-lang/libs-team#90 (comment) |
Ah, I see what you mean. I misinterpreted the original suggestion.
So with struct Cyclic {
this: Weak<Cyclic>,
}
let mut cycle = UniqueRc::new(Cyclic { this: Weak::new() };
cycle.this = UniqueRc::weak(cycle);
// convert to a normal `Rc`
let rc = UniqueRc::into_rc(cycle); I'm curious why the Libs API team prefers this approach? I personally find something like One nice property I see for I appreciate the discussion here! One thing this has pointed out to me is that it's actually possible to implement this without depending on any internal details of |
for |
Oh, good point. Thanks for pointing that out! |
I updated the PR to implement I also changed all the functions to not have a self, since the pattern for Finally, I renamed |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
I like your previous approach more. It is simpler to use |
imho both APIs are useful, both the |
|
I think this is ready now. It's tempting to add @rustbot ready |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementation looks fine to me. I think the ACP looks like it was positively received, so I'm inclined to say we should file a tracking issue and I can approve this PR -- the naming of the type feels unfortunate as mentioned in my comment, but I wouldn't block this over that.
I opened #112566 as the tracking issue for this feature. |
@rustbot ready |
Please check API consistency with the Rust-for-Linux UniqueArc https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/sync/arc.rs#L502-L578 |
imho API consistency with |
@rustbot author r=me with commits squashed |
I think we can revise API as needed in future PRs since this is unstable (e.g., for renaming or for kernel API consistencies). |
I added this under open questions in the tracking issue (#112566). Thanks for pointing this out to me, it's good to know of another use case for something like this. |
This is an `Rc` that is guaranteed to only have one strong reference. Because it is uniquely owned, it can safely implement `DerefMut`, which allows programs to have an initialization phase where structures inside the `Rc` can be mutated. The `UniqueRc` can then be converted to a regular `Rc`, allowing sharing and but read-only access. During the "initialization phase," weak references can be created, but attempting to upgrade these will fail until the `UniqueRc` has been converted to a regular `Rc`. This feature can be useful to create cyclic data structures. This API is an implementation based on the feedback provided to the ACP at rust-lang/libs-team#90.
☀️ Test successful - checks-actions |
Finished benchmarking commit (14803bd): comparison URL. Overall result: no relevant changes - no action needed@rustbot label: -perf-regression Instruction countThis benchmark run did not return any relevant results for this metric. Max RSS (memory usage)ResultsThis is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeThis benchmark run did not return any relevant results for this metric. Bootstrap: 657.668s -> 655.501s (-0.33%) |
/// me: Weak::new(), | ||
/// }); | ||
/// rc.me = UniqueRc::downgrade(&rc); | ||
/// Some(UniqueRc::into_rc(rc)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does create_gadget
return an Option
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example evolved a bit from my first version of it. Originally I wanted to show that you could use this to initialize a circular data structure while the initialization phase is fallible, which is something that the existing new_cyclic
function doesn't handle well. Unfortunately, there's no fallibility left in this example so the Option
is probably more of a distraction.
This PR implements
UniqueRc
as described in rust-lang/libs-team#90.I've tried to stick to the API proposed there, incorporating the feedback from the ACP review. For now I've just implemented
UniqueRc
, but we'll wantUniqueArc
as well. I wanted to get feedback on this implementation first since theUniqueArc
version should be mostly a copy/paste/rename job.