Implement Weak::new_downgraded() (#30425) #30467

Merged
merged 7 commits into from Dec 30, 2015

Projects

None yet

7 participants

@shahn
Contributor
shahn commented Dec 18, 2015

This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.

@shahn shahn Implement Weak::new_downgraded() (#30425)
This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.
79d0235
@brson brson was assigned by rust-highfive Dec 18, 2015
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

Contributor
brson commented Dec 18, 2015

cc @rust-lang/libs Is everyone cool with this? Does it need an RFC?

@apasel422 apasel422 and 1 other commented on an outdated diff Dec 18, 2015
src/liballoc/rc.rs
+ /// ```
+ /// use std::rc::Weak;
+ ///
+ /// let five = Weak::new_downgraded();
+ /// ```
+
+ #[unstable(feature = "downgraded_weak",
+ reason = "recently added",
+ issue="30425")]
+ pub fn new_downgraded() -> Weak<T> {
+ unsafe {
+ Weak {
+ _ptr: Shared::new(Box::into_raw(box RcBox {
+ strong: Cell::new(0),
+ weak: Cell::new(1),
+ value: uninit(),
apasel422
apasel422 Dec 18, 2015 Member

Can this use core::mem::uninitialized instead?

shahn
shahn Dec 18, 2015 Contributor

fixing

Owner

I would probably personally call this Weak::new to stick closer to our existing conventions, but otherwise it seems harmless to me as well.

Member

Should Arc's Weak have this as well?

Contributor
shahn commented Dec 18, 2015

@alexcrichton The reason I went with a different name was to make it clear that this won't be usable for "normal" code. Happy to defer to you of course, extending PR.

@apasei422 good point! I'll extend the PR.

@alexcrichton alexcrichton added the T-libs label Dec 18, 2015
Contributor
shahn commented Dec 18, 2015

I'm thinking if everyone likes this branch I should probably rebase & squash it into a single commit?

@apasel422 apasel422 commented on an outdated diff Dec 18, 2015
src/liballoc/arc.rs
@@ -910,6 +911,36 @@ impl<T> From<T> for Arc<T> {
}
}
+impl<T> Weak<T> {
+ /// Constructs a new `Weak<T>` without an accompanying instance of T.
+ ///
+ /// This allocates memory for T, but does not initialize it. Calling
+ /// Weak<T>::upgrade() on the return value always gives None.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(downgraded_weak)]
+ ///
+ /// use std::sync::Arc;
+ ///
+ /// let five = Arc::new(5);
apasel422
apasel422 Dec 18, 2015 Member

This should be Weak::new.

@apasel422 apasel422 commented on an outdated diff Dec 18, 2015
src/liballoc/rc.rs
@@ -830,6 +830,38 @@ impl<T: ?Sized + fmt::Debug> fmt::Debug for Weak<T> {
}
}
+impl<T> Weak<T> {
+ /// Constructs a new `Weak<T>` without an accompanying instance of T.
+ ///
+ /// This allocates memory for T, but does not initialize it. Calling
+ /// Weak<T>::upgrade() on the return value always gives None.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(downgraded_weak)]
+ ///
+ /// use std::rc::Weak;
+ ///
+ /// let empty:Weak<i64> = Weak::new();
apasel422
apasel422 Dec 18, 2015 Member

There should be a space before Weak<i64>.

@apasel422 apasel422 and 1 other commented on an outdated diff Dec 18, 2015
src/liballoc/arc.rs
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// #![feature(downgraded_weak)]
+ ///
+ /// use std::sync::Arc;
+ ///
+ /// let five = Arc::new(5);
+ /// ```
+ #[unstable(feature = "downgraded_weak",
+ reason = "recently added",
+ issue = "30425")]
+ pub fn new() -> Weak<T> {
+ unsafe {
+ let x: Box<_> = box ArcInner {
apasel422
apasel422 Dec 18, 2015 Member

Is there a reason this Box is assigned to a local variable when the one for rc::Weak isn't?

shahn
shahn Dec 19, 2015 Contributor

I mirrored Arc::new(). But I can remove it.

@sfackler

Not sure how valuable an example that does nothing but call the method is.

Contributor
shahn commented Dec 19, 2015

I agree wrt the examples, I added them because they were there for Rc::new and Arc::new (where they also only assign a value). Should I update this?

shahn added some commits Dec 19, 2015
@shahn shahn Address review comments 7dd618f
@shahn shahn Have to use Weak instead of Arc in Weak::new() example
5b3bdaf
Contributor
shahn commented Dec 20, 2015

I think I addressed all review comments, thanks a bunch. Anything else for me to do except wait while a decision is made?

Contributor
brson commented Dec 28, 2015

@bors r+

Contributor
bors commented Dec 28, 2015

📌 Commit 5b3bdaf has been approved by brson

@Manishearth Manishearth added a commit to Manishearth/rust that referenced this pull request Dec 29, 2015
@Manishearth Manishearth Rollup merge of #30467 - shahn:master, r=brson
This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.
4d32400
Contributor
bors commented Dec 30, 2015

⌛️ Testing commit 5b3bdaf with merge cb50f54...

@bors bors added a commit that referenced this pull request Dec 30, 2015
@bors bors Auto merge of #30467 - shahn:master, r=brson
This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.
cb50f54
Contributor
bors commented Dec 30, 2015

💔 Test failed - auto-linux-64-nopt-t

Member

@bors retry

@bors bors added a commit that referenced this pull request Dec 30, 2015
@bors bors Auto merge of #30467 - shahn:master, r=brson
This adds a constructor for a Weak that can never be upgraded. These are
mostly useless, but for example are required when deserializing.
682cfc5
Contributor
bors commented Dec 30, 2015

⌛️ Testing commit 5b3bdaf with merge 682cfc5...

@bors bors merged commit 5b3bdaf into rust-lang:master Dec 30, 2015

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment