From 7f6a0ba347a49e694d11212678401523af6cc1d2 Mon Sep 17 00:00:00 2001 From: Protobuf Team Bot Date: Fri, 29 Mar 2024 10:26:49 -0700 Subject: [PATCH] Implement Extend for Repeated PiperOrigin-RevId: 620277895 --- rust/repeated.rs | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/rust/repeated.rs b/rust/repeated.rs index edcd7c826e80..03376fbcc531 100644 --- a/rust/repeated.rs +++ b/rust/repeated.rs @@ -446,6 +446,18 @@ where } } +impl<'msg, 'view, T, ViewT> Extend for RepeatedMut<'msg, T> +where + T: ProxiedInRepeated + ?Sized + 'view, + ViewT: Into>, +{ + fn extend>(&mut self, iter: I) { + for item in iter { + self.push(item.into()); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -492,4 +504,20 @@ mod tests { bool => [false, true, true, false], ); } + + #[test] + fn test_repeated_extend() { + let mut r = Repeated::::new(); + r.as_mut().extend([0, 1]); + assert_that!(r.as_mut().iter().collect::>(), elements_are![eq(0), eq(1)]); + let mut x = Repeated::::new(); + x.as_mut().extend([2, 3]); + + r.as_mut().extend(&x.as_mut()); + + assert_that!( + r.as_mut().iter().collect::>(), + elements_are![eq(0), eq(1), eq(2), eq(3)] + ); + } }