Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ derive = ["implicit-clone-derive"]
implicit-clone-derive = { version = "0.1", optional = true, path = "./implicit-clone-derive" }
indexmap = { version = "2", optional = true }
serde = { version = "1", optional = true }
ouroboros = "0.18"

[dev-dependencies]
static_assertions = "1"
Expand Down
47 changes: 23 additions & 24 deletions src/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::fmt;

use ouroboros::self_referencing;

use super::Rc;
use crate::ImplicitClone;

Expand Down Expand Up @@ -110,47 +112,44 @@ 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> {
#[self_referencing]
struct IArrayIntoIterInternal<T: ImplicitClone + 'static> {
array: IArray<T>,
left: usize,
right: usize,
#[covariant]
#[borrows(array)]
iter: std::slice::Iter<'this, T>,
}

/// An iterator over the cloned elements of an `IArray`.
#[allow(missing_debug_implementations)]
pub struct IArrayIntoIter<T: ImplicitClone + 'static>(IArrayIntoIterInternal<T>);

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

fn into_iter(self) -> <Self as IntoIterator>::IntoIter {
IArrayIntoIter {
left: 0,
right: self.len(),
array: self,
}
IArrayIntoIter(
IArrayIntoIterInternalBuilder {
array: self,
iter_builder: |a| a.iter(),
}
.build(),
)
}
}

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())
self.0.with_iter_mut(|iter| iter.next().cloned())
}
}

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.0.with_iter_mut(|iter| iter.next_back().cloned())
}
}

Expand Down Expand Up @@ -333,7 +332,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 +368,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 +489,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
67 changes: 61 additions & 6 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use indexmap::map::Iter as MapIter;
use indexmap::map::Keys as MapKeys;
use indexmap::map::Values as MapValues;
use indexmap::IndexMap as Map;
use ouroboros::self_referencing;
use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
Expand Down Expand Up @@ -308,8 +309,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 +320,51 @@ impl<'a, K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone +
}
}

#[self_referencing]
struct IMapIntoIterInternal<
K: Eq + Hash + ImplicitClone + 'static,
V: PartialEq + ImplicitClone + 'static,
> {
map: IMap<K, V>,
#[covariant]
#[borrows(map)]
iter: IMapIter<'this, K, V>,
}

#[allow(missing_docs, missing_debug_implementations)]
pub struct IMapIntoIter<
K: Eq + Hash + ImplicitClone + 'static,
V: PartialEq + ImplicitClone + 'static,
>(IMapIntoIterInternal<K, V>);

impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static> IntoIterator
for IMap<K, V>
{
type Item = (K, V);
type IntoIter = IMapIntoIter<K, V>;

fn into_iter(self) -> Self::IntoIter {
IMapIntoIter(
IMapIntoIterInternalBuilder {
map: self,
iter_builder: |a| a.iter(),
}
.build(),
)
}
}

impl<K: Eq + Hash + ImplicitClone + 'static, V: PartialEq + ImplicitClone + 'static> Iterator
for IMapIntoIter<K, V>
{
type Item = (K, V);

fn next(&mut self) -> Option<Self::Item> {
self.0
.with_iter_mut(|iter| iter.next().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 +384,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 +414,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 +518,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));
}
}