mirror of
https://github.com/Lokathor/bytemuck.git
synced 2024-11-23 15:22:24 +00:00
Fix 177 (#178)
* reduce required bounds. * clippy get off my case * spelling * clippy lints on tests.
This commit is contained in:
parent
1039388f0b
commit
1b1b9a0e89
@ -209,7 +209,7 @@ pub fn cast_vec<A: NoUninit, B: AnyBitPattern>(input: Vec<A>) -> Vec<B> {
|
||||
/// alignment.
|
||||
/// * The start and end content size in bytes of the `Vec` must be the exact
|
||||
/// same.
|
||||
/// * The start and end capacity in bytes of the `Vec` mest be the exact same.
|
||||
/// * The start and end capacity in bytes of the `Vec` must be the exact same.
|
||||
#[inline]
|
||||
pub fn try_cast_vec<A: NoUninit, B: AnyBitPattern>(
|
||||
input: Vec<A>,
|
||||
@ -282,10 +282,7 @@ pub fn try_cast_vec<A: NoUninit, B: AnyBitPattern>(
|
||||
/// assert_eq!(&vec_of_words[..], &[0x0005_0006, 0x0007_0008][..])
|
||||
/// }
|
||||
/// ```
|
||||
pub fn pod_collect_to_vec<
|
||||
A: NoUninit + AnyBitPattern,
|
||||
B: NoUninit + AnyBitPattern,
|
||||
>(
|
||||
pub fn pod_collect_to_vec<A: NoUninit, B: NoUninit + AnyBitPattern>(
|
||||
src: &[A],
|
||||
) -> Vec<B> {
|
||||
let src_size = size_of_val(src);
|
||||
|
@ -1,6 +1,7 @@
|
||||
#![no_std]
|
||||
#![warn(missing_docs)]
|
||||
#![allow(clippy::match_like_matches_macro)]
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
#![cfg_attr(feature = "nightly_portable_simd", feature(portable_simd))]
|
||||
#![cfg_attr(feature = "nightly_stdsimd", feature(stdsimd))]
|
||||
|
||||
|
@ -140,7 +140,7 @@ fn test_try_from_bytes_mut() {
|
||||
Err(PodCastError::SizeMismatch)
|
||||
);
|
||||
assert_eq!(
|
||||
try_from_bytes::<u32>(&mut bytes[1..5]),
|
||||
try_from_bytes::<u32>(&bytes[1..5]),
|
||||
Err(PodCastError::TargetAlignmentGreaterAndInputNotAligned)
|
||||
);
|
||||
}
|
||||
|
@ -1,11 +1,18 @@
|
||||
use core::{mem::size_of, num::{NonZeroU32, NonZeroU8}};
|
||||
use core::{
|
||||
mem::size_of,
|
||||
num::{NonZeroU32, NonZeroU8},
|
||||
};
|
||||
|
||||
use bytemuck::{*, checked::CheckedCastError};
|
||||
use bytemuck::{checked::CheckedCastError, *};
|
||||
|
||||
#[test]
|
||||
fn test_try_cast_slice() {
|
||||
// some align4 data
|
||||
let nonzero_u32_slice: &[NonZeroU32] = &[NonZeroU32::new(4).unwrap(), NonZeroU32::new(5).unwrap(), NonZeroU32::new(6).unwrap()];
|
||||
let nonzero_u32_slice: &[NonZeroU32] = &[
|
||||
NonZeroU32::new(4).unwrap(),
|
||||
NonZeroU32::new(5).unwrap(),
|
||||
NonZeroU32::new(6).unwrap(),
|
||||
];
|
||||
|
||||
// contains bytes with invalid bitpattern for NonZeroU8
|
||||
assert_eq!(
|
||||
@ -25,11 +32,14 @@ fn test_try_cast_slice() {
|
||||
the_bytes.len() * size_of::<u8>()
|
||||
);
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for NonZeroU32.
|
||||
// by taking one byte off the front, we're definitely mis-aligned for
|
||||
// NonZeroU32.
|
||||
let mis_aligned_bytes = &the_bytes[1..];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice::<u8, NonZeroU32>(mis_aligned_bytes),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
@ -62,7 +72,8 @@ fn test_try_cast_slice_mut() {
|
||||
let u32_ptr = u32_slice.as_ptr();
|
||||
|
||||
// the same data as align1, nonzero bytes
|
||||
let the_nonzero_bytes: &mut [NonZeroU8] = checked::try_cast_slice_mut(u32_slice).unwrap();
|
||||
let the_nonzero_bytes: &mut [NonZeroU8] =
|
||||
checked::try_cast_slice_mut(u32_slice).unwrap();
|
||||
let the_nonzero_bytes_len = the_nonzero_bytes.len();
|
||||
let the_nonzero_bytes_ptr = the_nonzero_bytes.as_ptr();
|
||||
|
||||
@ -70,7 +81,10 @@ fn test_try_cast_slice_mut() {
|
||||
u32_ptr as *const u32 as usize,
|
||||
the_nonzero_bytes_ptr as *const NonZeroU8 as usize
|
||||
);
|
||||
assert_eq!(u32_len * size_of::<u32>(), the_nonzero_bytes_len * size_of::<NonZeroU8>());
|
||||
assert_eq!(
|
||||
u32_len * size_of::<u32>(),
|
||||
the_nonzero_bytes_len * size_of::<NonZeroU8>()
|
||||
);
|
||||
|
||||
// the same data as align1
|
||||
let the_bytes: &mut [u8] = checked::try_cast_slice_mut(u32_slice).unwrap();
|
||||
@ -81,13 +95,18 @@ fn test_try_cast_slice_mut() {
|
||||
u32_ptr as *const u32 as usize,
|
||||
the_bytes_ptr as *const u8 as usize
|
||||
);
|
||||
assert_eq!(u32_len * size_of::<u32>(), the_bytes_len * size_of::<NonZeroU8>());
|
||||
assert_eq!(
|
||||
u32_len * size_of::<u32>(),
|
||||
the_bytes_len * size_of::<NonZeroU8>()
|
||||
);
|
||||
|
||||
// by taking one byte off the front, we're definitely mis-aligned for u32.
|
||||
let mis_aligned_bytes = &mut the_bytes[1..];
|
||||
assert_eq!(
|
||||
checked::try_cast_slice_mut::<u8, NonZeroU32>(mis_aligned_bytes),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
// by taking one byte off the end, we're aligned but would have slop bytes for
|
||||
@ -99,7 +118,8 @@ fn test_try_cast_slice_mut() {
|
||||
Err(CheckedCastError::PodCastError(PodCastError::OutputSliceWouldHaveSlop))
|
||||
);
|
||||
|
||||
// if we don't mess with it we can up-alignment cast, since there are no zeroes in the original slice
|
||||
// if we don't mess with it we can up-alignment cast, since there are no
|
||||
// zeroes in the original slice
|
||||
checked::try_cast_slice_mut::<u8, NonZeroU32>(the_bytes).unwrap();
|
||||
}
|
||||
|
||||
@ -112,10 +132,14 @@ fn test_types() {
|
||||
let _: &mut [NonZeroU32] = checked::cast_slice_mut(&mut [1.0_f32]);
|
||||
//
|
||||
let _: Result<NonZeroU32, CheckedCastError> = checked::try_cast(1.0_f32);
|
||||
let _: Result<&mut NonZeroU32, CheckedCastError> = checked::try_cast_mut(&mut 1.0_f32);
|
||||
let _: Result<&NonZeroU32, CheckedCastError> = checked::try_cast_ref(&1.0_f32);
|
||||
let _: Result<&[NonZeroU32], CheckedCastError> = checked::try_cast_slice(&[1.0_f32]);
|
||||
let _: Result<&mut [NonZeroU32], CheckedCastError> = checked::try_cast_slice_mut(&mut [1.0_f32]);
|
||||
let _: Result<&mut NonZeroU32, CheckedCastError> =
|
||||
checked::try_cast_mut(&mut 1.0_f32);
|
||||
let _: Result<&NonZeroU32, CheckedCastError> =
|
||||
checked::try_cast_ref(&1.0_f32);
|
||||
let _: Result<&[NonZeroU32], CheckedCastError> =
|
||||
checked::try_cast_slice(&[1.0_f32]);
|
||||
let _: Result<&mut [NonZeroU32], CheckedCastError> =
|
||||
checked::try_cast_slice_mut(&mut [1.0_f32]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -145,9 +169,15 @@ fn test_try_pod_read_unaligned() {
|
||||
|
||||
#[test]
|
||||
fn test_try_from_bytes() {
|
||||
let nonzero_u32s = [NonZeroU32::new(0xaabbccdd).unwrap(), NonZeroU32::new(0x11223344).unwrap()];
|
||||
let nonzero_u32s = [
|
||||
NonZeroU32::new(0xaabbccdd).unwrap(),
|
||||
NonZeroU32::new(0x11223344).unwrap(),
|
||||
];
|
||||
let bytes = bytemuck::checked::cast_slice::<NonZeroU32, u8>(&nonzero_u32s);
|
||||
assert_eq!(checked::try_from_bytes::<NonZeroU32>(&bytes[..4]), Ok(&nonzero_u32s[0]));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..4]),
|
||||
Ok(&nonzero_u32s[0])
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
@ -158,7 +188,9 @@ fn test_try_from_bytes() {
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
let zero_u32s = [0, 0x11223344_u32];
|
||||
@ -167,7 +199,10 @@ fn test_try_from_bytes() {
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..4]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(checked::try_from_bytes::<NonZeroU32>(&bytes[4..]), Ok(&NonZeroU32::new(zero_u32s[1]).unwrap()));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[4..]),
|
||||
Ok(&NonZeroU32::new(zero_u32s[1]).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
@ -178,7 +213,9 @@ fn test_try_from_bytes() {
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@ -188,8 +225,14 @@ fn test_try_from_bytes_mut() {
|
||||
let b = 0x11223344_u32;
|
||||
let mut u32s = [a, b];
|
||||
let bytes = bytemuck::checked::cast_slice_mut::<u32, u8>(&mut u32s);
|
||||
assert_eq!(checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..4]), Ok(&mut NonZeroU32::new(a).unwrap()));
|
||||
assert_eq!(checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]), Ok(&mut NonZeroU32::new(b).unwrap()));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..4]),
|
||||
Ok(&mut NonZeroU32::new(a).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]),
|
||||
Ok(&mut NonZeroU32::new(b).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
@ -199,8 +242,10 @@ fn test_try_from_bytes_mut() {
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&mut bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
|
||||
let mut u32s = [0, b];
|
||||
@ -209,7 +254,10 @@ fn test_try_from_bytes_mut() {
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..4]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]), Ok(&mut NonZeroU32::new(b).unwrap()));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[4..]),
|
||||
Ok(&mut NonZeroU32::new(b).unwrap())
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes_mut::<NonZeroU32>(&mut bytes[..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
@ -219,8 +267,10 @@ fn test_try_from_bytes_mut() {
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<NonZeroU32>(&mut bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::TargetAlignmentGreaterAndInputNotAligned))
|
||||
checked::try_from_bytes::<NonZeroU32>(&bytes[1..5]),
|
||||
Err(CheckedCastError::PodCastError(
|
||||
PodCastError::TargetAlignmentGreaterAndInputNotAligned
|
||||
))
|
||||
);
|
||||
}
|
||||
|
||||
@ -228,8 +278,14 @@ fn test_try_from_bytes_mut() {
|
||||
fn test_from_bytes() {
|
||||
let abcd = 0xaabbccdd_u32;
|
||||
let aligned_bytes = bytemuck::bytes_of(&abcd);
|
||||
assert_eq!(checked::from_bytes::<NonZeroU32>(aligned_bytes), &NonZeroU32::new(abcd).unwrap());
|
||||
assert!(core::ptr::eq(checked::from_bytes(aligned_bytes) as *const NonZeroU32 as *const u32, &abcd));
|
||||
assert_eq!(
|
||||
checked::from_bytes::<NonZeroU32>(aligned_bytes),
|
||||
&NonZeroU32::new(abcd).unwrap()
|
||||
);
|
||||
assert!(core::ptr::eq(
|
||||
checked::from_bytes(aligned_bytes) as *const NonZeroU32 as *const u32,
|
||||
&abcd
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -237,9 +293,13 @@ fn test_from_bytes_mut() {
|
||||
let mut a = 0xaabbccdd_u32;
|
||||
let a_addr = &a as *const _ as usize;
|
||||
let aligned_bytes = bytemuck::bytes_of_mut(&mut a);
|
||||
assert_eq!(*checked::from_bytes_mut::<NonZeroU32>(aligned_bytes), NonZeroU32::new(0xaabbccdd).unwrap());
|
||||
assert_eq!(
|
||||
checked::from_bytes_mut::<NonZeroU32>(aligned_bytes) as *const NonZeroU32 as usize,
|
||||
*checked::from_bytes_mut::<NonZeroU32>(aligned_bytes),
|
||||
NonZeroU32::new(0xaabbccdd).unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
checked::from_bytes_mut::<NonZeroU32>(aligned_bytes) as *const NonZeroU32
|
||||
as usize,
|
||||
a_addr
|
||||
);
|
||||
}
|
||||
@ -274,33 +334,55 @@ fn test_panics() {
|
||||
let aligned_bytes = bytemuck::cast_slice::<u32, u8>(&[0, 0]);
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(aligned_bytes));
|
||||
should_panic!(checked::from_bytes::<NonZeroU32>(&aligned_bytes[1..5]));
|
||||
should_panic!(checked::pod_read_unaligned::<NonZeroU32>(&aligned_bytes[1..5]));
|
||||
should_panic!(checked::pod_read_unaligned::<NonZeroU32>(
|
||||
&aligned_bytes[1..5]
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_char() {
|
||||
assert_eq!(checked::try_cast::<u32, char>(0), Ok('\0'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xd7ff), Ok('\u{d7ff}'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xd800), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xdfff), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0xd800),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0xdfff),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(checked::try_cast::<u32, char>(0xe000), Ok('\u{e000}'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0x10ffff), Ok('\u{10ffff}'));
|
||||
assert_eq!(checked::try_cast::<u32, char>(0x110000), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(checked::try_cast::<u32, char>(-1i32 as u32), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(0x110000),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<u32, char>(-1i32 as u32),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[test]
|
||||
fn test_bool() {
|
||||
assert_eq!(checked::try_cast::<u8, bool>(0), Ok(false));
|
||||
assert_eq!(checked::try_cast::<u8, bool>(1), Ok(true));
|
||||
for i in 2..=255 {
|
||||
assert_eq!(checked::try_cast::<u8, bool>(i), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(
|
||||
checked::try_cast::<u8, bool>(i),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
}
|
||||
|
||||
assert_eq!(checked::try_from_bytes::<bool>(&[1]), Ok(&true));
|
||||
assert_eq!(checked::try_from_bytes::<bool>(&[3]), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(checked::try_from_bytes::<bool>(&[0, 1]), Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch)));
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<bool>(&[3]),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_from_bytes::<bool>(&[0, 1]),
|
||||
Err(CheckedCastError::PodCastError(PodCastError::SizeMismatch))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -308,8 +390,14 @@ fn test_all_nonzero() {
|
||||
use core::num::*;
|
||||
macro_rules! test_nonzero {
|
||||
($nonzero:ty: $primitive:ty) => {
|
||||
assert_eq!(checked::try_cast::<$primitive, $nonzero>(0), Err(CheckedCastError::InvalidBitPattern));
|
||||
assert_eq!(checked::try_cast::<$primitive, $nonzero>(1), Ok(<$nonzero>::new(1).unwrap()));
|
||||
assert_eq!(
|
||||
checked::try_cast::<$primitive, $nonzero>(0),
|
||||
Err(CheckedCastError::InvalidBitPattern)
|
||||
);
|
||||
assert_eq!(
|
||||
checked::try_cast::<$primitive, $nonzero>(1),
|
||||
Ok(<$nonzero>::new(1).unwrap())
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,5 @@
|
||||
#![allow(clippy::disallowed_names)]
|
||||
|
||||
//! Cargo miri doesn't run doctests yet, so we duplicate these here. It's
|
||||
//! probably not that important to sweat keeping these perfectly up to date, but
|
||||
//! we should try to catch the cases where the primary tests are doctests.
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![allow(clippy::disallowed_names)]
|
||||
use bytemuck::{offset_of, Zeroable};
|
||||
|
||||
#[test]
|
||||
|
@ -1,3 +1,4 @@
|
||||
#![allow(clippy::uninlined_format_args)]
|
||||
//! The integration tests seem to always have `std` linked, so things that would
|
||||
//! depend on that can go here.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user