use core::ptr::NonNull;
use crate::prelude::*;
pub trait OwnerPtr {
type Target;
fn into_raw(self) -> *const Self::Target;
unsafe fn from_raw(ptr: *const Self::Target) -> Self;
}
impl<T> OwnerPtr for Box<T> {
type Target = T;
fn into_raw(self) -> *const Self::Target {
Box::into_raw(self) as *const _
}
unsafe fn from_raw(ptr: *const Self::Target) -> Self {
Box::from_raw(ptr as *mut _)
}
}
impl<T> OwnerPtr for Arc<T> {
type Target = T;
fn into_raw(self) -> *const Self::Target {
Arc::into_raw(self)
}
unsafe fn from_raw(ptr: *const Self::Target) -> Self {
Arc::from_raw(ptr)
}
}
impl<P> OwnerPtr for Option<P>
where
P: OwnerPtr,
<P as OwnerPtr>::Target: Sized,
{
type Target = P::Target;
fn into_raw(self) -> *const Self::Target {
self.map(|p| <P as OwnerPtr>::into_raw(p))
.unwrap_or(core::ptr::null())
}
unsafe fn from_raw(ptr: *const Self::Target) -> Self {
if ptr.is_null() {
Some(<P as OwnerPtr>::from_raw(ptr))
} else {
None
}
}
}