Is there a way to prevent Weak::new() from over allocating?

前端 未结 1 1122
夕颜
夕颜 2021-02-20 03:21

Follow-up of Why doesn't Weak::new() work when Rc::downgrade() does?

When attempting to implement Weak::new() in a way that would NOT require i

相关标签:
1条回答
  • 2021-02-20 03:50

    Yes, there is a way, and it was actually submitted to the standard library:

    • Arc: remove unused allocation from Weak::new()
    • Rc: remove unused allocation and fix segfault in Weak::new()

    This change makes it so that Weak::new() allocates no memory at all. Instead, it is created with a null pointer. The only things done with a Weak are trying to upgrade, cloning, and dropping, meaning there are very few places that the code actually needs to check if the pointer is null.

    0 讨论(0)
提交回复
热议问题