use core::fmt; use core::hash::{Hash, Hasher}; use core::ptr::{self, NonNull}; /// A helper type in Wasmtime to store a raw pointer to `T` while automatically /// inferring the `Send` and `Sync` traits for the container based on the /// properties of `T`. #[repr(transparent)] pub struct SendSyncPtr(NonNull); unsafe impl Send for SendSyncPtr {} unsafe impl Sync for SendSyncPtr {} impl SendSyncPtr { /// Creates a new pointer wrapping the non-nullable pointer provided. #[inline] pub fn new(ptr: NonNull) -> SendSyncPtr { SendSyncPtr(ptr) } /// Returns the underlying raw pointer. #[inline] pub fn as_ptr(&self) -> *mut T { self.0.as_ptr() } /// Unsafely assert that this is a pointer to valid contents and it's also /// valid to get a shared reference to it at this time. #[inline] pub unsafe fn as_ref<'a>(&self) -> &'a T { unsafe { self.0.as_ref() } } /// Unsafely assert that this is a pointer to valid contents and it's also /// valid to get a mutable reference to it at this time. #[inline] pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T { unsafe { self.0.as_mut() } } /// Returns the underlying `NonNull` wrapper. #[inline] pub fn as_non_null(&self) -> NonNull { self.0 } /// Cast this to a pointer to a `U`. #[inline] pub fn cast(&self) -> SendSyncPtr { SendSyncPtr(self.0.cast::()) } } impl SendSyncPtr<[T]> { /// Returns the slice's length component of the pointer. #[inline] pub fn len(&self) -> usize { self.0.len() } } impl From for SendSyncPtr where U: Into>, { #[inline] fn from(ptr: U) -> SendSyncPtr { SendSyncPtr::new(ptr.into()) } } impl fmt::Debug for SendSyncPtr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_ptr().fmt(f) } } impl fmt::Pointer for SendSyncPtr { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.as_ptr().fmt(f) } } impl Clone for SendSyncPtr { #[inline] fn clone(&self) -> Self { *self } } impl Copy for SendSyncPtr {} impl PartialEq for SendSyncPtr { #[inline] fn eq(&self, other: &SendSyncPtr) -> bool { ptr::eq(self.0.as_ptr(), other.0.as_ptr()) } } impl Eq for SendSyncPtr {} impl Hash for SendSyncPtr { fn hash(&self, state: &mut H) { self.as_ptr().hash(state); } }