#[repr(C)]
pub struct Fat<T: ?Sized, U: ?Sized = Erased> { /* private fields */ }
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 from Fat<T, U> references through erasure, where Erased replaces U. The original U 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 when U: 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 by erase_ref and erase_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

  • new – To create a Fat value.
  • erase_ref, erase_mut – To obtain a thin reference from a Fat reference.

Implementations

Constructs a new Fat value with metadata from unsizing value as a T.

Example
let fat = Fat::<dyn Debug, u8>::new(42);
let thin: &Fat<dyn Debug> = Fat::erase_ref(&fat);
assert_eq!(mem::size_of_val(&thin), mem::size_of::<&()>());

dbg!(thin); // 42

Extracts the sized referent, discarding the associated metadata.

Obtains an immutable reference to the sized referent.

Obtains a mutable reference to the sized referent.

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.

Obtains a mutable 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_mut.

Obtains a thin immutable reference by erasing the type of the sized referent.

Obtains a thin mutable reference by erasing the type of the sized referent.

Obtains an immutable reference to the metadata used to construct references to the unsized referent.

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

Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a mutable reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Converts this type into a shared reference of the (usually inferred) input type.
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Formats the value using the given formatter. Read more
Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
The returned type after indexing.
Performs the indexing (container[index]) operation. Read more
Performs the mutable indexing (container[index]) operation. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
This method returns an Ordering between self and other. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
Converts to this type from the input type.

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.