Skip to content

Commit

Permalink
Merge pull request #474 from XOSplicer/linear-map-into-iter
Browse files Browse the repository at this point in the history
Implement IntoIterator for LinearMap
  • Loading branch information
reitermarkus authored May 23, 2024
2 parents 804fa58 + d4f7db3 commit c118d70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `Deque::make_contiguous`.
- Added `VecView`, the `!Sized` version of `Vec`.
- Added pool implementations for 64-bit architectures.
- Added `IntoIterator` implementation for `LinearMap`

### Changed

Expand Down
27 changes: 27 additions & 0 deletions src/linear_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ where
}
}

impl<K, V, const N: usize> IntoIterator for LinearMap<K, V, N>
where
K: Eq,
{
type Item = (K, V);
type IntoIter = IntoIter<K, V, N>;

fn into_iter(self) -> Self::IntoIter {
IntoIter {
inner: self.buffer.into_iter(),
}
}
}

impl<'a, K, V, const N: usize> IntoIterator for &'a LinearMap<K, V, N>
where
K: Eq,
Expand Down Expand Up @@ -557,4 +571,17 @@ mod test {

assert_eq!(Droppable::count(), 0);
}

#[test]
fn into_iter() {
let mut src: LinearMap<_, _, 4> = LinearMap::new();
src.insert("k1", "v1").unwrap();
src.insert("k2", "v2").unwrap();
src.insert("k3", "v3").unwrap();
src.insert("k4", "v4").unwrap();
let clone = src.clone();
for (k, v) in clone.into_iter() {
assert_eq!(v, src.remove(k).unwrap());
}
}
}

0 comments on commit c118d70

Please sign in to comment.