use std::fmt::{Debug, Formatter};
pub(crate) struct NonNull<T>
where
T: ?Sized,
{
ptr: std::ptr::NonNull<T>,
}
impl<T> NonNull<T>
where
T: ?Sized,
{
pub(crate) const fn from_ref(r: &T) -> Self {
unsafe {
NonNull {
ptr: std::ptr::NonNull::new_unchecked(r as *const T as *mut T),
}
}
}
pub(crate) const fn from_ptr(p: *const T) -> Self {
NonNull {
ptr: std::ptr::NonNull::new(p as *mut T).expect("pointer is not null"),
}
}
pub(crate) const unsafe fn into_ref<'a>(self) -> &'a T {
unsafe { self.as_ref() }
}
pub(crate) const unsafe fn as_ref<'a>(&self) -> &'a T {
unsafe { self.ptr.as_ref() }
}
pub(crate) const fn as_ptr(&self) -> *const T {
self.ptr.as_ptr()
}
}
impl<T> Clone for NonNull<T>
where
T: ?Sized,
{
fn clone(&self) -> Self {
NonNull { ptr: self.ptr }
}
}
impl<T> PartialEq for NonNull<T>
where
T: ?Sized,
{
fn eq(&self, other: &NonNull<T>) -> bool {
std::ptr::eq(self.as_ptr(), other.as_ptr())
}
}
impl<T> Eq for NonNull<T> where T: ?Sized {}
impl<T> Debug for NonNull<T>
where
T: ?Sized,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Debug::fmt(&self.ptr, f)
}
}
unsafe impl<T> Send for NonNull<T> where T: Send + Sync + ?Sized {}
unsafe impl<T> Sync for NonNull<T> where T: Send + Sync + ?Sized {}