-
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
Tracking Issue for arc_new_cyclic #75861
Comments
Add Arc::new_cyclic Rework of rust-lang#72443 References rust-lang#75861 cc @Diggsey @RalfJung r? @KodrAus
@Dylan-DPC @KodrAus allow me a day to implement |
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
`impl Rc::new_cyclic` References rust-lang#75861 r? @Dylan-DPC
I think "Implement |
Is there a specific reason why the definitions of |
I need a version of The API would look something like the following: impl<T> Arc<T> {
pub fn try_new_cyclic<F, E>(data_fn: F) -> T) -> Result<Arc<T>, E>
where
F: FnOnce(&Weak<T>) -> Result<T, E>,
{
// ...
}
} |
It would be nice to have multiple arities of this function, i.e.
and ditto for 3, etc. The use case would be for when your type |
This comment has been minimized.
This comment has been minimized.
@Johannesd3 Thanks for that, it's a lot cleaner than what I got from experimenting myself. One thing I noticed when testing it though is that there is a bug with
To me this implies that after the function returns, it will be safe. (I know that's not technically what it says, but...) also:
When putting this to test, the upgrade fails: #[test]
fn arc_new_cyclic_deferred_upgrades_succeed() {
let mut foo = None;
Arc::new_cyclic(|weak_a| {
foo = Some(weak_a.clone());
(1,)
});
assert_eq!(*foo.unwrap().upgrade().unwrap(), (1,));
}
|
@philip-peterson That's because you did not keep the Arc alive, so it is destroyed after the function call. fix it by assigning the resulting Arc to some value: |
@ogoffart ah, thank you!! I created a third party crate to house the multi-arity versions of |
Are additional docs required for this feature? The documentation in std looks sufficient. The rustc guide says the reference must be updated, but I'm pretty sure that doesn't apply to library APIs. The unstable book has a stub entry, that might need a blurb on the feature but I'm not sure it matters for stabilization. Are there any other blockers for stabilization? |
With assistance from the Forum, I found that implementing try_new_cyclic appears pretty simple: https://users.rust-lang.org/t/implementing-an-rc-try-new-cyclic/62945. To echo @Thomasdezeeuw, should I submit a new issue / pull request for it? |
@chris-laplante |
How about |
What's a real world use case for try_new_cyclic where the fallible work can't be done before the new_cyclic call, possibly putting Weak::new() placeholders into the object? That way new_cyclic only needs to wire in the self references into an exclusively owned object. let mut obj = try_obj()?;
let rc = Rc::new_cyclic(move |this| {
obj.this = Weak::clone(this);
obj
}); |
@dtolnay I have a use case where a fallible The code is available here: With the call to And the intermediate type: |
How close is this to stabilization? |
Very close! The FCP just finished and I approved the stabilization PR just now. It'll be stable in Rust 1.60.0. |
Stabilize arc_new_cyclic This stabilizes feature `arc_new_cyclic` as the implementation has been merged for one year and there is no unresolved questions. The FCP is not started yet. Closes rust-lang#75861 . `@rustbot` label +T-libs-api
The feature gate for the issue is
#![feature(arc_new_cyclic)]
.Tracking the following API:
Steps
Arc::new_with()
function for constructing self-referential data structures #72443Arc::new_cyclic
(Add Arc::new_cyclic #75505)Rc::new_cyclic
(impl Rc::new_cyclic
#75994)Unresolved Questions
The text was updated successfully, but these errors were encountered: