use core::mem::size_of; use lokacore::*; #[test] fn test_try_cast_slice() { // some align4 data let u32_slice: &[u32] = &[4, 5, 6]; // the same data as align1 let the_bytes: &[u8] = try_cast_slice(u32_slice).unwrap(); assert_eq!( u32_slice.as_ptr() as *const u32 as usize, the_bytes.as_ptr() as *const u8 as usize ); assert_eq!( u32_slice.len() * size_of::(), the_bytes.len() * size_of::() ); // by taking one byte off the front, we're definitely mis-aligned for u32. let mis_aligned_bytes = &the_bytes[1..]; assert_eq!( try_cast_slice::(mis_aligned_bytes), Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) ); // by taking one byte off the end, we're aligned but would have slop bytes for u32 let the_bytes_len_minus1 = the_bytes.len() - 1; let slop_bytes = &the_bytes[..the_bytes_len_minus1]; assert_eq!( try_cast_slice::(slop_bytes), Err(PodCastError::OutputSliceWouldHaveSlop) ); // if we don't mess with it we can up-alignment cast try_cast_slice::(the_bytes).unwrap(); } #[test] fn test_try_cast_slice_mut() { // some align4 data let u32_slice: &mut [u32] = &mut [4, 5, 6]; let u32_len = u32_slice.len(); let u32_ptr = u32_slice.as_ptr(); // the same data as align1 let the_bytes: &mut [u8] = try_cast_slice_mut(u32_slice).unwrap(); let the_bytes_len = the_bytes.len(); let the_bytes_ptr = the_bytes.as_ptr(); assert_eq!( u32_ptr as *const u32 as usize, the_bytes_ptr as *const u8 as usize ); assert_eq!(u32_len * size_of::(), the_bytes_len * size_of::()); // by taking one byte off the front, we're definitely mis-aligned for u32. let mis_aligned_bytes = &mut the_bytes[1..]; assert_eq!( try_cast_slice_mut::(mis_aligned_bytes), Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned) ); // by taking one byte off the end, we're aligned but would have slop bytes for u32 let the_bytes_len_minus1 = the_bytes.len() - 1; let slop_bytes = &mut the_bytes[..the_bytes_len_minus1]; assert_eq!( try_cast_slice_mut::(slop_bytes), Err(PodCastError::OutputSliceWouldHaveSlop) ); // if we don't mess with it we can up-alignment cast try_cast_slice_mut::(the_bytes).unwrap(); }