Expand description
A type which combines a value with the metadata used to construct references to it.
A value of the type Fat<T: ?Sized, U:
Unsize<T>
>
, created by
new
, supports pointer-sized “thin” references &Fat<T>
(via
erase_ref
) and &mut Fat<T>
(via erase_mut
), even when T
is not Thin
, such as in the case of slices or trait objects.
Terminology
- The value of type
T
is referred to as the unsized referent. - The value of type
U
is referred to as the sized referent. Fat<T>
references are produced fromFat<T, U>
references through erasure, whereErased
replacesU
. The originalU
is referred to as the un-erased type.
Auto-Deref
Fat<T>
is Deref
and DerefMut
as T
. All operations are provided
as associated functions to prevent name collisions. This means that you
use references to Fat<T>
as-if they were to T
.
Safety
- Incoherent tyes can be named but cannot be constructed by safe code
because
new
is only provided whenU:
Unsize<T>
. - The ability to incoherently mutate metadata is prevented by
Erased
, which is a fully-opaque extern type. - There is no constructor to create a bare
Fat<T>
. As such, the type can only exist behind a reference, which can only safely be created byerase_ref
anderase_mut
.
Example
// References to wrapped trait objects are smaller than the bare references.
assert!(mem::size_of::<&Fat<dyn Debug>>() < mem::size_of::<&dyn Debug>());
// References to wrapped trait objects are, in fact, pointer sized.
assert!(mem::size_of::<&Fat<dyn Debug>>() == mem::size_of::<&()>());
See Also
Implementations§
source§impl<T: ?Sized, U> Fat<T, U>
impl<T: ?Sized, U> Fat<T, U>
sourcepub fn into_inner(fat: Self) -> U
pub fn into_inner(fat: Self) -> U
Extracts the sized referent, discarding the associated metadata.
sourcepub fn inner_ref(fat: &Self) -> &U
pub fn inner_ref(fat: &Self) -> &U
Obtains an immutable reference to the sized referent.
sourcepub fn inner_mut(fat: &mut Self) -> &mut U
pub fn inner_mut(fat: &mut Self) -> &mut U
Obtains a mutable reference to the sized referent.
source§impl<T: ?Sized> Fat<T>
impl<T: ?Sized> Fat<T>
sourcepub unsafe fn container_of(referent: *const T) -> NonNull<Self>
pub unsafe fn container_of(referent: *const T) -> NonNull<Self>
Obtains a pointer to the Fat<T>
containing the specified
unsized referent.
Safety
referent
must be the unsized referent of some Fat<T>
, such as the
reference obtained from deref
.
Safety Considerations
core::ptr::NonNull
provides methods that treat the contained pointer
as-if it were a pointer to mutable data. Use of these operations may
lead to undefined behavior.
Strict Pointer Provenance
The provenance of the returned pointer is inherited from the argument.
source§impl<T: ?Sized, U: ?Sized> Fat<T, U>
impl<T: ?Sized, U: ?Sized> Fat<T, U>
sourcepub fn erase_ref(fat: &Self) -> &Fat<T>
pub fn erase_ref(fat: &Self) -> &Fat<T>
Obtains a thin immutable reference by erasing the type of the sized referent.
sourcepub fn erase_mut(fat: &mut Self) -> &mut Fat<T>
pub fn erase_mut(fat: &mut Self) -> &mut Fat<T>
Obtains a thin mutable reference by erasing the type of the sized referent.
sourcepub fn data_addr(fat: &Self) -> NonNull<()>
pub fn data_addr(fat: &Self) -> NonNull<()>
Returns a thin pointer to the sized referent, suitable for use with
core::ptr::from_raw_parts
.
Safety Considerations
core::ptr::NonNull
provides methods that treat the contained pointer
as-if it were a pointer to mutable data. Use of these operations may
lead to undefined behavior. Consider using Fat::data_addr_mut
instead, if possible.
Strict Pointer Provenance
The provenance of the returned pointer is inherited from the argument.
sourcepub fn data_addr_mut(fat: &mut Self) -> NonNull<()>
pub fn data_addr_mut(fat: &mut Self) -> NonNull<()>
Returns a thin pointer to the sized referent, suitable for use with
core::ptr::from_raw_parts_mut
.
Strict Pointer Provenance
The provenance of the returned pointer is inherited from the argument.
sourcepub fn metadata(fat: &Self) -> &<T as Pointee>::Metadata
pub fn metadata(fat: &Self) -> &<T as Pointee>::Metadata
Obtains an immutable reference to the metadata used to construct references to the unsized referent.
sourcepub fn layout_of(fat: &Self) -> (Layout, usize)
pub fn layout_of(fat: &Self) -> (Layout, usize)
Obtains layout information for the specified value.
See Fat::layout_for
.
Example
let fat = Fat::<dyn Debug, u32>::new(42);
let thin: &Fat<dyn Debug> = Fat::erase_ref(&fat);
// Despite type erasure, we can obtain the original layout.
assert_eq!(Fat::layout_of(thin).0, Layout::for_value(&fat));
sourcepub unsafe fn layout_for(metadata: &<T as Pointee>::Metadata) -> (Layout, usize)
pub unsafe fn layout_for(metadata: &<T as Pointee>::Metadata) -> (Layout, usize)
Obtains layout information for a Fat<T, U>
having the provided
pointer metadata.
Returns
- The allocation
Layout
of aFat<T, U>
with the specified metadata. - The corresponding pointer offset of the unsized referent.
The returned layout information is correct in the sense that unsafe code
may rely on it being the actual layout of a Fat<T, U>
with the
specified metadata.
Safety
The provided metadata
must be the result of the unsizing operation
T as U
.
Trait Implementations§
source§impl<T: ?Sized, U> BorrowMut<Fat<T, Erased>> for Fat<T, U>
impl<T: ?Sized, U> BorrowMut<Fat<T, Erased>> for Fat<T, U>
source§fn borrow_mut(&mut self) -> &mut Fat<T>
fn borrow_mut(&mut self) -> &mut Fat<T>
source§impl<T: ?Sized, U: ?Sized> BorrowMut<T> for Fat<T, U>
impl<T: ?Sized, U: ?Sized> BorrowMut<T> for Fat<T, U>
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<'a, T: ?Sized, U: ?Sized> IntoIterator for &'a Fat<T, U>where
&'a T: IntoIterator,
impl<'a, T: ?Sized, U: ?Sized> IntoIterator for &'a Fat<T, U>where
&'a T: IntoIterator,
source§impl<'a, T: ?Sized, U: ?Sized> IntoIterator for &'a mut Fat<T, U>where
&'a mut T: IntoIterator,
impl<'a, T: ?Sized, U: ?Sized> IntoIterator for &'a mut Fat<T, U>where
&'a mut T: IntoIterator,
source§impl<T: PartialEq + ?Sized, U: ?Sized, V: ?Sized> PartialEq<Fat<T, V>> for Fat<T, U>
impl<T: PartialEq + ?Sized, U: ?Sized, V: ?Sized> PartialEq<Fat<T, V>> for Fat<T, U>
source§impl<T: PartialOrd + ?Sized, U: ?Sized, V: ?Sized> PartialOrd<Fat<T, V>> for Fat<T, U>
impl<T: PartialOrd + ?Sized, U: ?Sized, V: ?Sized> PartialOrd<Fat<T, V>> for Fat<T, U>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl<T: PartialOrd + ?Sized, U: ?Sized> PartialOrd<T> for Fat<T, U>
impl<T: PartialOrd + ?Sized, U: ?Sized> PartialOrd<T> for Fat<T, U>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more