use core::ptr::NonNull;
pub const unsafe fn as_ref<'a, T: ?Sized>(ptr: *const T) -> Option<&'a T> {
core::mem::transmute(ptr)
}
#[cfg(feature = "mut_refs")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "mut_refs")))]
pub const unsafe fn as_mut<'a, T: ?Sized>(ptr: *mut T) -> Option<&'a mut T> {
core::mem::transmute(ptr)
}
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub const fn is_null<T: ?Sized>(ptr: *const T) -> bool {
unsafe {
matches!(
core::mem::transmute::<*const T, Option<NonNull<T>>>(ptr),
None
)
}
}
pub mod nonnull {
use core::ptr::NonNull;
#[allow(clippy::not_unsafe_ptr_arg_deref)]
pub const fn new<T: ?Sized>(ptr: *mut T) -> Option<NonNull<T>> {
unsafe { core::mem::transmute(ptr) }
}
pub const unsafe fn as_ref<'a, T: ?Sized>(ptr: NonNull<T>) -> &'a T {
core::mem::transmute(ptr)
}
#[cfg(feature = "mut_refs")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "mut_refs")))]
pub const unsafe fn as_mut<'a, T: ?Sized>(ptr: NonNull<T>) -> &'a mut T {
core::mem::transmute(ptr)
}
pub const fn from_ref<T: ?Sized>(reff: &T) -> NonNull<T> {
unsafe { NonNull::new_unchecked(reff as *const _ as *mut _) }
}
#[cfg(feature = "mut_refs")]
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "mut_refs")))]
pub const fn from_mut<T: ?Sized>(mutt: &mut T) -> NonNull<T> {
unsafe { NonNull::new_unchecked(mutt) }
}
}