Function safe_transmute::transmute_vec
source · pub fn transmute_vec<S: TriviallyTransmutable, T: TriviallyTransmutable>(
vec: Vec<S>
) -> Result<Vec<T>, Error<'static, S, T>>
Expand description
Transform a vector into a vector of values with the given target type.
The resulting vector will reuse the allocated byte buffer when successful.
§Errors
An error is returned if either the size or the minimum memory
requirements are not the same between S
and U
:
std::mem::size_of::<S>() != std::mem::size_of::<T>()
std::mem::align_of::<S>() != std::mem::align_of::<T>()
Otherwise, the only truly safe way of doing this is to create a transmuted
slice view of the vector, or make a copy anyway. The
IncompatibleVecTargetError
error
type provides a means of making this copy to the intended target type.
§Examples
assert_eq!(transmute_vec::<u8, i8>(vec![0x00, 0x01, 0x00, 0x02])?,
vec![0x00i8, 0x01i8, 0x00i8, 0x02i8]);
assert_eq!(transmute_vec::<u8, i8>(vec![0x04, 0x00, 0x00, 0x00, 0xED])?,
vec![0x04, 0x00, 0x00, 0x00, -0x13i8]);