Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Default for WeakPtr? #77

Open
raphlinus opened this issue Feb 13, 2019 · 1 comment
Open

Implement Default for WeakPtr? #77

raphlinus opened this issue Feb 13, 2019 · 1 comment

Comments

@raphlinus
Copy link

In the code I'm writing, it would be useful to have Default on WeakPtr so I don't need to wrap it in an extra layer of Option. This would make it more like std::rc::Weak and I think presents no problem because the null pointer is valid.

Happy to send a PR, I'm filing as an issue first to verify that it would be wanted, and in case there's a problem with the idea I'm missing.

@madsmtm
Copy link

madsmtm commented Jun 14, 2021

alloc::rc::Weak is actually core::ptr::NonNull internally - they use dangling pointers to create a Default implementation.

But yeah, WeakPtr could probably have a Default impl of WeakPtr::new(core::ptr::null_mut()), but I don't think we want this, since load would now return null in two cases: If the object was deallocated or if it was created from a null pointer - that is, you can't distinguish these two cases, which makes for a bad API IMO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants