Function safe_transmute::trivial::align_to

source ·
pub fn align_to<S: TriviallyTransmutable, T: TriviallyTransmutable>(
    slice: &[S]
) -> (&[S], &[T], &[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().

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

§Example

let bytes: [u8; 7] = [1, 2, 3, 4, 5, 6, 7];
let (prefix, shorts, suffix) = align_to::<_, u16>(&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);