From 63ba0eb91a9de123c9bd5a58dab5c802320afe03 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 30 Dec 2023 16:05:23 -0700 Subject: [PATCH] hybrid-array: `AssociatedArraySize` trait (#1006) Adds a type for associating a `typenum`-based size with a given array type, which can be either `[T; N]` or `Array`. This subsumes the previous `IntoArray` trait, which can be replaced by a simple `Into` bound instead. --- hybrid-array/src/impls.rs | 20 +++++++++----------- hybrid-array/src/lib.rs | 21 +++++++-------------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/hybrid-array/src/impls.rs b/hybrid-array/src/impls.rs index dfe564fb..76cf70b0 100644 --- a/hybrid-array/src/impls.rs +++ b/hybrid-array/src/impls.rs @@ -1,4 +1,4 @@ -use super::{Array, ArrayOps, ArraySize, IntoArray}; +use super::{Array, ArrayOps, ArraySize, AssociatedArraySize}; #[cfg(feature = "zeroize")] use zeroize::{Zeroize, ZeroizeOnDrop}; @@ -27,7 +27,6 @@ macro_rules! impl_array_size { $( impl ArrayOps for Array { const SIZE: usize = $len; - type Size = typenum::$ty; #[inline] fn as_core_array(&self) -> &[T; $len] { @@ -69,17 +68,17 @@ macro_rules! impl_array_size { type ArrayType = [T; $len]; } - impl From> for [T; $len] { - fn from(arr: Array) -> [T; $len] { - arr.0 - } + impl AssociatedArraySize for [T; $len] { + type Size = typenum::$ty; } - impl IntoArray for [T; $len] { + impl AssociatedArraySize for Array { type Size = typenum::$ty; + } - fn into_hybrid_array(self) -> Array { - Array::from_core_array(self) + impl From> for [T; $len] { + fn from(arr: Array) -> [T; $len] { + arr.0 } } )+ @@ -169,10 +168,9 @@ impl_array_size! { impl ArrayOps for [T; N] where - Self: IntoArray, + Self: AssociatedArraySize, { const SIZE: usize = N; - type Size = >::Size; #[inline] fn as_core_array(&self) -> &[T; N] { diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index 1293a8b3..d41656e6 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -536,7 +536,8 @@ fn check_slice_length(slice: &[T]) -> Result<(), TryFromSliceEr /// Array operations which are const generic over a given array size. pub trait ArrayOps: - Borrow<[T; N]> + AssociatedArraySize + + Borrow<[T; N]> + BorrowMut<[T; N]> + From<[T; N]> + Into<[T; N]> @@ -546,14 +547,9 @@ pub trait ArrayOps: { /// Size of an array as a `usize`. /// - /// Not to be confused with [`ArrayOps::Size`], which is `typenum`-based. + /// Not to be confused with [`AssociatedArraySize::Size`], which is [`typenum`]-based. const SIZE: usize; - /// [`ArraySize`] type: `typenum`-provided [`Unsigned`] integer. - /// - /// Not to be confused with [`ArrayOps::SIZE`], which is a `usize`. - type Size: ArraySize; - /// Returns a reference to the inner array. fn as_core_array(&self) -> &[T; N]; @@ -652,16 +648,13 @@ impl ArrayExt for [T; N] { /// It is implemented only for a number of types defined in [`typenum::consts`]. pub unsafe trait ArraySize: Unsigned { /// Array type which corresponds to this size. - type ArrayType: ArrayExt + IntoArray + IntoIterator + SliceOps; + type ArrayType: ArrayExt + Into> + IntoIterator + SliceOps; } -/// Convert the given type into an [`Array`]. -pub trait IntoArray { - /// Size of the [`Array`]. +/// Associates an [`ArraySize`] with a given type. +pub trait AssociatedArraySize: Sized { + /// Size of an array type, expressed as a [`typenum`]-based [`ArraySize`]. type Size: ArraySize; - - /// Convert into the `hybrid-array` crate's [`Array`] type. - fn into_hybrid_array(self) -> Array; } /// Splits the shared slice into a slice of `N`-element arrays, starting at the beginning