`ptr::Unique` should be a feature

I have a pull request draft that converts ptr_internals to an actual feature named unique. This makes it possible for Unique to be stabilized in the future.

But there was one thing I did not understand:
Why was it "internal" in the first place?

I think users can achieve the same optimization enabled by Unique with NonNull. This is what I did in my own crate:

#[repr(transparent)]
pub struct Unique<T: ?Sized>(NonNull<T>, PhantomData<T>);

unsafe impl<T: Send + ?Sized> Send for Unique<T> {}
unsafe impl<T: Sync + ?Sized> Sync for Unique<T> {}

But creating such an abstraction, when the standard library already has it, feels redundant (and the standard library might do it better). Why was it not a feature with a tracking issue? Was it because it did not have many use cases?

This was also brought up in #84184 as a side note, but I do not think many people noticed.

2 Likes

Originally, it was: Tracking issue for `NonZero`/`Unique`/`Shared` stabilization · Issue #27730 · rust-lang/rust · GitHub

At that time, we had two non-null pointer types, Unique<T> and Shared<T>, but ended up deciding that it was only necessary to stabilize a single type. See the comments there for more history.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.