From 79595b1ae1a12fa2414d4b6c3c4bb2f7a0a9c094 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:24:07 +0200 Subject: Resolve imports by mutating Expr instead of cloning it --- dhall_syntax/src/core/map.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 'dhall_syntax/src/core/map.rs') diff --git a/dhall_syntax/src/core/map.rs b/dhall_syntax/src/core/map.rs index 6a0ebda..c4c6126 100644 --- a/dhall_syntax/src/core/map.rs +++ b/dhall_syntax/src/core/map.rs @@ -13,6 +13,8 @@ mod one_or_more { } pub type Iter<'a, T> = Either, iter::Once<&'a T>>; + pub type IterMut<'a, T> = + Either, iter::Once<&'a mut T>>; pub type IntoIter = Either, iter::Once>; impl OneOrMore { @@ -36,6 +38,13 @@ mod one_or_more { OneOrMore::One(x) => Either::Right(iter::once(x)), } } + + pub fn iter_mut(&mut self) -> IterMut<'_, T> { + match self { + OneOrMore::More(vec) => Either::Left(vec.iter_mut()), + OneOrMore::One(x) => Either::Right(iter::once(x)), + } + } } impl IntoIterator for OneOrMore { @@ -76,6 +85,19 @@ mod dup_tree_map { iter: IterInternal<'a, K, V>, size: usize, } + pub type IterMutInternalIntermediate<'a, K, V> = + iter::Zip, one_or_more::IterMut<'a, V>>; + pub type IterMutInternal<'a, K, V> = iter::FlatMap< + btree_map::IterMut<'a, K, OneOrMore>, + IterMutInternalIntermediate<'a, K, V>, + for<'b> fn( + (&'b K, &'b mut OneOrMore), + ) -> IterMutInternalIntermediate<'b, K, V>, + >; + pub struct IterMut<'a, K, V> { + iter: IterMutInternal<'a, K, V>, + size: usize, + } pub type IntoIterInternalIntermediate = iter::Zip, one_or_more::IntoIter>; pub type IntoIterInternal = iter::FlatMap< @@ -134,6 +156,21 @@ mod dup_tree_map { size: self.size, } } + + pub fn iter_mut(&mut self) -> IterMut<'_, K, V> + where + K: Ord, + { + fn foo<'a, K, V>( + (k, oom): (&'a K, &'a mut OneOrMore), + ) -> IterMutInternalIntermediate<'a, K, V> { + iter::repeat(k).zip(oom.iter_mut()) + } + IterMut { + iter: self.map.iter_mut().flat_map(foo), + size: self.size, + } + } } impl Default for DupTreeMap @@ -180,6 +217,18 @@ mod dup_tree_map { } } + impl<'a, K, V> IntoIterator for &'a mut DupTreeMap + where + K: Ord, + { + type Item = (&'a K, &'a mut V); + type IntoIter = IterMut<'a, K, V>; + + fn into_iter(self) -> Self::IntoIter { + self.iter_mut() + } + } + impl iter::FromIterator<(K, V)> for DupTreeMap where K: Ord, @@ -212,6 +261,22 @@ mod dup_tree_map { } } + impl<'a, K, V> Iterator for IterMut<'a, K, V> { + type Item = (&'a K, &'a mut V); + + fn next(&mut self) -> Option { + let next = self.iter.next(); + if next.is_some() { + self.size -= 1; + } + next + } + + fn size_hint(&self) -> (usize, Option) { + (self.size, Some(self.size)) + } + } + impl Iterator for IntoIter where K: Clone, -- cgit v1.2.3