dyn_dyn/
cast_target.rs

1use core::ptr::{self, DynMetadata, NonNull, Pointee};
2
3/// A type whose pointer metadata can be stored in a [`DynDynTable`](crate::DynDynTable).
4pub trait DynDynCastTarget: private::Sealed {
5    /// The root type that the trait object metadata is for.
6    type Root: ?Sized;
7
8    /// Splits a fat pointer into its data pointer and metadata.
9    fn ptr_into_parts(ptr: NonNull<Self>) -> (NonNull<()>, DynMetadata<Self::Root>);
10
11    /// Combines a data pointer with the provided metadata to produce a fat pointer.
12    fn ptr_from_parts(data: NonNull<()>, meta: DynMetadata<Self::Root>) -> NonNull<Self>;
13}
14
15impl<M: ?Sized, T: Pointee<Metadata = DynMetadata<M>> + ?Sized> DynDynCastTarget for T {
16    type Root = M;
17
18    fn ptr_into_parts(ptr: NonNull<Self>) -> (NonNull<()>, DynMetadata<M>) {
19        (ptr.cast(), ptr::metadata(ptr.as_ptr()))
20    }
21
22    fn ptr_from_parts(data: NonNull<()>, meta: DynMetadata<M>) -> NonNull<Self> {
23        // SAFETY: If data is not null, then the result of attaching metadata to it is not null either
24        unsafe { NonNull::new_unchecked(ptr::from_raw_parts_mut(data.as_ptr(), meta)) }
25    }
26}
27
28mod private {
29    use core::ptr::{DynMetadata, Pointee};
30
31    pub trait Sealed {}
32
33    impl<M: ?Sized, T: Pointee<Metadata = DynMetadata<M>> + ?Sized> Sealed for T {}
34}