Function safe_transmute::trivial::align_to_mut

source ·
pub fn align_to_mut<S: TriviallyTransmutable, T: TriviallyTransmutable>(
    slice: &mut [S]
) -> (&mut [S], &mut [T], &mut [S])
Expand description

Transmute the slice to a slice of another type, ensuring alignment of the types is maintained.

This function is equivalent to std::slice::align_to_mut().

However, since both source and target types are trivially transmutable, the operation is always safe.

§Example

let mut bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
let (prefix, shorts, suffix) = align_to_mut::<_, u16>(&mut bytes);

// less_efficient_algorithm_for_bytes(prefix);
// more_efficient_algorithm_for_aligned_shorts(shorts);
// less_efficient_algorithm_for_bytes(suffix);

assert_eq!(prefix.len() + shorts.len() * 2 + suffix.len(), 7);