Function safe_transmute::base::transmute_vec

source ·
pub unsafe fn transmute_vec<S, T>(vec: Vec<S>) -> Vec<T>
Expand description

Transform a vector into a vector of another element type.

The vector’s allocated byte buffer (if already allocated) will be reused.

§Safety

Vector transmutations are exceptionally dangerous because of the constraints imposed by Vec::from_raw_parts().

Unless all of the following requirements are fulfilled, this operation may result in undefined behavior:

  • The target type T must have the same size and minimum alignment as the type S.
  • The vector’s data needs to correspond to a valid contiguous sequence of T values. Types T with a Drop implementation are unlikely to be safe in this regard.

§Examples

unsafe {
    assert_eq!(
        transmute_vec::<u8, i8>(vec![0x00, 0x01, 0x00, 0x02]),
        vec![0x00i8, 0x01i8, 0x00i8, 0x02i8]
    );
}