From c4138b68c48d3a60af68b99aebed8dbf86d73956 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Wed, 1 Nov 2023 14:17:42 +0100 Subject: [PATCH] Remove opaque type on function iter() of IArray (#40) --- src/array.rs | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/src/array.rs b/src/array.rs index bc27342..cf20ca7 100644 --- a/src/array.rs +++ b/src/array.rs @@ -83,6 +83,28 @@ impl From<[T; 1]> for IArray { } } +#[derive(Debug)] +pub struct Iter { + array: IArray, + index: usize, +} + +impl Iter { + fn new(array: IArray) -> Self { + Self { array, index: 0 } + } +} + +impl Iterator for Iter { + type Item = T; + + fn next(&mut self) -> Option { + let item = self.array.get(self.index); + self.index += 1; + item + } +} + impl IArray { /// Returns an iterator over the slice. /// @@ -99,12 +121,8 @@ impl IArray { /// assert_eq!(iterator.next(), None); /// ``` #[inline] - pub fn iter(&self) -> impl Iterator + '_ { - match self { - Self::Static(a) => a.iter().cloned(), - Self::Rc(a) => a.iter().cloned(), - Self::Single(a) => a.iter().cloned(), - } + pub fn iter(&self) -> Iter { + Iter::new(self.clone()) } /// Returns the number of elements in the vector, also referred to