Function fat_type::convert_mut[][src]

pub unsafe fn convert_mut<'a, R: FatMut<'a>, U>(thin: R) -> &'a mut Fat<R::T, U>
Expand description

Obtains a mutable reference by respecifying the type of the sized referent, which may be erased.

Safety

The safety of this operation depends on the types of R::U and U.

  • If R::U is Erased<T>, then either U must also be Erased<T> or the un-erased type of R::U must be safe into transmute to U and vice-versa.
  • If R::U is an un-erased type, then either U must be Erased<T> or R::U must be safe to transmute into U and vice-versa.

The transmutation requirements above are bijective because the associated metadata is unmodified and remains that of the un-erased type.

If U is Erased<T> then the returned mutable reference, which is of type &mut Fat<T>, must not be used to create a dynamically incoherent value. The following problematic operations can create such values because the referent is omitted from most operations due to erasure. The Mut<T> type can be used to create a safe wrapper around the reference.

Problematic Operations

  • move operations on owned values are problematic because the referent is not moved. Note that implicit copying will not occur because Erased<T> is not Copy.
  • copy functions such as core::ptr::copy and core::ptr::read are problematic because the referent will be omitted from any copies.
  • swap functions such as core::mem::swap are problematic because the metadata will be swapped but the referent will not.
  • write functions such as core::ptr::write are problematic because the referent will not be written.
  • replace functions such as core::mem::replace are problematic because the referent will neither be replaced nor returned.
  • Dropping a Fat<T> will not drop its referent. This is generally not desirable and may interact poorly with the Drop Guarantee.
  • Deallocating a Fat<T> which was allocated as a Fat<T, U> causes immediate undefined behavior because its layout omits the referent. Deallocation can be performed manually or by types such as Box, Rc, Arc, and the standard collections. To safely deallocate, use get_layout to obtain the correct Layout.

Safe Operations

All operations defined in fat_type are guaranteed to maintain dynamic coherence if their arguments are valid and dynamically coherent, except where explicitly documented otherwise.