From fc5d6143d34ae6c691da147a5a378d8db5d8c943 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sat, 30 Dec 2023 17:21:56 -0700 Subject: [PATCH] Impl `From>` for `[T; N]` generically (#1010) Uses a const generic implementation bounded on `ArrayOps` rather than using a macro to write a separate one for each array size. --- hybrid-array/src/impls.rs | 6 ------ hybrid-array/src/lib.rs | 15 ++++++++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/hybrid-array/src/impls.rs b/hybrid-array/src/impls.rs index 76cf70b0..55542ffc 100644 --- a/hybrid-array/src/impls.rs +++ b/hybrid-array/src/impls.rs @@ -75,12 +75,6 @@ macro_rules! impl_array_size { impl AssociatedArraySize for Array { type Size = typenum::$ty; } - - impl From> for [T; $len] { - fn from(arr: Array) -> [T; $len] { - arr.0 - } - } )+ }; } diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index 66602ee7..a9462485 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -348,6 +348,18 @@ where } } +impl From> for [T; N] +where + Array: ArrayOps, + U: ArraySize, +{ + #[inline] + fn from(arr: Array) -> [T; N] { + let mut items = arr.0.into_iter(); + core::array::from_fn(|_| items.next().expect("should always have item")) + } +} + impl<'a, T, U, const N: usize> From<&'a [T; N]> for &'a Array where Array: ArrayOps, @@ -543,6 +555,7 @@ pub trait ArrayOps: + Borrow<[T; N]> + BorrowMut<[T; N]> + From<[T; N]> + + FromFn + Into<[T; N]> + IntoIterator + Sized @@ -616,8 +629,8 @@ impl SliceOps for Array {} pub unsafe trait ArraySize: Unsigned { /// Array type which corresponds to this size. type ArrayType: AssociatedArraySize - + FromFn + From> + + FromFn + Into> + IntoIterator + SliceOps;