#[cfg(feature = "alloc")]
mod alloc;
mod core;
use ::core::{alloc::LayoutError, error::Error, fmt, ptr::NonNull};
use ptr_meta::{from_raw_parts_mut, Pointee};
use rancor::{fail, Fallible, ResultExt as _, Source, Strategy};
#[cfg(feature = "alloc")]
pub use self::alloc::*;
pub use self::core::*;
pub use crate::erased::{ErasedPtr, FromMetadata, Metadata};
use crate::{traits::LayoutRaw, ArchiveUnsized, DeserializeUnsized};
pub unsafe trait SharedPointer<T: Pointee + ?Sized> {
fn alloc(metadata: T::Metadata) -> Result<*mut T, LayoutError>;
unsafe fn from_value(ptr: *mut T) -> *mut T;
unsafe fn drop(ptr: *mut T);
}
pub enum PoolingState {
Started,
Pending,
Finished(ErasedPtr),
}
pub trait Pooling<E = <Self as Fallible>::Error> {
fn start_pooling(&mut self, address: usize) -> PoolingState;
unsafe fn finish_pooling(
&mut self,
address: usize,
ptr: ErasedPtr,
drop: unsafe fn(ErasedPtr),
) -> Result<(), E>;
}
impl<T, E> Pooling<E> for Strategy<T, E>
where
T: Pooling<E>,
{
fn start_pooling(&mut self, address: usize) -> PoolingState {
T::start_pooling(self, address)
}
unsafe fn finish_pooling(
&mut self,
address: usize,
ptr: ErasedPtr,
drop: unsafe fn(ErasedPtr),
) -> Result<(), E> {
unsafe { T::finish_pooling(self, address, ptr, drop) }
}
}
#[derive(Debug)]
struct CyclicSharedPointerError;
impl fmt::Display for CyclicSharedPointerError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"encountered cyclic shared pointers while deserializing\nhelp: \
change your deserialization strategy to `Unpool` or use the \
`Unpool` wrapper type to break the cycle",
)
}
}
impl Error for CyclicSharedPointerError {}
pub trait PoolingExt<E>: Pooling<E> {
fn deserialize_shared<T, P>(
&mut self,
value: &T::Archived,
) -> Result<*mut T, Self::Error>
where
T: ArchiveUnsized + Pointee + LayoutRaw + ?Sized,
T::Metadata: Into<Metadata> + FromMetadata,
T::Archived: DeserializeUnsized<T, Self>,
P: SharedPointer<T>,
Self: Fallible<Error = E>,
E: Source,
{
unsafe fn drop_shared<T, P>(ptr: ErasedPtr)
where
T: Pointee + ?Sized,
T::Metadata: FromMetadata,
P: SharedPointer<T>,
{
unsafe { P::drop(ptr.downcast_unchecked::<T>()) }
}
let address = value as *const T::Archived as *const () as usize;
let metadata = T::Archived::deserialize_metadata(value);
match self.start_pooling(address) {
PoolingState::Started => {
let out = P::alloc(metadata).into_error()?;
unsafe { value.deserialize_unsized(self, out)? };
let ptr = unsafe { NonNull::new_unchecked(P::from_value(out)) };
unsafe {
self.finish_pooling(
address,
ErasedPtr::new(ptr.as_ptr()),
drop_shared::<T, P>,
)?;
}
Ok(ptr.as_ptr())
}
PoolingState::Pending => fail!(CyclicSharedPointerError),
PoolingState::Finished(ptr) => {
Ok(from_raw_parts_mut(ptr.data_address(), metadata))
}
}
}
}
impl<T, E> PoolingExt<E> for T where T: Pooling<E> + ?Sized {}