From 71ec948f39cf2fcf78fe5a67f967a26e81529972 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 8 Oct 2023 10:37:21 -0600 Subject: [PATCH] hybrid-array: impl `IntoIterator` for all `ArraySize`s (#956) Uses an `IntoIterator` bound on `ArraySize::ArrayType` and forwards the trait invocation to the inner array. --- hybrid-array/src/lib.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index 1212b2c1..d618b332 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -27,7 +27,7 @@ pub use typenum; pub use typenum::consts; use core::{ - array::{IntoIter, TryFromSliceError}, + array::TryFromSliceError, borrow::{Borrow, BorrowMut}, cmp::Ordering, fmt::{self, Debug}, @@ -307,6 +307,21 @@ where } } +impl IntoIterator for Array +where + U: ArraySize, +{ + type Item = T; + type IntoIter = as IntoIterator>::IntoIter; + + /// Creates a consuming iterator, that is, one that moves each value out of + /// the array (from start to end). The array cannot be used after calling + /// this unless `T` implements `Copy`, so the whole array is copied. + fn into_iter(self) -> Self::IntoIter { + self.0.into_iter() + } +} + impl PartialEq for Array where T: PartialEq, @@ -495,9 +510,9 @@ impl ArrayExt for [T; N] { /// /// NOTE: do not implement this trait yourself. It is implemented for types in /// [`typenum::consts`]. -pub unsafe trait ArraySize: Unsigned { +pub unsafe trait ArraySize: Unsigned + 'static { /// Array type which corresponds to this size. - type ArrayType: ArrayExt + AsRef<[T]> + AsMut<[T]> + IntoArray; + type ArrayType: ArrayExt + AsRef<[T]> + AsMut<[T]> + IntoArray + IntoIterator; } /// Convert the given type into an [`Array`]. @@ -570,18 +585,6 @@ macro_rules! impl_array_size { } } - impl IntoIterator for Array { - type Item = T; - type IntoIter = IntoIter; - - /// Creates a consuming iterator, that is, one that moves each value out of - /// the array (from start to end). The array cannot be used after calling - /// this unless `T` implements `Copy`, so the whole array is copied. - fn into_iter(self) -> Self::IntoIter { - self.0.into_iter() - } - } - impl<'a, T> IntoIterator for &'a Array { type Item = &'a T; type IntoIter = Iter<'a, T>;