MCPcopy Create free account
hub / github.com/RustCrypto/utils / cast_slice

Function cast_slice

cmov/src/slice.rs:311–324  ·  view source on GitHub ↗

Performs an unsafe pointer cast from one slice type to the other. # Compile-time panics - If `T` and `U` differ in size - If `T` and `U` differ in alignment

(slice: &[T])

Source from the content-addressed store, hash-verified

309/// - If `T` and `U` differ in size
310/// - If `T` and `U` differ in alignment
311unsafe fn cast_slice<T, U>(slice: &[T]) -> &[U] {
312 const {
313 assert!(size_of::<T>() == size_of::<U>(), "T/U size differs");
314 assert!(align_of::<T>() == align_of::<U>(), "T/U alignment differs");
315 }
316
317 // SAFETY:
318 // - Slices are of same-sized/aligned types as asserted above.
319 // - It's up to the caller to ensure the pointer cast from `T` to `U` itself is valid.
320 #[allow(unsafe_code)]
321 unsafe {
322 &*(ptr::from_ref::<[T]>(slice) as *const [U])
323 }
324}
325
326/// Performs an unsafe pointer cast from one mutable slice type to the other.
327///

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected