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 fn new(ptr: *mut T) -> Self { Self(NonNull::new(ptr).expect("Did not expect pointer to be null")) } 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) } }