Function bytemuck::must_cast

source ·
pub const fn must_cast<A: NoUninit, B: AnyBitPattern>(a: A) -> B
Expand description

Cast A into B if infalliable, or fail to compile.

Note that for this particular type of cast, alignment isn’t a factor. The input value is semantically copied into the function and then returned to a new memory location which will have whatever the required alignment of the output type is.

§Failure

  • If the types don’t have the same size this fails to compile.

§Examples

// compiles:
let bytes: [u8; 2] = bytemuck::must_cast(12_u16);
// fails to compile (size mismatch):
let bytes : [u8; 3] = bytemuck::must_cast(12_u16);