1 // Currently this test doesn't actually check the output of the functions.
2 // It's only here for miri to check for any potential undefined behaviour.
3 // TODO: check function results
4 
5 #[test]
test_transparent_wrapper()6 fn test_transparent_wrapper() {
7   // An external type defined in a different crate.
8   #[derive(Copy, Clone, Default)]
9   struct Foreign(u8);
10 
11   use bytemuck::TransparentWrapper;
12 
13   #[derive(Copy, Clone)]
14   #[repr(transparent)]
15   struct Wrapper(Foreign);
16 
17   unsafe impl TransparentWrapper<Foreign> for Wrapper {}
18 
19   // Traits can be implemented on crate-local wrapper.
20   unsafe impl bytemuck::Zeroable for Wrapper {}
21   unsafe impl bytemuck::Pod for Wrapper {}
22 
23   let _: u8 = bytemuck::cast(Wrapper::wrap(Foreign::default()));
24   let _: Foreign = Wrapper::peel(bytemuck::cast(u8::default()));
25 
26   let _: &u8 = bytemuck::cast_ref(Wrapper::wrap_ref(&Foreign::default()));
27   let _: &Foreign = Wrapper::peel_ref(bytemuck::cast_ref(&u8::default()));
28 
29   let _: &mut u8 =
30     bytemuck::cast_mut(Wrapper::wrap_mut(&mut Foreign::default()));
31   let _: &mut Foreign =
32     Wrapper::peel_mut(bytemuck::cast_mut(&mut u8::default()));
33 
34   let _: &[u8] =
35     bytemuck::cast_slice(Wrapper::wrap_slice(&[Foreign::default()]));
36   let _: &[Foreign] =
37     Wrapper::peel_slice(bytemuck::cast_slice(&[u8::default()]));
38 
39   let _: &mut [u8] =
40     bytemuck::cast_slice_mut(Wrapper::wrap_slice_mut(
41       &mut [Foreign::default()],
42     ));
43   let _: &mut [Foreign] =
44     Wrapper::peel_slice_mut(bytemuck::cast_slice_mut(&mut [u8::default()]));
45 
46   let _: &[u8] = bytemuck::bytes_of(Wrapper::wrap_ref(&Foreign::default()));
47   let _: &Foreign = Wrapper::peel_ref(bytemuck::from_bytes(&[u8::default()]));
48 
49   let _: &mut [u8] =
50     bytemuck::bytes_of_mut(Wrapper::wrap_mut(&mut Foreign::default()));
51   let _: &mut Foreign =
52     Wrapper::peel_mut(bytemuck::from_bytes_mut(&mut [u8::default()]));
53 
54   // not sure if this is the right usage
55   let _ =
56     bytemuck::pod_align_to::<_, u8>(Wrapper::wrap_slice(&[Foreign::default()]));
57   // counterpart?
58 
59   // not sure if this is the right usage
60   let _ = bytemuck::pod_align_to_mut::<_, u8>(Wrapper::wrap_slice_mut(&mut [
61     Foreign::default(),
62   ]));
63   // counterpart?
64 }
65