From 7975d2aa8fccb34dd53a7124617ff700c6ad4c9e Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sat, 6 Sep 2025 16:23:30 +0200 Subject: [PATCH] Remove Unique It is only used for AlignedBox which can just directly implement Send and Sync. Additionally make the Send and Sync impls more restrictive by requiring the inner type to be Send cq Sync, previously AlignedBox was always Send and Sync, which is not sound. --- src/common/aligned_box.rs | 24 ++++++++++++------------ src/common/mod.rs | 1 - src/common/unique.rs | 30 ------------------------------ 3 files changed, 12 insertions(+), 43 deletions(-) delete mode 100644 src/common/unique.rs diff --git a/src/common/aligned_box.rs b/src/common/aligned_box.rs index c88aef3235..65827c37c7 100644 --- a/src/common/aligned_box.rs +++ b/src/common/aligned_box.rs @@ -1,13 +1,15 @@ use core::alloc::{GlobalAlloc, Layout}; -use crate::{common::unique::Unique, memory::Enomem}; +use crate::memory::Enomem; // Necessary because GlobalAlloc::dealloc requires the layout to be the same, and therefore Box // cannot be used for increased alignment directly. -// TODO: move to common? pub struct AlignedBox { - inner: Unique, + inner: *mut T, } +unsafe impl Send for AlignedBox {} +unsafe impl Sync for AlignedBox {} + pub unsafe trait ValidForZero {} unsafe impl ValidForZero for [u8; N] {} unsafe impl ValidForZero for u8 {} @@ -43,9 +45,7 @@ impl AlignedBox { if ptr.is_null() { return Err(Enomem); } - Self { - inner: Unique::new_unchecked(ptr.cast()), - } + Self { inner: ptr.cast() } }) } } @@ -64,7 +64,7 @@ impl AlignedBox<[T], ALIGN> { return Err(Enomem); } Self { - inner: Unique::new_unchecked(core::ptr::slice_from_raw_parts_mut(ptr.cast(), len)), + inner: core::ptr::slice_from_raw_parts_mut(ptr.cast(), len), } }) } @@ -75,7 +75,7 @@ impl core::fmt::Debug for AlignedBox { write!( f, "[aligned box at {:p}, size {} alignment {}]", - self.inner.as_ptr(), + self.inner, self.layout().size(), self.layout().align() ) @@ -85,8 +85,8 @@ impl Drop for AlignedBox { fn drop(&mut self) { unsafe { let layout = self.layout(); - core::ptr::drop_in_place(self.inner.as_ptr()); - crate::ALLOCATOR.dealloc(self.inner.as_ptr().cast(), layout); + core::ptr::drop_in_place(self.inner); + crate::ALLOCATOR.dealloc(self.inner.cast(), layout); } } } @@ -94,12 +94,12 @@ impl core::ops::Deref for AlignedBox { type Target = T; fn deref(&self) -> &Self::Target { - unsafe { &*self.inner.as_ptr() } + unsafe { &*self.inner } } } impl core::ops::DerefMut for AlignedBox { fn deref_mut(&mut self) -> &mut Self::Target { - unsafe { &mut *self.inner.as_ptr() } + unsafe { &mut *self.inner } } } impl Clone for AlignedBox { diff --git a/src/common/mod.rs b/src/common/mod.rs index 9f2a70f77c..cb9fc3b5ea 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,7 +1,6 @@ pub mod aligned_box; #[macro_use] pub mod int_like; -pub mod unique; /// Debug macro, lifted from the std #[macro_export] diff --git a/src/common/unique.rs b/src/common/unique.rs deleted file mode 100644 index 49888e9990..0000000000 --- a/src/common/unique.rs +++ /dev/null @@ -1,30 +0,0 @@ -use core::{fmt, ptr::NonNull}; - -/// A small wrapper around NonNull that is Send + Sync, which is -/// only correct if the pointer is never accessed from multiple -/// locations across threads. Which is always, if the pointer is -/// unique. -pub struct Unique(NonNull); - -impl Copy for Unique {} -impl Clone for Unique { - fn clone(&self) -> Self { - *self - } -} -unsafe impl Send for Unique {} -unsafe impl Sync for Unique {} - -impl Unique { - pub unsafe fn new_unchecked(ptr: *mut T) -> Self { - Self(NonNull::new_unchecked(ptr)) - } - pub fn as_ptr(self) -> *mut T { - self.0.as_ptr() - } -} -impl fmt::Debug for Unique { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?}", self.0) - } -}