diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c056526cc1..2ff03ec95cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -39,6 +39,8 @@ - Add Keviyah/Four Gates based optimized calculations module for the Hebrew calendar. (https://github.com/unicode-org/icu4x/pull/4504) - Expose `Hebrew` as a unit struct, add `Date::try_new_hebrew_date()`, `DateTime::try_new_hebrew_datetime()`. (https://github.com/unicode-org/icu4x/pulls/4532) - Deprecate `Hebrew::new_always_precomputing()`, `Date::try_new_hebrew_date_with_calendar()`, `DateTime::try_new_hebrew_datetime_with_calendar()`. The new implementation of the Hebrew calendar is faster and we do not need APIs for precomputation. (https://github.com/unicode-org/icu4x/pulls/4532) + - `litemap` + - Add `impl IntoIterator for LiteMap` by splitting `StoreIterableMut` trait (https://github.com/unicode-org/icu4x/pull/4359) - `yoke` - Remove `StableDeref` bound from `Yoke>` methods (https://github.com/unicode-org/icu4x/pull/4457) - Added `CartableOptionPointer` and function to convert from `Yoke>` (https://github.com/unicode-org/icu4x/pull/4449)\ diff --git a/components/locid/src/helpers.rs b/components/locid/src/helpers.rs index a29375a30cc..65d174c594b 100644 --- a/components/locid/src/helpers.rs +++ b/components/locid/src/helpers.rs @@ -316,19 +316,23 @@ impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for ShortSlice<(K, V)> { for<'r> fn(&'r mut (K, V)) -> (&'r K, &'r mut V), >; - type KeyValueIntoIter = ShortSliceIntoIter<(K, V)>; - fn lm_iter_mut( &'a mut self, ) -> >::KeyValueIterMut { self.iter_mut().map(|elt| (&elt.0, &mut elt.1)) } +} - fn lm_into_iter( - self, - ) -> >::KeyValueIntoIter { +impl StoreIntoIterator for ShortSlice<(K, V)> { + type KeyValueIntoIter = ShortSliceIntoIter<(K, V)>; + + fn lm_into_iter(self) -> Self::KeyValueIntoIter { self.into_iter() } + + // leave lm_extend_end as default + + // leave lm_extend_start as default } impl StoreFromIterator for ShortSlice<(K, V)> {} diff --git a/utils/litemap/src/map.rs b/utils/litemap/src/map.rs index 195a46d723a..e9b7bfb6f28 100644 --- a/utils/litemap/src/map.rs +++ b/utils/litemap/src/map.rs @@ -747,7 +747,7 @@ where impl<'a, K: 'a, V: 'a, S> LiteMap where K: Ord, - S: StoreIterableMut<'a, K, V> + StoreFromIterator, + S: StoreIntoIterator + StoreFromIterator, { /// Insert all elements from `other` into this `LiteMap`. /// @@ -898,6 +898,18 @@ where } } +impl IntoIterator for LiteMap +where + S: StoreIntoIterator, +{ + type Item = (K, V); + type IntoIter = S::KeyValueIntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.values.lm_into_iter() + } +} + impl LiteMap where S: StoreMut, @@ -1245,4 +1257,18 @@ mod test { } } } + + #[test] + fn into_iterator() { + let mut map = LiteMap::<_, _, Vec<(_, _)>>::new(); + map.insert(4, "four"); + map.insert(6, "six"); + let mut reference = vec![(6, "six"), (4, "four")]; + + for i in map { + let r = reference.pop().unwrap(); + assert_eq!(r, i); + } + assert!(reference.is_empty()); + } } diff --git a/utils/litemap/src/store/mod.rs b/utils/litemap/src/store/mod.rs index ca696a1afa1..78c4072f526 100644 --- a/utils/litemap/src/store/mod.rs +++ b/utils/litemap/src/store/mod.rs @@ -145,10 +145,13 @@ pub trait StoreIterable<'a, K: 'a + ?Sized, V: 'a + ?Sized>: Store { pub trait StoreIterableMut<'a, K: 'a, V: 'a>: StoreMut + StoreIterable<'a, K, V> { type KeyValueIterMut: Iterator + DoubleEndedIterator + 'a; - type KeyValueIntoIter: Iterator; /// Returns an iterator over key/value pairs, with a mutable value. fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut; +} + +pub trait StoreIntoIterator: StoreMut { + type KeyValueIntoIter: Iterator; /// Returns an iterator that moves every item from this store. fn lm_into_iter(self) -> Self::KeyValueIntoIter; diff --git a/utils/litemap/src/store/vec_impl.rs b/utils/litemap/src/store/vec_impl.rs index 2205e8e8ff1..f1366ccb8ce 100644 --- a/utils/litemap/src/store/vec_impl.rs +++ b/utils/litemap/src/store/vec_impl.rs @@ -150,12 +150,15 @@ impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for Vec<(K, V)> { impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for Vec<(K, V)> { type KeyValueIterMut = core::iter::Map, MapFMut>; - type KeyValueIntoIter = alloc::vec::IntoIter<(K, V)>; #[inline] fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut { self.as_mut_slice().iter_mut().map(map_f_mut) } +} + +impl StoreIntoIterator for Vec<(K, V)> { + type KeyValueIntoIter = alloc::vec::IntoIter<(K, V)>; #[inline] fn lm_into_iter(self) -> Self::KeyValueIntoIter { diff --git a/utils/litemap/src/testing.rs b/utils/litemap/src/testing.rs index 2fb8c522a47..f52646f54d5 100644 --- a/utils/litemap/src/testing.rs +++ b/utils/litemap/src/testing.rs @@ -48,12 +48,12 @@ where // Test code #[allow(clippy::expect_used)] -fn check_into_iter_equivalence<'a, K, V, S0, S1>(a: S0, b: S1) +fn check_into_iter_equivalence(a: S0, b: S1) where - K: Ord + Debug + PartialEq + 'a, - V: Debug + PartialEq + 'a, - S0: StoreIterableMut<'a, K, V>, - S1: StoreIterableMut<'a, K, V>, + K: Ord + Debug + PartialEq, + V: Debug + PartialEq, + S0: StoreIntoIterator, + S1: StoreIntoIterator, { let a_vec = a.lm_into_iter().collect::>(); let b_vec = b.lm_into_iter().collect::>(); @@ -175,6 +175,7 @@ pub fn check_store_full<'a, S>() where S: StoreConstEmpty + StoreIterableMut<'a, u32, u64> + + StoreIntoIterator + StoreFromIterator + Clone + Debug diff --git a/utils/litemap/tests/store.rs b/utils/litemap/tests/store.rs index bd28bee96ed..4e2b10bc7ae 100644 --- a/utils/litemap/tests/store.rs +++ b/utils/litemap/tests/store.rs @@ -108,12 +108,15 @@ impl<'a, K: 'a, V: 'a> StoreIterable<'a, K, V> for VecWithDefaults<(K, V)> { impl<'a, K: 'a, V: 'a> StoreIterableMut<'a, K, V> for VecWithDefaults<(K, V)> { type KeyValueIterMut = core::iter::Map, MapFMut>; - type KeyValueIntoIter = std::vec::IntoIter<(K, V)>; #[inline] fn lm_iter_mut(&'a mut self) -> Self::KeyValueIterMut { self.0.as_mut_slice().iter_mut().map(map_f_mut) } +} + +impl StoreIntoIterator for VecWithDefaults<(K, V)> { + type KeyValueIntoIter = std::vec::IntoIter<(K, V)>; #[inline] fn lm_into_iter(self) -> Self::KeyValueIntoIter {