1 #![allow(clippy::unnecessary_cast)]
2 #![allow(clippy::manual_slice_size_calculation)]
3 
4 use core::mem::size_of;
5 
6 use bytemuck::*;
7 
8 #[test]
test_try_cast_slice()9 fn test_try_cast_slice() {
10   // some align4 data
11   let u32_slice: &[u32] = &[4, 5, 6];
12   // the same data as align1
13   let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap();
14 
15   assert_eq!(
16     u32_slice.as_ptr() as *const u32 as usize,
17     the_bytes.as_ptr() as *const u8 as usize
18   );
19   assert_eq!(
20     u32_slice.len() * size_of::<u32>(),
21     the_bytes.len() * size_of::<u8>()
22   );
23 
24   // by taking one byte off the front, we're definitely mis-aligned for u32.
25   let mis_aligned_bytes = &the_bytes[1..];
26   assert_eq!(
27     try_cast_slice::<u8, u32>(mis_aligned_bytes),
28     Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
29   );
30 
31   // by taking one byte off the end, we're aligned but would have slop bytes for
32   // u32
33   let the_bytes_len_minus1 = the_bytes.len() - 1;
34   let slop_bytes = &the_bytes[..the_bytes_len_minus1];
35   assert_eq!(
36     try_cast_slice::<u8, u32>(slop_bytes),
37     Err(PodCastError::OutputSliceWouldHaveSlop)
38   );
39 
40   // if we don't mess with it we can up-alignment cast
41   try_cast_slice::<u8, u32>(the_bytes).unwrap();
42 }
43 
44 #[test]
test_try_cast_slice_mut()45 fn test_try_cast_slice_mut() {
46   // some align4 data
47   let u32_slice: &mut [u32] = &mut [4, 5, 6];
48   let u32_len = u32_slice.len();
49   let u32_ptr = u32_slice.as_ptr();
50 
51   // the same data as align1
52   let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap();
53   let the_bytes_len = the_bytes.len();
54   let the_bytes_ptr = the_bytes.as_ptr();
55 
56   assert_eq!(
57     u32_ptr as *const u32 as usize,
58     the_bytes_ptr as *const u8 as usize
59   );
60   assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<u8>());
61 
62   // by taking one byte off the front, we're definitely mis-aligned for u32.
63   let mis_aligned_bytes = &mut the_bytes[1..];
64   assert_eq!(
65     try_cast_slice_mut::<u8, u32>(mis_aligned_bytes),
66     Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
67   );
68 
69   // by taking one byte off the end, we're aligned but would have slop bytes for
70   // u32
71   let the_bytes_len_minus1 = the_bytes.len() - 1;
72   let slop_bytes = &mut the_bytes[..the_bytes_len_minus1];
73   assert_eq!(
74     try_cast_slice_mut::<u8, u32>(slop_bytes),
75     Err(PodCastError::OutputSliceWouldHaveSlop)
76   );
77 
78   // if we don't mess with it we can up-alignment cast
79   try_cast_slice_mut::<u8, u32>(the_bytes).unwrap();
80 }
81 
82 #[test]
test_types()83 fn test_types() {
84   let _: i32 = cast(1.0_f32);
85   let _: &mut i32 = cast_mut(&mut 1.0_f32);
86   let _: &i32 = cast_ref(&1.0_f32);
87   let _: &[i32] = cast_slice(&[1.0_f32]);
88   let _: &mut [i32] = cast_slice_mut(&mut [1.0_f32]);
89   //
90   let _: Result<i32, PodCastError> = try_cast(1.0_f32);
91   let _: Result<&mut i32, PodCastError> = try_cast_mut(&mut 1.0_f32);
92   let _: Result<&i32, PodCastError> = try_cast_ref(&1.0_f32);
93   let _: Result<&[i32], PodCastError> = try_cast_slice(&[1.0_f32]);
94   let _: Result<&mut [i32], PodCastError> = try_cast_slice_mut(&mut [1.0_f32]);
95 }
96 
97 #[test]
test_bytes_of()98 fn test_bytes_of() {
99   assert_eq!(bytes_of(&0xaabbccdd_u32), &0xaabbccdd_u32.to_ne_bytes());
100   assert_eq!(
101     bytes_of_mut(&mut 0xaabbccdd_u32),
102     &mut 0xaabbccdd_u32.to_ne_bytes()
103   );
104   let mut a = 0xaabbccdd_u32;
105   let a_addr = &a as *const _ as usize;
106   // ensure addresses match.
107   assert_eq!(bytes_of(&a).as_ptr() as usize, a_addr);
108   assert_eq!(bytes_of_mut(&mut a).as_ptr() as usize, a_addr);
109 }
110 
111 #[test]
test_try_from_bytes()112 fn test_try_from_bytes() {
113   let u32s = [0xaabbccdd, 0x11223344_u32];
114   let bytes = bytemuck::cast_slice::<u32, u8>(&u32s);
115   assert_eq!(try_from_bytes::<u32>(&bytes[..4]), Ok(&u32s[0]));
116   assert_eq!(
117     try_from_bytes::<u32>(&bytes[..5]),
118     Err(PodCastError::SizeMismatch)
119   );
120   assert_eq!(
121     try_from_bytes::<u32>(&bytes[..3]),
122     Err(PodCastError::SizeMismatch)
123   );
124   assert_eq!(
125     try_from_bytes::<u32>(&bytes[1..5]),
126     Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
127   );
128 }
129 
130 #[test]
test_try_from_bytes_mut()131 fn test_try_from_bytes_mut() {
132   let mut abcd = 0xaabbccdd;
133   let mut u32s = [abcd, 0x11223344_u32];
134   let bytes = bytemuck::cast_slice_mut::<u32, u8>(&mut u32s);
135   assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
136   assert_eq!(try_from_bytes_mut::<u32>(&mut bytes[..4]), Ok(&mut abcd));
137   assert_eq!(
138     try_from_bytes_mut::<u32>(&mut bytes[..5]),
139     Err(PodCastError::SizeMismatch)
140   );
141   assert_eq!(
142     try_from_bytes_mut::<u32>(&mut bytes[..3]),
143     Err(PodCastError::SizeMismatch)
144   );
145   assert_eq!(
146     try_from_bytes::<u32>(&bytes[1..5]),
147     Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
148   );
149 }
150 
151 #[test]
test_from_bytes()152 fn test_from_bytes() {
153   let abcd = 0xaabbccdd_u32;
154   let aligned_bytes = bytemuck::bytes_of(&abcd);
155   assert_eq!(from_bytes::<u32>(aligned_bytes), &abcd);
156   assert!(core::ptr::eq(from_bytes(aligned_bytes), &abcd));
157 }
158 
159 #[test]
test_from_bytes_mut()160 fn test_from_bytes_mut() {
161   let mut a = 0xaabbccdd_u32;
162   let a_addr = &a as *const _ as usize;
163   let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
164   assert_eq!(*from_bytes_mut::<u32>(aligned_bytes), 0xaabbccdd_u32);
165   assert_eq!(
166     from_bytes_mut::<u32>(aligned_bytes) as *const u32 as usize,
167     a_addr
168   );
169 }
170 
171 // like #[should_panic], but can be a part of another test, instead of requiring
172 // it to be it's own test.
173 macro_rules! should_panic {
174   ($ex:expr) => {
175     assert!(
176       std::panic::catch_unwind(|| {
177         let _ = $ex;
178       })
179       .is_err(),
180       concat!("should have panicked: `", stringify!($ex), "`")
181     );
182   };
183 }
184 
185 #[test]
test_panics()186 fn test_panics() {
187   should_panic!(cast_slice::<u8, u32>(&[1u8, 2u8]));
188   should_panic!(cast_slice_mut::<u8, u32>(&mut [1u8, 2u8]));
189   should_panic!(from_bytes::<u32>(&[1u8, 2]));
190   should_panic!(from_bytes::<u32>(&[1u8, 2, 3, 4, 5]));
191   should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2]));
192   should_panic!(from_bytes_mut::<u32>(&mut [1u8, 2, 3, 4, 5]));
193   // use cast_slice on some u32s to get some align>=4 bytes, so we can know
194   // we'll give from_bytes unaligned ones.
195   let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
196   should_panic!(from_bytes::<u32>(&aligned_bytes[1..5]));
197 }
198 
199 #[test]
test_zsts()200 fn test_zsts() {
201   #[derive(Debug, Clone, Copy)]
202   struct MyZst;
203   unsafe impl Zeroable for MyZst {}
204   unsafe impl Pod for MyZst {}
205   assert_eq!(42, cast_slice::<(), MyZst>(&[(); 42]).len());
206   assert_eq!(42, cast_slice_mut::<(), MyZst>(&mut [(); 42]).len());
207   assert_eq!(0, cast_slice::<(), u8>(&[(); 42]).len());
208   assert_eq!(0, cast_slice_mut::<(), u8>(&mut [(); 42]).len());
209   assert_eq!(0, cast_slice::<u8, ()>(&[]).len());
210   assert_eq!(0, cast_slice_mut::<u8, ()>(&mut []).len());
211 
212   assert_eq!(
213     PodCastError::OutputSliceWouldHaveSlop,
214     try_cast_slice::<u8, ()>(&[42]).unwrap_err()
215   );
216 
217   assert_eq!(
218     PodCastError::OutputSliceWouldHaveSlop,
219     try_cast_slice_mut::<u8, ()>(&mut [42]).unwrap_err()
220   );
221 }
222 
223 #[cfg(feature = "extern_crate_alloc")]
224 #[test]
test_boxed_slices()225 fn test_boxed_slices() {
226   let boxed_u8_slice: Box<[u8]> = Box::new([0, 1, u8::MAX, i8::MAX as u8]);
227   let boxed_i8_slice: Box<[i8]> = cast_slice_box::<u8, i8>(boxed_u8_slice);
228   assert_eq!(&*boxed_i8_slice, [0, 1, -1, i8::MAX]);
229 
230   let result: Result<Box<[u16]>, (PodCastError, Box<[i8]>)> =
231     try_cast_slice_box(boxed_i8_slice);
232   let (error, boxed_i8_slice) =
233     result.expect_err("u16 and i8 have different alignment");
234   assert_eq!(error, PodCastError::AlignmentMismatch);
235 
236   let result: Result<&[[i8; 3]], PodCastError> =
237     try_cast_slice(&*boxed_i8_slice);
238   let error =
239     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
240   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
241 
242   let result: Result<Box<[[i8; 3]]>, (PodCastError, Box<[i8]>)> =
243     try_cast_slice_box(boxed_i8_slice);
244   let (error, boxed_i8_slice) =
245     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
246   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
247 
248   let empty: Box<[()]> = cast_slice_box::<u8, ()>(Box::new([]));
249   assert!(empty.is_empty());
250 
251   let result: Result<Box<[()]>, (PodCastError, Box<[i8]>)> =
252     try_cast_slice_box(boxed_i8_slice);
253   let (error, boxed_i8_slice) =
254     result.expect_err("slice of ZST cannot be made from slice of 4 u8s");
255   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
256 
257   drop(boxed_i8_slice);
258 
259   let empty: Box<[i8]> = cast_slice_box::<(), i8>(Box::new([]));
260   assert!(empty.is_empty());
261 
262   let empty: Box<[i8]> = cast_slice_box::<(), i8>(Box::new([(); 42]));
263   assert!(empty.is_empty());
264 }
265 
266 #[cfg(feature = "extern_crate_alloc")]
267 #[test]
test_rc_slices()268 fn test_rc_slices() {
269   use std::rc::Rc;
270   let rc_u8_slice: Rc<[u8]> = Rc::new([0, 1, u8::MAX, i8::MAX as u8]);
271   let rc_i8_slice: Rc<[i8]> = cast_slice_rc::<u8, i8>(rc_u8_slice);
272   assert_eq!(&*rc_i8_slice, [0, 1, -1, i8::MAX]);
273 
274   let result: Result<Rc<[u16]>, (PodCastError, Rc<[i8]>)> =
275     try_cast_slice_rc(rc_i8_slice);
276   let (error, rc_i8_slice) =
277     result.expect_err("u16 and i8 have different alignment");
278   assert_eq!(error, PodCastError::AlignmentMismatch);
279 
280   let result: Result<&[[i8; 3]], PodCastError> = try_cast_slice(&*rc_i8_slice);
281   let error =
282     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
283   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
284 
285   let result: Result<Rc<[[i8; 3]]>, (PodCastError, Rc<[i8]>)> =
286     try_cast_slice_rc(rc_i8_slice);
287   let (error, rc_i8_slice) =
288     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
289   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
290 
291   let empty: Rc<[()]> = cast_slice_rc::<u8, ()>(Rc::new([]));
292   assert!(empty.is_empty());
293 
294   let result: Result<Rc<[()]>, (PodCastError, Rc<[i8]>)> =
295     try_cast_slice_rc(rc_i8_slice);
296   let (error, rc_i8_slice) =
297     result.expect_err("slice of ZST cannot be made from slice of 4 u8s");
298   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
299 
300   drop(rc_i8_slice);
301 
302   let empty: Rc<[i8]> = cast_slice_rc::<(), i8>(Rc::new([]));
303   assert!(empty.is_empty());
304 
305   let empty: Rc<[i8]> = cast_slice_rc::<(), i8>(Rc::new([(); 42]));
306   assert!(empty.is_empty());
307 }
308 
309 #[cfg(feature = "extern_crate_alloc")]
310 #[cfg(target_has_atomic = "ptr")]
311 #[test]
test_arc_slices()312 fn test_arc_slices() {
313   use std::sync::Arc;
314   let arc_u8_slice: Arc<[u8]> = Arc::new([0, 1, u8::MAX, i8::MAX as u8]);
315   let arc_i8_slice: Arc<[i8]> = cast_slice_arc::<u8, i8>(arc_u8_slice);
316   assert_eq!(&*arc_i8_slice, [0, 1, -1, i8::MAX]);
317 
318   let result: Result<Arc<[u16]>, (PodCastError, Arc<[i8]>)> =
319     try_cast_slice_arc(arc_i8_slice);
320   let (error, arc_i8_slice) =
321     result.expect_err("u16 and i8 have different alignment");
322   assert_eq!(error, PodCastError::AlignmentMismatch);
323 
324   let result: Result<&[[i8; 3]], PodCastError> = try_cast_slice(&*arc_i8_slice);
325   let error =
326     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
327   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
328 
329   let result: Result<Arc<[[i8; 3]]>, (PodCastError, Arc<[i8]>)> =
330     try_cast_slice_arc(arc_i8_slice);
331   let (error, arc_i8_slice) =
332     result.expect_err("slice of [i8; 3] cannot be made from slice of 4 i8s");
333   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
334 
335   let empty: Arc<[()]> = cast_slice_arc::<u8, ()>(Arc::new([]));
336   assert!(empty.is_empty());
337 
338   let result: Result<Arc<[()]>, (PodCastError, Arc<[i8]>)> =
339     try_cast_slice_arc(arc_i8_slice);
340   let (error, arc_i8_slice) =
341     result.expect_err("slice of ZST cannot be made from slice of 4 u8s");
342   assert_eq!(error, PodCastError::OutputSliceWouldHaveSlop);
343 
344   drop(arc_i8_slice);
345 
346   let empty: Arc<[i8]> = cast_slice_arc::<(), i8>(Arc::new([]));
347   assert!(empty.is_empty());
348 
349   let empty: Arc<[i8]> = cast_slice_arc::<(), i8>(Arc::new([(); 42]));
350   assert!(empty.is_empty());
351 }
352 
353 #[cfg(feature = "extern_crate_alloc")]
354 #[test]
box_bytes_zst()355 fn box_bytes_zst() {
356   let x: BoxBytes = box_bytes_of(Box::new([0u8; 0]));
357   let _: Box<[u8]> = from_box_bytes(x);
358 
359   let x: BoxBytes = box_bytes_of(Box::new([0u8; 0]));
360   let _: Box<[()]> = from_box_bytes(x);
361 
362   let x: BoxBytes = box_bytes_of(Box::new([(); 0]));
363   let _: Box<[u8]> = from_box_bytes(x);
364 
365   let x: BoxBytes = box_bytes_of(Box::new([0u8]));
366   let res: Result<Box<[()]>, _> = try_from_box_bytes(x);
367   assert_eq!(res.unwrap_err().0, PodCastError::OutputSliceWouldHaveSlop);
368 
369   // regression test for dropping zero-sized BoxBytes
370   let _: BoxBytes = box_bytes_of(Box::new([0u8; 0]));
371 }
372