diff --git a/hybrid-array/src/lib.rs b/hybrid-array/src/lib.rs index 417b412f..66602ee7 100644 --- a/hybrid-array/src/lib.rs +++ b/hybrid-array/src/lib.rs @@ -61,7 +61,7 @@ where where F: FnMut(usize) -> T, { - Self(ArrayExt::from_fn(cb)) + Self(FromFn::from_fn(cb)) } /// Create array from a slice. @@ -276,7 +276,7 @@ where U: ArraySize, { fn clone(&self) -> Self { - Self(U::ArrayType::::from_fn(|n| self.0.as_ref()[n].clone())) + Self::from_fn(|n| self.0.as_ref()[n].clone()) } } @@ -304,7 +304,7 @@ where U: ArraySize, { fn default() -> Self { - Self(ArrayExt::from_fn(|_| Default::default())) + Self::from_fn(|_| Default::default()) } } @@ -603,29 +603,6 @@ impl SliceOps for [T] {} impl SliceOps for [T; N] {} impl SliceOps for Array {} -/// Extension trait with helper functions for core arrays. -pub trait ArrayExt: Sized { - /// Create array using the given callback function for each element. - fn from_fn(cb: F) -> Self - where - F: FnMut(usize) -> T; -} - -impl ArrayExt for [T; N] { - fn from_fn(mut cb: F) -> Self - where - F: FnMut(usize) -> T, - { - let mut idx = 0; - - [(); N].map(|_| { - let res = cb(idx); - idx = idx.saturating_add(1); // TODO(tarcieri): better overflow handling? - res - }) - } -} - /// Trait which associates a [`usize`] size and `ArrayType` with a /// `typenum`-provided [`Unsigned`] integer. /// @@ -638,7 +615,8 @@ 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 + type ArrayType: AssociatedArraySize + + FromFn + From> + Into> + IntoIterator @@ -651,6 +629,35 @@ pub trait AssociatedArraySize: Sized { type Size: ArraySize; } +/// Construct an array type from the given function. +pub trait FromFn: Sized { + /// Create array using the given callback function for each element. + fn from_fn(cb: F) -> Self + where + F: FnMut(usize) -> T; +} + +impl FromFn for Array +where + U: ArraySize, +{ + fn from_fn(cb: F) -> Self + where + F: FnMut(usize) -> T, + { + Array::from_fn(cb) + } +} + +impl FromFn for [T; N] { + fn from_fn(cb: F) -> Self + where + F: FnMut(usize) -> T, + { + core::array::from_fn(cb) + } +} + /// Splits the shared slice into a slice of `N`-element arrays, starting at the beginning /// of the slice, and a remainder slice with length strictly less than `N`. ///