You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tracking issue for downgraded_weak (No way to construct std::rc::Weak without a strong reference) #30425
B-unstableBlocker: Implemented in the nightly compiler and unstable.T-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
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()
@sfackler, yeah, the Weak<T> never actually contains a T so it's always non-upgradable, but it allows deserialising things that happen to contain a Weak (where it isn't unexpected for the Weak to be invalid).
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.
If you care about allocating memory for empty Weaks, 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.
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
B-unstableBlocker: Implemented in the nightly compiler and unstable.T-libs-api[DEPRECATED; DO NOT USE]final-comment-periodIn the final comment period and will be merged soon unless new substantive objections are raised.
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 aTso it's always non-upgradable, but it allows deserialising things that happen to contain aWeak(where it isn't unexpected for theWeakto be invalid).Gankra commentedon Dec 17, 2015
Seems easy enough to add. Basically Option::None.
oli-obk commentedon Dec 17, 2015
Weakcould 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
Weaks, 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
12 remaining items
std: Stabilize APIs for the 1.10 release
std: Stabilize APIs for the 1.10 release