Skip to content

Commit

Permalink
Implement Extend for Repeated
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 620277895
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Mar 29, 2024
1 parent 11ef0ff commit 7f6a0ba
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions rust/repeated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,18 @@ where
}
}

impl<'msg, 'view, T, ViewT> Extend<ViewT> for RepeatedMut<'msg, T>
where
T: ProxiedInRepeated + ?Sized + 'view,
ViewT: Into<View<'view, T>>,
{
fn extend<I: IntoIterator<Item = ViewT>>(&mut self, iter: I) {
for item in iter {
self.push(item.into());
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -492,4 +504,20 @@ mod tests {
bool => [false, true, true, false],
);
}

#[test]
fn test_repeated_extend() {
let mut r = Repeated::<i32>::new();
r.as_mut().extend([0, 1]);
assert_that!(r.as_mut().iter().collect::<Vec<_>>(), elements_are![eq(0), eq(1)]);
let mut x = Repeated::<i32>::new();
x.as_mut().extend([2, 3]);

r.as_mut().extend(&x.as_mut());

assert_that!(
r.as_mut().iter().collect::<Vec<_>>(),
elements_are![eq(0), eq(1), eq(2), eq(3)]
);
}
}

0 comments on commit 7f6a0ba

Please sign in to comment.