Skip to main content

as_bytes

Function as_bytes 

Source
pub unsafe fn as_bytes<T: Copy>(value: &T) -> &[u8] 
Expand description

Reinterprets a reference to a Copy type as a byte slice.

This is useful for passing #[repr(C)] structs to Vulkan commands that accept &[u8], such as cmd_push_constants.

§Safety

T must not contain padding bytes. Reading uninitialized padding is undefined behavior. Use #[repr(C)] structs whose fields leave no gaps, or verify with assert_eq!(size_of::<T>(), /* expected */).

§Examples

use vulkan_rust::as_bytes;

#[repr(C)]
#[derive(Clone, Copy)]
struct PushData { time: f32, scale: f32 }

let data = PushData { time: 1.0, scale: 2.0 };
let bytes = unsafe { as_bytes(&data) };
assert_eq!(bytes.len(), 8);