diff --git a/src/array.rs b/src/array.rs index 346853d..3ef46c2 100644 --- a/src/array.rs +++ b/src/array.rs @@ -1,7 +1,7 @@ use alloc::{borrow::Cow, boxed::Box, sync::Arc, vec::Vec}; use core::{fmt::Debug, hash::Hash, mem::ManuallyDrop, ptr::NonNull}; -use crate::length::{InvalidLength, NonZero, SmallLen, ValidLength}; +use crate::length::{InvalidLength, SmallLen, ValidLength}; #[cold] fn truncate_vec(err: InvalidLength, max_len: usize) -> Vec { @@ -16,7 +16,7 @@ fn truncate_vec(err: InvalidLength, max_len: usize) -> Vec { #[repr(packed)] pub struct FixedArray { ptr: NonNull, - len: LenT::NonZero, + len: LenT, } impl FixedArray { @@ -31,34 +31,14 @@ impl FixedArray { pub fn empty() -> Self { Self { ptr: NonNull::dangling(), - len: LenT::DANGLING, + len: LenT::ZERO, } } /// # Safety /// - `len` must be equal to `ptr.len()` unsafe fn from_box(ptr: Box<[T]>, len: LenT) -> Self { - let len = LenT::NonZero::new(len).unwrap_or(LenT::DANGLING); - - // If the length was 0, the above `unwrap_or` has just set the value to `LenT::DANGLING`. - // If the length was not 0, the invariant is held by the caller. - Self::from_box_with_nonzero(ptr, len) - } - - /// # Safety - /// If the slice is empty: - /// - `len` must be equal to `LenT::DANGLING` - /// - /// If the slice is not empty: - /// - `len` must be equal to `ptr.len()` - #[must_use] - unsafe fn from_box_with_nonzero(ptr: Box<[T]>, len: LenT::NonZero) -> Self { - #[cfg(debug_assertions)] - if ptr.is_empty() { - assert_eq!(len, LenT::DANGLING); - } else { - assert_eq!(len.into().to_usize(), ptr.len()); - } + debug_assert_eq!(len.into().to_usize(), ptr.len()); let array_ptr = Box::into_raw(ptr).cast::(); Self { @@ -79,11 +59,7 @@ impl FixedArray { /// Returns the length of the [`FixedArray`]. #[must_use] pub fn len(&self) -> LenT { - if self.is_empty() { - LenT::ZERO - } else { - self.len.into() - } + self.len } /// Returns if the length is equal to 0. @@ -165,7 +141,7 @@ impl Clone for FixedArray { let ptr = self.as_slice().to_vec().into_boxed_slice(); // SAFETY: The Box::from cannot make the length mismatch. - unsafe { Self::from_box_with_nonzero(ptr, self.len) } + unsafe { Self::from_box(ptr, self.len) } } #[allow(clippy::assigning_clones)] @@ -198,12 +174,6 @@ impl Hash for FixedArray { } impl PartialEq for FixedArray { - // https://github.com/rust-lang/rust-clippy/issues/12154 - #[allow( - unknown_lints, - unconditional_recursion, - clippy::unconditional_recursion - )] fn eq(&self, other: &Self) -> bool { self.as_slice().eq(other.as_slice()) } diff --git a/src/length.rs b/src/length.rs index 5bbd632..e349c9c 100644 --- a/src/length.rs +++ b/src/length.rs @@ -107,6 +107,7 @@ impl TryFrom> for InvalidStrLength { pub trait NonZero: sealed::NonZeroSealed + Into + Sized + Copy + PartialEq + Debug { + #[allow(unused)] fn new(val: Int) -> Option; } @@ -138,8 +139,11 @@ pub trait ValidLength: { const ZERO: Self; const MAX: Self; + #[deprecated = "will be removed in the next major release"] + #[allow(deprecated)] const DANGLING: Self::NonZero; + #[deprecated = "will be removed in the next major release"] type NonZero: NonZero; #[cfg(feature = "typesize")] type InlineStrRepr: Copy + AsRef<[u8]> + AsMut<[u8]> + Default + typesize::TypeSize; @@ -158,6 +162,7 @@ pub trait ValidLength: impl ValidLength for u8 { const ZERO: Self = 0; const MAX: Self = Self::MAX; + #[allow(deprecated)] const DANGLING: Self::NonZero = Self::NonZero::MAX; type NonZero = NonZeroU8; @@ -171,6 +176,7 @@ impl ValidLength for u8 { impl ValidLength for u16 { const ZERO: Self = 0; const MAX: Self = Self::MAX; + #[allow(deprecated)] const DANGLING: Self::NonZero = Self::NonZero::MAX; type NonZero = NonZeroU16; @@ -185,6 +191,7 @@ impl ValidLength for u16 { impl ValidLength for u32 { const ZERO: Self = 0; const MAX: Self = Self::MAX; + #[allow(deprecated)] const DANGLING: Self::NonZero = Self::NonZero::MAX; type NonZero = NonZeroU32; diff --git a/tests/tests.rs b/tests/tests.rs new file mode 100644 index 0000000..53bd508 --- /dev/null +++ b/tests/tests.rs @@ -0,0 +1,7 @@ +use small_fixed_array::FixedArray; + +#[test] +fn check_zst_functionality() { + let array = FixedArray::<(), u32>::from_vec_trunc(vec![(); 16]); + assert_eq!(array.len(), 16); +}