use std::fmt;
use std::hash::Hash;
use std::ptr::NonNull;
#[repr(transparent)]
pub struct SendSyncPtr<T: ?Sized>(NonNull<T>);
unsafe impl<T: Send + ?Sized> Send for SendSyncPtr<T> {}
unsafe impl<T: Sync + ?Sized> Sync for SendSyncPtr<T> {}
impl<T: ?Sized> SendSyncPtr<T> {
#[inline]
pub fn new(ptr: NonNull<T>) -> SendSyncPtr<T> {
SendSyncPtr(ptr)
}
#[inline]
pub fn as_ptr(&self) -> *mut T {
self.0.as_ptr()
}
#[inline]
pub unsafe fn as_ref<'a>(&self) -> &'a T {
self.0.as_ref()
}
#[inline]
pub unsafe fn as_mut<'a>(&mut self) -> &'a mut T {
self.0.as_mut()
}
#[inline]
pub fn as_non_null(&self) -> NonNull<T> {
self.0
}
#[inline]
pub fn cast<U>(&self) -> SendSyncPtr<U> {
SendSyncPtr(self.0.cast::<U>())
}
}
impl<T> SendSyncPtr<[T]> {
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
}
impl<T: ?Sized, U> From<U> for SendSyncPtr<T>
where
U: Into<NonNull<T>>,
{
#[inline]
fn from(ptr: U) -> SendSyncPtr<T> {
SendSyncPtr::new(ptr.into())
}
}
impl<T: ?Sized> fmt::Debug for SendSyncPtr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_ptr().fmt(f)
}
}
impl<T: ?Sized> fmt::Pointer for SendSyncPtr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_ptr().fmt(f)
}
}
impl<T: ?Sized> Clone for SendSyncPtr<T> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for SendSyncPtr<T> {}
impl<T: ?Sized> PartialEq for SendSyncPtr<T> {
#[inline]
fn eq(&self, other: &SendSyncPtr<T>) -> bool {
self.0 == other.0
}
}
impl<T: ?Sized> Eq for SendSyncPtr<T> {}
impl<T: ?Sized> Hash for SendSyncPtr<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_ptr().hash(state);
}
}