Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 6 additions & 41 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,47 +110,12 @@ impl<T: ImplicitClone + 'static, const N: usize> From<[T; N]> for IArray<T> {
}
}

/// An iterator over the cloned elements of an `IArray`.
#[derive(Debug)]
pub struct IArrayIntoIter<T: ImplicitClone + 'static> {
array: IArray<T>,
left: usize,
right: usize,
}

impl<T: ImplicitClone + 'static> IntoIterator for IArray<T> {
impl<'a, T: ImplicitClone + 'static> IntoIterator for &'a IArray<T> {
type Item = T;
type IntoIter = IArrayIntoIter<T>;
type IntoIter = std::iter::Cloned<std::slice::Iter<'a, T>>;

fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
IArrayIntoIter {
left: 0,
right: self.len(),
array: self,
}
}
}

impl<T: ImplicitClone + 'static> Iterator for IArrayIntoIter<T> {
type Item = T;

fn next(&mut self) -> Option<Self::Item> {
if self.left >= self.right {
return None;
}
let item = &self.array[self.left];
self.left += 1;
Some(item.clone())
}
}

impl<T: ImplicitClone + 'static> DoubleEndedIterator for IArrayIntoIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
if self.left >= self.right {
return None;
}
self.right -= 1;
Some(self.array[self.right].clone())
self.iter().cloned()
}
}

Expand Down Expand Up @@ -333,7 +298,7 @@ impl<T: ImplicitClone + 'static> IArray<T> {
}
}

impl<'a, T, U, const N: usize> PartialEq<&'a [U; N]> for IArray<T>
impl<T, U, const N: usize> PartialEq<&[U; N]> for IArray<T>
where
T: PartialEq<U> + ImplicitClone,
{
Expand Down Expand Up @@ -369,7 +334,7 @@ where
}
}

impl<'a, T, U> PartialEq<&'a [U]> for IArray<T>
impl<T, U> PartialEq<&[U]> for IArray<T>
where
T: PartialEq<U> + ImplicitClone,
{
Expand Down Expand Up @@ -490,7 +455,7 @@ mod test_array {
}

#[test]
fn into_iter() {
fn iterators() {
let array = IArray::Static(&[1, 2, 3]);
assert_eq!(array.iter().next().unwrap(), &1);
assert_eq!(array.into_iter().next().unwrap(), 1);
Expand Down
32 changes: 26 additions & 6 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone +
}
}

impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapIter<'a, K, V>
impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapIter<'_, K, V>
{
fn next_back(&mut self) -> Option<Self::Item> {
match self {
Expand All @@ -319,6 +319,17 @@ impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone +
}
}

impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
IntoIterator for &'a IMap<K, V>
{
type Item = (K, V);
type IntoIter = std::iter::Map<IMapIter<'a, K, V>, fn((&'a K, &'a V)) -> (K, V)>;

fn into_iter(self) -> Self::IntoIter {
self.iter().map(|(k, v)| (k.clone(), v.clone()))
}
}

#[allow(missing_docs, missing_debug_implementations)]
pub enum IMapKeys<'a, K, V> {
Slice(std::slice::Iter<'a, (K, V)>),
Expand All @@ -338,8 +349,8 @@ impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone +
}
}

impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapKeys<'a, K, V>
impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapKeys<'_, K, V>
{
fn next_back(&mut self) -> Option<Self::Item> {
match self {
Expand Down Expand Up @@ -368,8 +379,8 @@ impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone +
}
}

impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapValues<'a, K, V>
impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static>
DoubleEndedIterator for IMapValues<'_, K, V>
{
fn next_back(&mut self) -> Option<Self::Item> {
match self {
Expand Down Expand Up @@ -472,4 +483,13 @@ mod test_map {
let x: IMap<u32, u32> = IMap::Static(&[]);
let _out = IMap::from(&x);
}

#[test]
fn iterators() {
let map = IMap::Static(&[(1, 10), (2, 20), (3, 30)]);
assert_eq!(map.iter().next().unwrap(), (&1, &10));
assert_eq!(map.keys().next().unwrap(), &1);
assert_eq!(map.values().next().unwrap(), &10);
assert_eq!(map.into_iter().next().unwrap(), (1, 10));
}
}