-
Notifications
You must be signed in to change notification settings - Fork 22
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
struct Unique
: Copy more from std
and use for Rav1dPictureDataComponentInner::ptr
to make it Send + Sync
#1327
base: main
Are you sure you want to change the base?
Conversation
…he `Unique` from `CBox::from_c` to the API boundary.
…t is `Send + Sync`.
@@ -189,7 +192,7 @@ impl Rav1dPictureDataComponentInner { | |||
/// so it is sound to further subdivide it into disjoint `&mut`s. | |||
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.
@@ -0,0 +1,190 @@ | |||
//! Largely copied from [`rust-lang/rust/library/core/src/ptr/unique.rs`](https://github.com/rust-lang/rust/blob/e35364a521372ce682e4bd4a5850d97ea33b0eab/library/core/src/ptr/unique.rs#L58). |
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.
License? Copyright?
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.
Umm I don't know how to do that. What do I write?
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.
//! Largely copied from [`rust-lang/rust/library/core/src/ptr/unique.rs`](https://github.com/rust-lang/rust/blob/e35364a521372ce682e4bd4a5850d97ea33b0eab/library/core/src/ptr/unique.rs#L58). | |
//! Largely copied from [`rust-lang/rust/library/core/src/ptr/unique.rs`](https://github.com/rust-lang/rust/blob/e35364a521372ce682e4bd4a5850d97ea33b0eab/library/core/src/ptr/unique.rs#L58). | |
//! | |
//! Used under the MIT license <LICENSE-MIT> or <http://opensource.org/licenses/MIT>. | |
//! Copyright is retained by the original Rust contributors. | |
//! For full authorship information, see the Rust compiler version control history or | |
//! https://thanks.rust-lang.org |
To follow up on the discussion of "should |
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).