Move shim into its own file

This commit is contained in:
Manish Goregaokar 2017-08-25 17:17:54 -07:00
parent 63c8b345be
commit 61c6d4e1df
2 changed files with 61 additions and 63 deletions

View file

@ -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<Q: ?Sized> {
fn take(&mut self, key: &Q) -> Option<Self::Key>;
fn replace(&mut self, key: Self::Key) -> Option<Self::Key>;
}
mod shim {
use std::marker::PhantomData;
pub struct NonZeroPtr<T: 'static>(&'static T);
impl<T: 'static> NonZeroPtr<T> {
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<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
}
impl<T: 'static> Unique<T> {
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<T: Send + 'static> Send for Unique<T> { }
unsafe impl<T: Sync + 'static> Sync for Unique<T> { }
pub struct Shared<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
// force it to be !Send/!Sync
_marker2: PhantomData<*const u8>,
}
impl<T: 'static> Shared<T> {
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<T> {
fn from(reference: &'a mut T) -> Self {
unsafe { Shared::new_unchecked(reference) }
}
}
}

60
src/shim.rs Normal file
View file

@ -0,0 +1,60 @@
use std::marker::PhantomData;
pub struct NonZeroPtr<T: 'static>(&'static T);
impl<T: 'static> NonZeroPtr<T> {
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<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
}
impl<T: 'static> Unique<T> {
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<T: Send + 'static> Send for Unique<T> { }
unsafe impl<T: Sync + 'static> Sync for Unique<T> { }
pub struct Shared<T: 'static> {
ptr: NonZeroPtr<T>,
_marker: PhantomData<T>,
// force it to be !Send/!Sync
_marker2: PhantomData<*const u8>,
}
impl<T: 'static> Shared<T> {
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<T> {
fn from(reference: &'a mut T) -> Self {
unsafe { Shared::new_unchecked(reference) }
}
}