Skip to content

Commit b9f5fb9

Browse files
authored
Impl AsRef<Self> for Array<T, U> (#43)
Notably this makes it possible to bound on: Arr: AssocArraySize + AsRef<ArrayN<T, N>> ...and accept either `[T; N]` or `ArrayN<T, N>` interchangably. `AsRef` is more convenient than `Borrow` in this context because it's in the prelude.
1 parent 26dfbe5 commit b9f5fb9

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/lib.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,6 @@ impl<T, U> Array<T, U>
165165
where
166166
U: ArraySize,
167167
{
168-
/// Returns an iterator over the array.
169-
#[inline]
170-
pub fn iter(&self) -> Iter<'_, T> {
171-
self.as_ref().iter()
172-
}
173-
174-
/// Returns an iterator that allows modifying each value.
175-
#[inline]
176-
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
177-
self.as_mut().iter_mut()
178-
}
179-
180168
/// Returns a slice containing the entire array. Equivalent to `&s[..]`.
181169
#[inline]
182170
pub fn as_slice(&self) -> &[T] {
@@ -228,6 +216,18 @@ where
228216
slice.try_into().expect("slice length mismatch")
229217
}
230218

219+
/// Returns an iterator over the array.
220+
#[inline]
221+
pub fn iter(&self) -> Iter<'_, T> {
222+
self.as_slice().iter()
223+
}
224+
225+
/// Returns an iterator that allows modifying each value.
226+
#[inline]
227+
pub fn iter_mut(&mut self) -> IterMut<'_, T> {
228+
self.as_mut().iter_mut()
229+
}
230+
231231
/// Concatenates `self` with `other`.
232232
#[inline]
233233
pub fn concat<N>(self, other: Array<T, N>) -> Array<T, Sum<U, N>>
@@ -411,6 +411,16 @@ where
411411
}
412412
}
413413

414+
impl<T, U> AsRef<Array<T, U>> for Array<T, U>
415+
where
416+
U: ArraySize,
417+
{
418+
#[inline]
419+
fn as_ref(&self) -> &Self {
420+
self
421+
}
422+
}
423+
414424
impl<T, U> AsRef<[T]> for Array<T, U>
415425
where
416426
U: ArraySize,

0 commit comments

Comments
 (0)