diff --git a/src/lib.rs b/src/lib.rs index 6ffb28f642f..23340a713ce 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub use std::*; mod bench; mod table; +mod shim; pub mod hash_map; pub mod hash_set; @@ -18,66 +19,3 @@ trait Recover { fn take(&mut self, key: &Q) -> Option; fn replace(&mut self, key: Self::Key) -> Option; } - -mod shim { - use std::marker::PhantomData; - - pub struct NonZeroPtr(&'static T); - - impl NonZeroPtr { - pub unsafe fn new_unchecked(ptr: *mut T) -> Self { - NonZeroPtr(&*ptr) - } - pub fn as_ptr(&self) -> *mut T { - self.0 as *const T as *mut T - } - } - - pub struct Unique { - ptr: NonZeroPtr, - _marker: PhantomData, - } - - impl Unique { - pub unsafe fn new_unchecked(ptr: *mut T) -> Self { - Unique { - ptr: NonZeroPtr::new_unchecked(ptr), - _marker: PhantomData, - } - } - pub fn as_ptr(&self) -> *mut T { - self.ptr.as_ptr() - } - } - - unsafe impl Send for Unique { } - - unsafe impl Sync for Unique { } - - pub struct Shared { - ptr: NonZeroPtr, - _marker: PhantomData, - // force it to be !Send/!Sync - _marker2: PhantomData<*const u8>, - } - - impl Shared { - pub unsafe fn new_unchecked(ptr: *mut T) -> Self { - Shared { - ptr: NonZeroPtr::new_unchecked(ptr), - _marker: PhantomData, - _marker2: PhantomData, - } - } - - pub unsafe fn as_mut(&self) -> &mut T { - &mut *self.ptr.as_ptr() - } - } - - impl<'a, T> From<&'a mut T> for Shared { - fn from(reference: &'a mut T) -> Self { - unsafe { Shared::new_unchecked(reference) } - } - } -} diff --git a/src/shim.rs b/src/shim.rs new file mode 100644 index 00000000000..08fbf32b72f --- /dev/null +++ b/src/shim.rs @@ -0,0 +1,60 @@ +use std::marker::PhantomData; + +pub struct NonZeroPtr(&'static T); + +impl NonZeroPtr { + pub unsafe fn new_unchecked(ptr: *mut T) -> Self { + NonZeroPtr(&*ptr) + } + pub fn as_ptr(&self) -> *mut T { + self.0 as *const T as *mut T + } +} + +pub struct Unique { + ptr: NonZeroPtr, + _marker: PhantomData, +} + +impl Unique { + pub unsafe fn new_unchecked(ptr: *mut T) -> Self { + Unique { + ptr: NonZeroPtr::new_unchecked(ptr), + _marker: PhantomData, + } + } + pub fn as_ptr(&self) -> *mut T { + self.ptr.as_ptr() + } +} + +unsafe impl Send for Unique { } + +unsafe impl Sync for Unique { } + +pub struct Shared { + ptr: NonZeroPtr, + _marker: PhantomData, + // force it to be !Send/!Sync + _marker2: PhantomData<*const u8>, +} + +impl Shared { + pub unsafe fn new_unchecked(ptr: *mut T) -> Self { + Shared { + ptr: NonZeroPtr::new_unchecked(ptr), + _marker: PhantomData, + _marker2: PhantomData, + } + } + + pub unsafe fn as_mut(&self) -> &mut T { + &mut *self.ptr.as_ptr() + } +} + +impl<'a, T> From<&'a mut T> for Shared { + fn from(reference: &'a mut T) -> Self { + unsafe { Shared::new_unchecked(reference) } + } +} \ No newline at end of file