1 use core::mem::size_of;
2
3 use bytemuck::*;
4
5 #[test]
test_try_cast_slice()6 fn test_try_cast_slice() {
7 // some align4 data
8 let u32_slice: &[u32] = &[4, 5, 6];
9 // the same data as align1
10 let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap();
11
12 assert_eq!(
13 u32_slice.as_ptr() as *const u32 as usize,
14 the_bytes.as_ptr() as *const u8 as usize
15 );
16 assert_eq!(
17 u32_slice.len() * size_of::<u32>(),
18 the_bytes.len() * size_of::<u8>()
19 );
20
21 // by taking one byte off the front, we're definitely mis-aligned for u32.
22 let mis_aligned_bytes = &the_bytes[1..];
23 assert_eq!(
24 try_cast_slice::<u8, u32>(mis_aligned_bytes),
25 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
26 );
27
28 // by taking one byte off the end, we're aligned but would have slop bytes for u32
29 let the_bytes_len_minus1 = the_bytes.len() - 1;
30 let slop_bytes = &the_bytes[..the_bytes_len_minus1];
31 assert_eq!(
32 try_cast_slice::<u8, u32>(slop_bytes),
33 Err(PodCastError::OutputSliceWouldHaveSlop)
34 );
35
36 // if we don't mess with it we can up-alignment cast
37 try_cast_slice::<u8, u32>(the_bytes).unwrap();
38 }
39
40 #[test]
test_try_cast_slice_mut()41 fn test_try_cast_slice_mut() {
42 // some align4 data
43 let u32_slice: &mut [u32] = &mut [4, 5, 6];
44 let u32_len = u32_slice.len();
45 let u32_ptr = u32_slice.as_ptr();
46
47 // the same data as align1
48 let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap();
49 let the_bytes_len = the_bytes.len();
50 let the_bytes_ptr = the_bytes.as_ptr();
51
52 assert_eq!(
53 u32_ptr as *const u32 as usize,
54 the_bytes_ptr as *const u8 as usize
55 );
56 assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<u8>());
57
58 // by taking one byte off the front, we're definitely mis-aligned for u32.
59 let mis_aligned_bytes = &mut the_bytes[1..];
60 assert_eq!(
61 try_cast_slice_mut::<u8, u32>(mis_aligned_bytes),
62 Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
63 );
64
65 // by taking one byte off the end, we're aligned but would have slop bytes for u32
66 let the_bytes_len_minus1 = the_bytes.len() - 1;
67 let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
68 assert_eq!(
69 try_cast_slice_mut::<u8, u32>(slop_bytes),
70 Err(PodCastError::OutputSliceWouldHaveSlop)
71 );
72
73 // if we don't mess with it we can up-alignment cast
74 try_cast_slice_mut::<u8, u32>(the_bytes).unwrap();
75 }
76
77 #[test]
test_types()78 fn test_types() {
79 let _: i32 = cast(1.0_f32);
80 let _: &mut i32 = cast_mut(&mut 1.0_f32);
81 let _: &i32 = cast_ref(&1.0_f32);
82 let _: &[i32] = cast_slice(&[1.0_f32]);
83 let _: &mut [i32] = cast_slice_mut(&mut [1.0_f32]);
84 //
85 let _: Result<i32, PodCastError> = try_cast(1.0_f32);
86 let _: Result<&mut i32, PodCastError> = try_cast_mut(&mut 1.0_f32);
87 let _: Result<&i32, PodCastError> = try_cast_ref(&1.0_f32);
88 let _: Result<&[i32], PodCastError> = try_cast_slice(&[1.0_f32]);
89 let _: Result<&mut [i32], PodCastError> = try_cast_slice_mut(&mut [1.0_f32]);
90 }
91