use std::{
fmt,
ops::{Deref, DerefMut},
ptr::NonNull,
};
#[repr(transparent)]
pub struct OwnedPtr<T>(NonNull<T>);
impl<T> OwnedPtr<T> {
pub fn as_ptr(&self) -> *mut T {
self.0.as_ptr()
}
}
impl<T> Deref for OwnedPtr<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { self.0.as_ref() }
}
}
impl<T> AsRef<T> for OwnedPtr<T> {
fn as_ref(&self) -> &T {
self.deref()
}
}
impl<T> DerefMut for OwnedPtr<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.as_mut() }
}
}
impl<T> AsMut<T> for OwnedPtr<T> {
fn as_mut(&mut self) -> &mut T {
self.deref_mut()
}
}
impl<T> fmt::Debug for OwnedPtr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
self.0.fmt(f)
}
}
impl<T> fmt::Pointer for OwnedPtr<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
fmt::Pointer::fmt(&self.0, f)
}
}
unsafe impl<T> Send for OwnedPtr<T> {}
unsafe impl<T> Sync for OwnedPtr<T> where T: Sync {}