use std::any::type_name;
use std::fmt::Debug;
use std::marker::PhantomData;
use std::ptr::NonNull;
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
pub struct RawFactoryPointer<'a, T> {
inner: NonNull<T>,
_marker: PhantomData<&'a T>,
}
impl<'a, T> RawFactoryPointer<'a, T> {
#[inline]
pub fn new(inner: &'a T) -> Self {
Self {
inner: inner.into(),
_marker: PhantomData,
}
}
#[inline]
pub const unsafe fn from_raw(raw: NonNull<T>) -> Self {
Self {
inner: raw,
_marker: PhantomData,
}
}
#[inline]
pub const fn as_raw(&self) -> NonNull<T> {
self.inner
}
#[inline]
pub const fn as_ptr(&self) -> *mut T {
self.inner.as_ptr()
}
}
impl<T: Copy> RawFactoryPointer<'_, T> {
#[inline]
pub fn get(&self) -> T {
unsafe { self.inner.read() }
}
}
impl<T> Debug for RawFactoryPointer<'_, T> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{} ({:p})", type_name::<T>(), self.inner)
}
}
unsafe impl<T: Send> Send for RawFactoryPointer<'_, T> {}
unsafe impl<T: Sync> Sync for RawFactoryPointer<'_, T> {}