Skip to content

Commit 71ec948

Browse files
authored
hybrid-array: impl IntoIterator for all ArraySizes (#956)
Uses an `IntoIterator` bound on `ArraySize::ArrayType` and forwards the trait invocation to the inner array.
1 parent c19c5cc commit 71ec948

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

hybrid-array/src/lib.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub use typenum;
2727
pub use typenum::consts;
2828

2929
use core::{
30-
array::{IntoIter, TryFromSliceError},
30+
array::TryFromSliceError,
3131
borrow::{Borrow, BorrowMut},
3232
cmp::Ordering,
3333
fmt::{self, Debug},
@@ -307,6 +307,21 @@ where
307307
}
308308
}
309309

310+
impl<T, U> IntoIterator for Array<T, U>
311+
where
312+
U: ArraySize,
313+
{
314+
type Item = T;
315+
type IntoIter = <U::ArrayType<T> as IntoIterator>::IntoIter;
316+
317+
/// Creates a consuming iterator, that is, one that moves each value out of
318+
/// the array (from start to end). The array cannot be used after calling
319+
/// this unless `T` implements `Copy`, so the whole array is copied.
320+
fn into_iter(self) -> Self::IntoIter {
321+
self.0.into_iter()
322+
}
323+
}
324+
310325
impl<T, U> PartialEq for Array<T, U>
311326
where
312327
T: PartialEq,
@@ -495,9 +510,9 @@ impl<T, const N: usize> ArrayExt<T> for [T; N] {
495510
///
496511
/// NOTE: do not implement this trait yourself. It is implemented for types in
497512
/// [`typenum::consts`].
498-
pub unsafe trait ArraySize: Unsigned {
513+
pub unsafe trait ArraySize: Unsigned + 'static {
499514
/// Array type which corresponds to this size.
500-
type ArrayType<T>: ArrayExt<T> + AsRef<[T]> + AsMut<[T]> + IntoArray<T>;
515+
type ArrayType<T>: ArrayExt<T> + AsRef<[T]> + AsMut<[T]> + IntoArray<T> + IntoIterator<Item = T>;
501516
}
502517

503518
/// Convert the given type into an [`Array`].
@@ -570,18 +585,6 @@ macro_rules! impl_array_size {
570585
}
571586
}
572587

573-
impl<T> IntoIterator for Array<T, typenum::$ty> {
574-
type Item = T;
575-
type IntoIter = IntoIter<T, $len>;
576-
577-
/// Creates a consuming iterator, that is, one that moves each value out of
578-
/// the array (from start to end). The array cannot be used after calling
579-
/// this unless `T` implements `Copy`, so the whole array is copied.
580-
fn into_iter(self) -> Self::IntoIter {
581-
self.0.into_iter()
582-
}
583-
}
584-
585588
impl<'a, T> IntoIterator for &'a Array<T, typenum::$ty> {
586589
type Item = &'a T;
587590
type IntoIter = Iter<'a, T>;

0 commit comments

Comments
 (0)