-
Notifications
You must be signed in to change notification settings - Fork 60
struct Unique
: Copy more from std
and use for Rav1dPictureDataComponentInner::ptr
to make it Send + Sync
#1327
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
base: main
Are you sure you want to change the base?
Conversation
pub fn wrap_buf<BD: BitDepth>(buf: &mut [BD::Pixel], stride: usize) -> Self { | ||
let buf = AsBytes::as_bytes_mut(buf); | ||
let ptr = NonNull::new(buf.as_mut_ptr()).unwrap(); | ||
let ptr = Unique::from_ref_mut(buf).cast(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this unique in the wrap_buf case? We're borrowing a reference to this data, right? So should we really use unique here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A &mut
reference is always unique. So it can be &mut
or owned. core::ptr::Unique
impl
s From<&mut T>
even.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But once that constructor returns the borrow is over, unless there's a lifetime. So it's no longer unique? How do we ensure no where else has a pointer to the same thing?
NonNull::new(self.as_strided_byte_mut_ptr().cast()) | ||
let ptr = NonNull::new(self.as_strided_byte_mut_ptr()).unwrap(); | ||
// SAFETY: The `ptr` originally comes from a `Unique` in `Rav1dPictureDataComponentInner::ptr`. | ||
let ptr = unsafe { Unique::new(ptr) }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, how is this unique? Can this method return a NonNull instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because self.as_strided_byte_mut_ptr()
is derived from Rav1dPictureDataComponentInner::ptr
, which is a Unique
. I can say that more explicitly if that's clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does that mean there's two Uniques pointing to the same data? The original and the one into the middle?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.g. can I just call this twice and get two Unique
s? Doesn't that violate the invariants?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, yeah, you're right.
Why does core::ptr::Unique: Copy + Clone
then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No idea.
To follow up on the discussion of "should |
…he `Unique` from `CBox::from_c` to the API boundary.
…t is `Send + Sync`.
64390bb
to
5366e5a
Compare
Co-authored-by: Stephen Crane <sjc@immunant.com>
5366e5a
to
4369d09
Compare
We may be able to replace
SendSyncNonNull
withUnique
, but we can do that later. They have basically the same APIs, though slightly different safety invariants (uniqueness vs. thread-safe).