-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Closed
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.In the final comment period and will be merged soon unless new substantive objections are raised.
Description
When deserializing Weak, there's currently no clean way to handle the case that there's no strong reference anymore. Serializing doesn't have the same issue. There seem to be two solutions to this, either provide an explicit constructor that returns a Weak without using a strong reference to create it, or implementing Default for it that does the same. I prefer the former, as Default might be a bit surprising.
This would be a simple, non-backwards-incompatible change. Possible constructor name bikeshedding: Weak::new_downgraded()
Unstable APIs
Weak::new
Metadata
Metadata
Assignees
Labels
B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.In the final comment period and will be merged soon unless new substantive objections are raised.
Type
Projects
Milestone
Relationships
Development
Select code repository
Activity
Aatch commentedon Dec 17, 2015
/cc @rust-lang/libs
sfackler commentedon Dec 17, 2015
I feel like I'm missing a bit of context here - wouldn't the stored object immediately be freed since the strong reference count will be zero?
huonw commentedon Dec 17, 2015
@sfackler, yeah, the
Weak<T>
never actually contains aT
so it's always non-upgradable, but it allows deserialising things that happen to contain aWeak
(where it isn't unexpected for theWeak
to be invalid).Gankra commentedon Dec 17, 2015
Seems easy enough to add. Basically Option::None.
oli-obk commentedon Dec 17, 2015
Weak
could simply implementDefault
...shahn commentedon Dec 17, 2015
@oli-obk It could, but wouldn't it be a bit surprising that the Default for Weak is an unusable Weak?
sfackler commentedon Dec 17, 2015
Ah, got it.
Weak::new_empty()
(or whatever name) seems reasonable enough.Gankra commentedon Dec 18, 2015
Just to be clear: an empty Weak would still allocate memory. We're all in agreement that this is the desired semantic? The alternative would be adding a sentinel state to every weak pointer that needs to be checked on every access.
Veedrac commentedon Dec 18, 2015
If you care about allocating memory for empty
Weak
s, you could make them point to some thread-local instead. Just increment the thread-local's weak count on construction and make sure it starts with a count of 1 so it never gets deallocated.It sounds like a needless optimization for a rare case, and the aliasing is probably undefined behaviour without care, but it's workable if you really want it.
shahn commentedon Dec 18, 2015
Yes, it's fine to allocate the memory (you even want to do it to match the pre-serialization state). Having a constructor called new_* should make that clear, imo
Implement Weak::new_downgraded() (rust-lang#30425)
apasel422 commentedon Feb 6, 2016
This was implemented in #30467.
shahn commentedon Feb 6, 2016
I think this issue needs to stay open until a stabilization decision was made
14 remaining items
std: Stabilize APIs for the 1.10 release
std: Stabilize APIs for the 1.10 release