Implement IntoIterator for &Repeated{View,Mut}

googletest matchers use this. Unfortunately, we still can't
use `elements_are!` with them:
https://github.com/google/googletest-rust/issues/351

PiperOrigin-RevId: 599281808
pull/15466/head
Alyssa Haroldsen 11 months ago committed by Copybara-Service
parent 19851968bb
commit e16dd47999
  1. 24
      rust/repeated.rs
  2. 3
      rust/test/shared/accessors_repeated_test.rs

@ -418,6 +418,30 @@ where
}
}
impl<'msg, T> iter::IntoIterator for &'_ RepeatedView<'msg, T>
where
T: ProxiedInRepeated + ?Sized + 'msg,
{
type Item = View<'msg, T>;
type IntoIter = RepeatedIter<'msg, T>;
fn into_iter(self) -> Self::IntoIter {
RepeatedIter { view: *self, current_index: 0 }
}
}
impl<'borrow, T> iter::IntoIterator for &'borrow RepeatedMut<'_, T>
where
T: ProxiedInRepeated + ?Sized + 'borrow,
{
type Item = View<'borrow, T>;
type IntoIter = RepeatedIter<'borrow, T>;
fn into_iter(self) -> Self::IntoIter {
RepeatedIter { view: self.as_view(), current_index: 0 }
}
}
#[cfg(test)]
mod tests {
use super::*;

@ -15,6 +15,7 @@ macro_rules! generate_repeated_numeric_test {
#[test]
fn [< test_repeated_ $field _accessors >]() {
let mut msg = TestAllTypes::new();
assert_that!(msg.[< repeated_ $field >](), empty());
assert_that!(msg.[< repeated_ $field >]().len(), eq(0));
assert_that!(msg.[<repeated_ $field >]().get(0), none());
@ -82,6 +83,7 @@ generate_repeated_numeric_test!(
#[test]
fn test_repeated_bool_accessors() {
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_bool(), empty());
assert_that!(msg.repeated_bool().len(), eq(0));
assert_that!(msg.repeated_bool().get(0), none());
@ -116,6 +118,7 @@ fn test_repeated_enum_accessors() {
use TestAllTypes_::NestedEnum;
let mut msg = TestAllTypes::new();
assert_that!(msg.repeated_nested_enum(), empty());
assert_that!(msg.repeated_nested_enum().len(), eq(0));
assert_that!(msg.repeated_nested_enum().get(0), none());

Loading…
Cancel
Save