From f63faee85e4249baaff7c40e1a287abb5c6dbdd0 Mon Sep 17 00:00:00 2001 From: Gary Guo Date: Fri, 27 Sep 2024 23:51:03 +0100 Subject: [PATCH] read: use const generics in `src/read/util.rs` The MSRV has been bumped past the minimum required for const generics. --- src/read/util.rs | 80 +++++++++++++++++++----------------------------- 1 file changed, 31 insertions(+), 49 deletions(-) diff --git a/src/read/util.rs b/src/read/util.rs index 041ca5af..39adab32 100644 --- a/src/read/util.rs +++ b/src/read/util.rs @@ -41,70 +41,52 @@ pub trait ArrayLike: Sealed { fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit]; } -// Use macro since const generics can't be used due to MSRV. -macro_rules! impl_array { - () => {}; - ($n:literal $($rest:tt)*) => { - // SAFETY: does not modify the content in storage. - unsafe impl Sealed for [T; $n] { - type Storage = [MaybeUninit; $n]; - - fn new_storage() -> Self::Storage { - // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. - unsafe { MaybeUninit::uninit().assume_init() } - } - } +// SAFETY: does not modify the content in storage. +unsafe impl Sealed for [T; N] { + type Storage = [MaybeUninit; N]; - impl ArrayLike for [T; $n] { - type Item = T; + fn new_storage() -> Self::Storage { + // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. + unsafe { MaybeUninit::uninit().assume_init() } + } +} - fn as_slice(storage: &Self::Storage) -> &[MaybeUninit] { - storage - } +impl ArrayLike for [T; N] { + type Item = T; - fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit] { - storage - } - } + fn as_slice(storage: &Self::Storage) -> &[MaybeUninit] { + storage + } - impl_array!($($rest)*); + fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit] { + storage } } +// SAFETY: does not modify the content in storage. #[cfg(feature = "read")] -macro_rules! impl_box { - () => {}; - ($n:literal $($rest:tt)*) => { - // SAFETY: does not modify the content in storage. - unsafe impl Sealed for Box<[T; $n]> { - type Storage = Box<[MaybeUninit; $n]>; - - fn new_storage() -> Self::Storage { - // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. - Box::new(unsafe { MaybeUninit::uninit().assume_init() }) - } - } +unsafe impl Sealed for Box<[T; N]> { + type Storage = Box<[MaybeUninit; N]>; - impl ArrayLike for Box<[T; $n]> { - type Item = T; + fn new_storage() -> Self::Storage { + // SAFETY: An uninitialized `[MaybeUninit<_>; _]` is valid. + Box::new(unsafe { MaybeUninit::uninit().assume_init() }) + } +} - fn as_slice(storage: &Self::Storage) -> &[MaybeUninit] { - &storage[..] - } +#[cfg(feature = "read")] +impl ArrayLike for Box<[T; N]> { + type Item = T; - fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit] { - &mut storage[..] - } - } + fn as_slice(storage: &Self::Storage) -> &[MaybeUninit] { + &storage[..] + } - impl_box!($($rest)*); + fn as_mut_slice(storage: &mut Self::Storage) -> &mut [MaybeUninit] { + &mut storage[..] } } -impl_array!(0 1 2 3 4 8 16 32 64 128 192); -#[cfg(feature = "read")] -impl_box!(0 1 2 3 4 8 16 32 64 128 192); - #[cfg(feature = "read")] unsafe impl Sealed for Vec { type Storage = Box<[MaybeUninit]>;