diff --git a/src/praetor/mod.rs b/src/praetor/mod.rs index b470231..02b803c 100644 --- a/src/praetor/mod.rs +++ b/src/praetor/mod.rs @@ -4,6 +4,7 @@ mod util { marker::PhantomData, mem::ManuallyDrop, num::NonZero, + ops::{Deref, DerefMut}, ptr::NonNull, sync::atomic::{AtomicPtr, Ordering}, }; @@ -30,6 +31,41 @@ mod util { } } + #[repr(transparent)] + #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct SendPtr(NonNull); + + impl std::fmt::Pointer for SendPtr { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + as core::fmt::Pointer>::fmt(&self.0, f) + } + } + + unsafe impl Send for SendPtr {} + + impl Deref for SendPtr { + type Target = NonNull; + + fn deref(&self) -> &Self::Target { + &self.0 + } + } + + impl DerefMut for SendPtr { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } + } + + impl SendPtr { + pub fn new(ptr: *mut T) -> Option { + NonNull::new(ptr).map(Self) + } + pub unsafe fn new_unchecked(ptr: *mut T) -> Self { + unsafe { Self(NonNull::new_unchecked(ptr)) } + } + } + #[repr(transparent)] pub struct TaggedAtomicPtr(AtomicPtr<()>, PhantomData);