Skip to content

Commit

Permalink
Implement IntoIterator for &Repeated{View,Mut}
Browse files Browse the repository at this point in the history
googletest matchers use this. Unfortunately, we still can't
use `elements_are!` with them:
google/googletest-rust#351

PiperOrigin-RevId: 599281808
  • Loading branch information
kupiakos authored and copybara-github committed Jan 17, 2024
1 parent 1985196 commit e16dd47
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
24 changes: 24 additions & 0 deletions rust/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down
3 changes: 3 additions & 0 deletions rust/test/shared/accessors_repeated_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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());

Expand Down

0 comments on commit e16dd47

Please sign in to comment.