wgpu_types/cast_utils.rs
1/// Wrapper to unsafely define a wrapper type that can be used with `bytemuck`'s traits.
2///
3/// This is very useful as it allows us to use bytemuck on foreign types. Despite the
4/// unsafe assertion, it means that bytemuck is handling all the actual casting,
5/// so we can't screw up size or alignment handling.
6///
7/// Once wrapped you can use the [`bytemuck::TransparentWrapper`] methods and
8/// all the free methods that come with [`bytemuck::Pod`] and [`bytemuck::Zeroable`].
9///
10/// # Safety
11///
12/// Once wrapped, the resulting type must follow all the invariants
13/// of the [`bytemuck::Pod`] and [`bytemuck::Zeroable`] traits.
14#[macro_export]
15macro_rules! bytemuck_wrapper {
16 (unsafe struct $name:ident($inner:ty)) => {
17 #[derive(Copy, Clone)]
18 #[repr(transparent)]
19 struct $name($inner);
20
21 unsafe impl bytemuck::Zeroable for $name {}
22 unsafe impl bytemuck::Pod for $name {}
23 unsafe impl bytemuck::TransparentWrapper<$inner> for $name {}
24 };
25}