diff --git a/rust/repeated.rs b/rust/repeated.rs index 22d6fb98d9..2753fcee2d 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 @@ where } } +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 d0b9a3bc42..1f44a52ac4 100644 --- a/rust/test/shared/accessors_repeated_test.rs +++ b/rust/test/shared/accessors_repeated_test.rs @@ -21,6 +21,7 @@ macro_rules! generate_repeated_numeric_test { let mut mutator = msg.[](); 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)));