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
Tis referred to as the unsized referent. - The value of type
Uis referred to as the sized referent. Fat<T>references are produced fromFat<T, U>references through erasure, whereErasedreplacesU. The originalUis 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
newis 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_refanderase_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
sourceimpl<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.
sourceimpl<T: ?Sized> Fat<T>
impl<T: ?Sized> Fat<T>
sourcepub unsafe fn of_ref(referent: &T) -> *const Self
pub unsafe fn of_ref(referent: &T) -> *const Self
Obtains a const 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.
sourceimpl<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 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(fat: &Self) -> Layout
pub fn layout(fat: &Self) -> Layout
Returns the un-erased allocation Layout of the specified value.
The returned Layout is identical to that of Fat<T, U>, where U
is the un-erased type.
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(thin), Layout::for_value(&fat));Trait Implementations
sourceimpl<T: ?Sized, U: ?Sized> BorrowMut<T> for Fat<T, U>
impl<T: ?Sized, U: ?Sized> BorrowMut<T> for Fat<T, U>
sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
sourceimpl<'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,
sourceimpl<'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,
sourceimpl<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>
sourceimpl<T: PartialEq + ?Sized, U: ?Sized> PartialEq<T> for Fat<T, U>
impl<T: PartialEq + ?Sized, U: ?Sized> PartialEq<T> for Fat<T, U>
sourceimpl<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>
sourcefn partial_cmp(&self, other: &Fat<T, V>) -> Option<Ordering>
fn partial_cmp(&self, other: &Fat<T, V>) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresourceimpl<T: PartialOrd + ?Sized, U: ?Sized> PartialOrd<T> for Fat<T, U>
impl<T: PartialOrd + ?Sized, U: ?Sized> PartialOrd<T> for Fat<T, U>
sourcefn partial_cmp(&self, other: &T) -> Option<Ordering>
fn partial_cmp(&self, other: &T) -> Option<Ordering>
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more