rust-objc icon indicating copy to clipboard operation
rust-objc copied to clipboard

Implement Default for WeakPtr?

Open raphlinus opened this issue 5 years ago • 1 comments

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.

raphlinus avatar Feb 13 '19 20:02 raphlinus

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.

madsmtm avatar Jun 14 '21 16:06 madsmtm