Add map key and value iterators

PiperOrigin-RevId: 604495084
pull/15692/head
Alyssa Haroldsen 10 months ago committed by Copybara-Service
parent f5a225597b
commit d445953603
  1. 36
      rust/map.rs
  2. 32
      rust/test/shared/accessors_map_test.rs

@ -215,10 +215,27 @@ where
self.len() == 0 self.len() == 0
} }
/// An alias for `<Self as IntoIterator>::into_iterator`. /// Returns an iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<K>, View<V>)`.
/// This is an alias for `<Self as IntoIterator>::into_iter`.
pub fn iter(self) -> MapIter<'msg, K, V> { pub fn iter(self) -> MapIter<'msg, K, V> {
self.into_iter() self.into_iter()
} }
/// Returns an iterator visiting all keys in arbitrary order.
///
/// The iterator element type is `View<K>`.
pub fn keys(self) -> impl Iterator<Item = View<'msg, K>> + 'msg {
self.into_iter().map(|(k, _)| k)
}
/// Returns an iterator visiting all values in arbitrary order.
///
/// The iterator element type is `View<V>`.
pub fn values(self) -> impl Iterator<Item = View<'msg, V>> + 'msg {
self.into_iter().map(|(_, v)| v)
}
} }
#[doc(hidden)] #[doc(hidden)]
@ -286,9 +303,26 @@ where
todo!("implement b/28530933"); todo!("implement b/28530933");
} }
/// Returns an iterator visiting all key-value pairs in arbitrary order.
///
/// The iterator element type is `(View<K>, View<V>)`.
pub fn iter(&self) -> MapIter<'_, K, V> { pub fn iter(&self) -> MapIter<'_, K, V> {
self.into_iter() self.into_iter()
} }
/// Returns an iterator visiting all keys in arbitrary order.
///
/// The iterator element type is `View<K>`.
pub fn keys(&self) -> impl Iterator<Item = View<'_, K>> + '_ {
self.as_view().keys()
}
/// Returns an iterator visiting all values in arbitrary order.
///
/// The iterator element type is `View<V>`.
pub fn values(&self) -> impl Iterator<Item = View<'_, V>> + '_ {
self.as_view().values()
}
} }
/// An iterator visiting all key-value pairs in arbitrary order. /// An iterator visiting all key-value pairs in arbitrary order.

@ -25,6 +25,14 @@ macro_rules! generate_map_primitives_tests {
msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(), msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(),
elements_are![] elements_are![]
); );
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
elements_are![]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
elements_are![]
);
let k = <$k_type>::default(); let k = <$k_type>::default();
let v = <$v_type>::default(); let v = <$v_type>::default();
assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k, v), eq(true)); assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k, v), eq(true));
@ -34,18 +42,34 @@ macro_rules! generate_map_primitives_tests {
msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(), msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(),
elements_are![eq((k, v))] elements_are![eq((k, v))]
); );
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
elements_are![eq(k)]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
elements_are![eq(v)]
);
let k: $k_type = $k_nonzero; let k2: $k_type = $k_nonzero;
let v: $v_type = $v_nonzero; let v2: $v_type = $v_nonzero;
assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k, v), eq(true)); assert_that!(msg.[< map_ $k_field _ $v_field _mut>]().insert(k2, v2), eq(true));
assert_that!(msg.[< map_ $k_field _ $v_field >]().len(), eq(2)); assert_that!(msg.[< map_ $k_field _ $v_field >]().len(), eq(2));
assert_that!( assert_that!(
msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(), msg.[< map_ $k_field _ $v_field >]().iter().collect::<Vec<_>>(),
unordered_elements_are![ unordered_elements_are![
eq((k, v)), eq((k, v)),
eq((<$k_type>::default(), <$v_type>::default())), eq((k2, v2)),
] ]
); );
assert_that!(
msg.[< map_ $k_field _ $v_field >]().keys().collect::<Vec<_>>(),
unordered_elements_are![eq(k), eq(k2)]
);
assert_that!(
msg.[< map_ $k_field _ $v_field >]().values().collect::<Vec<_>>(),
unordered_elements_are![eq(v), eq(v2)]
);
} }
)* } )* }
}; };

Loading…
Cancel
Save