From 5ba4cfce573f3704d1bdbcd7fe8c979a96475f1c Mon Sep 17 00:00:00 2001 From: anyingiit <49945850+anyingiit@users.noreply.github.com> Date: Fri, 2 Oct 2026 06:03:31 +0000 Subject: [PATCH] Implement IntoIterator for &mut LinearMap Allows iterating mutably over a LinearMap (or LinearMapView) with `for (k, v) in &mut map`, reusing the existing IterMut iterator. Closes #492 --- CHANGELOG.md | 1 + src/linear_map.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 548396b03c..a1ca906be6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Fixed unsoundness in `IndexMap:insert`. - Limited max size of `IndexMap` to u16::MAX + 1. The implementation for sizes higher than u16::MAX were unsound anyway. +- Implemented `IntoIterator` for `&mut LinearMap` and `&mut LinearMapView`. ## [v0.9.3] 2025-04-15 diff --git a/src/linear_map.rs b/src/linear_map.rs index c62fcc8b62..9ee360c965 100644 --- a/src/linear_map.rs +++ b/src/linear_map.rs @@ -654,6 +654,18 @@ where } } +impl<'a, K, V, S: LinearMapStorage + ?Sized> IntoIterator for &'a mut LinearMapInner +where + K: Eq, +{ + type Item = (&'a K, &'a mut V); + type IntoIter = IterMut<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } +} + /// An iterator over the items of a [`LinearMap`] /// /// This struct is created by calling the [`iter`](LinearMap::iter) method on [`LinearMap`]. @@ -1159,4 +1171,24 @@ mod test { }; assert_eq!(MAP_SLOTS - 1, src.len()); } + + #[test] + fn into_iter_mut() { + let mut map = LinearMap::<_, _, 8>::new(); + map.insert(1, 10).unwrap(); + map.insert(2, 20).unwrap(); + + for (_, v) in &mut map { + *v += 1; + } + assert_eq!(map.get(&1), Some(&11)); + assert_eq!(map.get(&2), Some(&21)); + + let view: &mut LinearMapView<_, _> = &mut map; + for (_, v) in view { + *v *= 2; + } + assert_eq!(map.get(&1), Some(&22)); + assert_eq!(map.get(&2), Some(&42)); + } }