use std::ops::{Deref, DerefMut};
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct MutPtr<T>(pub *mut T);
impl<T> MutPtr<T> {
#[inline(always)]
pub fn new(ptr: *mut T) -> Self {
Self(ptr)
}
#[inline(always)]
pub fn is_null(&self) -> bool {
self.0.is_null()
}
#[inline(always)]
pub fn as_ptr(&self) -> *mut T {
self.0
}
}
impl<T> Deref for MutPtr<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &Self::Target {
if self.0.is_null() {
panic!("Null pointer dereference: {}", std::any::type_name::<T>());
}
unsafe { &*self.0 }
}
}
impl<T> DerefMut for MutPtr<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
if self.0.is_null() {
panic!("Null pointer dereference: {}", std::any::type_name::<T>());
}
unsafe { &mut *self.0 }
}
}