Skip to content

Tracking issue for downgraded_weak (No way to construct std::rc::Weak without a strong reference) #30425

Closed

Description

@shahn

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

Activity

  1. added
    T-libs-api
    on Dec 17, 2015
  2. Aatch commented on Dec 17, 2015

    @Aatch
    Contributor

    /cc @rust-lang/libs

  3. sfackler commented on Dec 17, 2015

    @sfackler
    Member

    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?

  4. huonw commented on Dec 17, 2015

    @huonw
    Contributor

    @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).

  5. Gankra commented on Dec 17, 2015

    @Gankra
    Contributor

    Seems easy enough to add. Basically Option::None.

  6. oli-obk commented on Dec 17, 2015

    @oli-obk
    Contributor

    Weak could simply implement Default...

  7. shahn commented on Dec 17, 2015

    @shahn
    ContributorAuthor

    @oli-obk It could, but wouldn't it be a bit surprising that the Default for Weak is an unusable Weak?

  8. sfackler commented on Dec 17, 2015

    @sfackler
    Member

    Ah, got it. Weak::new_empty() (or whatever name) seems reasonable enough.

  9. Gankra commented on Dec 18, 2015

    @Gankra
    Contributor

    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.

  10. Veedrac commented on Dec 18, 2015

    @Veedrac
    Contributor

    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.

  11. shahn commented on Dec 18, 2015

    @shahn
    ContributorAuthor

    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

  12. apasel422 commented on Feb 6, 2016

    @apasel422
    Contributor

    This was implemented in #30467.

  13. shahn commented on Feb 6, 2016

    @shahn
    ContributorAuthor

    I think this issue needs to stay open until a stabilization decision was made

  14. 12 remaining items

  15. added a commit that references this issue on May 24, 2016
    cae91d7
  16. added a commit that references this issue on May 26, 2016
    a2141e2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-unstableT-libs-apifinal-comment-period

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions