Function core_extensions::utils::transmute_ignore_size
source · pub unsafe fn transmute_ignore_size<T, U>(v: T) -> U
Expand description
Allows transmuting between types of different sizes.
Necessary for transmuting in generic functions, since (as of Rust 1.51.0) transmute doesn’t work well with generic types.
§Safety
This function has the same safety requirements as std::mem::transmute_copy
.
§Example
use core_extensions::utils::transmute_ignore_size;
use std::mem::MaybeUninit;
unsafe fn transmute_into_init<T>(array: [MaybeUninit<T>; 3]) -> [T; 3] {
transmute_ignore_size(array)
}
let array = [MaybeUninit::new(3), MaybeUninit::new(5), MaybeUninit::new(8)];
unsafe{ assert_eq!(transmute_into_init(array), [3, 5, 8]); }
This is the error you get if you tried to use std::mem::transmute
.
error[E0512]: cannot transmute between types of different sizes, or dependently-sized types
--> src/lib.rs:4:5
|
4 | std::mem::transmute(array)
| ^^^^^^^^^^^^^^^^^^^
|
= note: source type: `[MaybeUninit<T>; 3]` (size can vary because of T)
= note: target type: `[T; 3]` (size can vary because of T)