Implement ExactSizeIterator and FusedIterator for repeated fields.
PiperOrigin-RevId: 591306340
diff --git a/rust/repeated.rs b/rust/repeated.rs
index 22d6fb9..2753fce 100644
--- a/rust/repeated.rs
+++ b/rust/repeated.rs
@@ -7,6 +7,7 @@
use std::fmt::{self, Debug};
use std::iter;
+use std::iter::FusedIterator;
/// Repeated scalar fields are implemented around the runtime-specific
/// `RepeatedField` struct. `RepeatedField` stores an opaque pointer to the
/// runtime-specific representation of a repeated scalar (`upb_Array*` on upb,
@@ -367,6 +368,14 @@
}
}
+impl<'msg, T: ?Sized + ProxiedInRepeated> ExactSizeIterator for RepeatedIter<'msg, T> {
+ fn len(&self) -> usize {
+ self.view.len()
+ }
+}
+
+impl<'msg, T: ?Sized + ProxiedInRepeated> FusedIterator for RepeatedIter<'msg, T> {}
+
impl<'msg, T> iter::IntoIterator for RepeatedView<'msg, T>
where
T: ProxiedInRepeated + ?Sized + 'msg,
diff --git a/rust/test/shared/accessors_repeated_test.rs b/rust/test/shared/accessors_repeated_test.rs
index d0b9a3b..1f44a52 100644
--- a/rust/test/shared/accessors_repeated_test.rs
+++ b/rust/test/shared/accessors_repeated_test.rs
@@ -21,6 +21,7 @@
let mut mutator = msg.[<repeated_ $field _mut >]();
mutator.push(1 as $t);
assert_that!(mutator.len(), eq(1));
+ assert_that!(mutator.iter().len(), eq(1));
assert_that!(mutator.get(0), some(eq(1 as $t)));
mutator.set(0, 2 as $t);
assert_that!(mutator.get(0), some(eq(2 as $t)));