Struct fat_type::Fat

source ·
#[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 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.

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.

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.

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.

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

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));

Obtains layout information for a Fat<T, U> having the provided pointer metadata.

Returns
  • The allocation Layout of a Fat<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§

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
Immutably borrows from an owned value. Read more
Mutably 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 ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
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.