Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
| 2 | // Copyright 2023 Google LLC. All rights reserved. |
| 3 | // |
| 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file or at |
| 6 | // https://developers.google.com/open-source/licenses/bsd |
| 7 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 8 | use std::fmt::{self, Debug}; |
| 9 | use std::iter; |
Protobuf Team Bot | 14dd8e9 | 2023-12-15 11:05:56 -0800 | [diff] [blame] | 10 | use std::iter::FusedIterator; |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 11 | /// Repeated scalar fields are implemented around the runtime-specific |
| 12 | /// `RepeatedField` struct. `RepeatedField` stores an opaque pointer to the |
| 13 | /// runtime-specific representation of a repeated scalar (`upb_Array*` on upb, |
| 14 | /// and `RepeatedField<T>*` on cpp). |
| 15 | use std::marker::PhantomData; |
| 16 | |
| 17 | use crate::{ |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 18 | IntoProxied, Mut, MutProxied, MutProxy, Proxied, View, ViewProxy, |
Protobuf Team Bot | 2678e10 | 2024-04-12 13:50:28 -0700 | [diff] [blame] | 19 | __internal::Private, |
| 20 | __runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField}, |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 21 | }; |
| 22 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 23 | /// Views the elements in a `repeated` field of `T`. |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 24 | #[repr(transparent)] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 25 | pub struct RepeatedView<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 26 | // This does not need to carry an arena in upb, so it can be just the raw repeated field |
| 27 | raw: RawRepeatedField, |
| 28 | _phantom: PhantomData<&'msg T>, |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 31 | impl<'msg, T> Copy for RepeatedView<'msg, T> {} |
| 32 | impl<'msg, T> Clone for RepeatedView<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 33 | fn clone(&self) -> Self { |
| 34 | *self |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 35 | } |
| 36 | } |
| 37 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 38 | unsafe impl<'msg, T> Sync for RepeatedView<'msg, T> {} |
| 39 | unsafe impl<'msg, T> Send for RepeatedView<'msg, T> {} |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 40 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 41 | impl<'msg, T> Debug for RepeatedView<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 42 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 | f.debug_struct("RepeatedView").field("raw", &self.raw).finish() |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 47 | /// Mutates the elements in a `repeated` field of `T`. |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 48 | pub struct RepeatedMut<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 49 | pub(crate) inner: InnerRepeatedMut<'msg>, |
| 50 | _phantom: PhantomData<&'msg mut T>, |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 53 | unsafe impl<'msg, T> Sync for RepeatedMut<'msg, T> {} |
Kevin King | 65cdac4 | 2023-10-23 14:50:42 -0700 | [diff] [blame] | 54 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 55 | impl<'msg, T> Debug for RepeatedMut<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 56 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 57 | f.debug_struct("RepeatedMut").field("raw", &self.inner.raw).finish() |
Protobuf Team Bot | e1bb7d6 | 2023-10-17 14:15:37 -0700 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 61 | #[doc(hidden)] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 62 | impl<'msg, T> RepeatedView<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 63 | #[doc(hidden)] |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 64 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 65 | pub fn as_raw(&self, _private: Private) -> RawRepeatedField { |
| 66 | self.raw |
| 67 | } |
| 68 | |
| 69 | /// # Safety |
| 70 | /// - `inner` must be valid to read from for `'msg` |
| 71 | #[doc(hidden)] |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 72 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 73 | pub unsafe fn from_raw(_private: Private, raw: RawRepeatedField) -> Self { |
| 74 | Self { raw, _phantom: PhantomData } |
| 75 | } |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 76 | } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 77 | |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 78 | impl<'msg, T> RepeatedView<'msg, T> |
| 79 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 80 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 81 | { |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 82 | /// Gets the length of the repeated field. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 83 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 84 | pub fn len(&self) -> usize { |
| 85 | T::repeated_len(*self) |
| 86 | } |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 87 | |
| 88 | /// Returns true if the repeated field has no values. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 89 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 90 | pub fn is_empty(&self) -> bool { |
| 91 | self.len() == 0 |
| 92 | } |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 93 | |
| 94 | /// Gets the value at `index`. |
| 95 | /// |
| 96 | /// Returns `None` if `index > len`. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 97 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 98 | pub fn get(self, index: usize) -> Option<View<'msg, T>> { |
| 99 | if index >= self.len() { |
| 100 | return None; |
| 101 | } |
| 102 | // SAFETY: `index` has been checked to be in-bounds |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 103 | Some(unsafe { self.get_unchecked(index) }) |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 104 | } |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 105 | |
| 106 | /// Gets the value at `index` without bounds-checking. |
| 107 | /// |
| 108 | /// # Safety |
| 109 | /// Undefined behavior if `index >= len` |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 110 | #[inline] |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 111 | pub unsafe fn get_unchecked(self, index: usize) -> View<'msg, T> { |
| 112 | // SAFETY: in-bounds as promised |
| 113 | unsafe { T::repeated_get_unchecked(self, index) } |
| 114 | } |
| 115 | |
| 116 | /// Iterates over the values in the repeated field. |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 117 | pub fn iter(self) -> RepeatedIter<'msg, T> { |
| 118 | self.into_iter() |
| 119 | } |
| 120 | } |
| 121 | |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 122 | #[doc(hidden)] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 123 | impl<'msg, T> RepeatedMut<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 124 | /// # Safety |
| 125 | /// - `inner` must be valid to read and write from for `'msg` |
| 126 | /// - There must be no aliasing references or mutations on the same |
| 127 | /// underlying object. |
| 128 | #[doc(hidden)] |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 129 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 130 | pub unsafe fn from_inner(_private: Private, inner: InnerRepeatedMut<'msg>) -> Self { |
| 131 | Self { inner, _phantom: PhantomData } |
| 132 | } |
| 133 | |
| 134 | #[doc(hidden)] |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 135 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 136 | pub fn as_raw(&mut self, _private: Private) -> RawRepeatedField { |
| 137 | self.inner.raw |
| 138 | } |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 139 | } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 140 | |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 141 | impl<'msg, T> RepeatedMut<'msg, T> |
| 142 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 143 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 3657e05 | 2024-02-05 12:51:41 -0800 | [diff] [blame] | 144 | { |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 145 | /// Gets the length of the repeated field. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 146 | #[inline] |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 147 | pub fn len(&self) -> usize { |
| 148 | self.as_view().len() |
| 149 | } |
| 150 | |
| 151 | /// Returns true if the repeated field has no values. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 152 | #[inline] |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 153 | pub fn is_empty(&self) -> bool { |
| 154 | self.len() == 0 |
| 155 | } |
| 156 | |
| 157 | /// Gets the value at `index`. |
| 158 | /// |
| 159 | /// Returns `None` if `index > len`. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 160 | #[inline] |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 161 | pub fn get(&self, index: usize) -> Option<View<T>> { |
| 162 | self.as_view().get(index) |
| 163 | } |
| 164 | |
| 165 | /// Gets the value at `index` without bounds-checking. |
| 166 | /// |
| 167 | /// # Safety |
| 168 | /// Undefined behavior if `index >= len` |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 169 | #[inline] |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 170 | pub unsafe fn get_unchecked(&self, index: usize) -> View<T> { |
| 171 | // SAFETY: in-bounds as promised |
| 172 | unsafe { self.as_view().get_unchecked(index) } |
| 173 | } |
| 174 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 175 | /// Appends `val` to the end of the repeated field. |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 176 | #[inline] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 177 | pub fn push(&mut self, val: impl IntoProxied<T>) { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 178 | T::repeated_push(self.as_mut(), val); |
| 179 | } |
| 180 | |
| 181 | /// Sets the value at `index` to the value `val`. |
| 182 | /// |
| 183 | /// # Panics |
| 184 | /// Panics if `index >= len` |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 185 | #[inline] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 186 | pub fn set(&mut self, index: usize, val: impl IntoProxied<T>) { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 187 | let len = self.len(); |
| 188 | if index >= len { |
| 189 | panic!("index {index} >= repeated len {len}"); |
| 190 | } |
Alyssa Haroldsen | fcf0d01 | 2023-12-18 10:13:01 -0800 | [diff] [blame] | 191 | unsafe { self.set_unchecked(index, val) } |
| 192 | } |
| 193 | |
| 194 | /// Sets the value at `index` to the value `val`. |
| 195 | /// |
| 196 | /// # Safety |
| 197 | /// Undefined behavior if `index >= len` |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 198 | #[inline] |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 199 | pub unsafe fn set_unchecked(&mut self, index: usize, val: impl IntoProxied<T>) { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 200 | unsafe { T::repeated_set_unchecked(self.as_mut(), index, val) } |
| 201 | } |
| 202 | |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 203 | /// Iterates over the values in the repeated field. |
| 204 | pub fn iter(&self) -> RepeatedIter<T> { |
| 205 | self.as_view().into_iter() |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /// Copies from the `src` repeated field into this one. |
| 209 | /// |
| 210 | /// Also provided by [`MutProxy::set`]. |
| 211 | pub fn copy_from(&mut self, src: RepeatedView<'_, T>) { |
| 212 | T::repeated_copy_from(src, self.as_mut()) |
| 213 | } |
Alyssa Haroldsen | f51182b | 2023-12-14 14:07:40 -0800 | [diff] [blame] | 214 | |
| 215 | /// Clears the repeated field. |
| 216 | pub fn clear(&mut self) { |
| 217 | T::repeated_clear(self.as_mut()) |
| 218 | } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 221 | impl<T> Repeated<T> |
| 222 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 223 | T: ProxiedInRepeated, |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 224 | { |
| 225 | pub fn as_view(&self) -> View<Repeated<T>> { |
| 226 | RepeatedView { raw: self.inner.raw(), _phantom: PhantomData } |
| 227 | } |
| 228 | |
| 229 | #[doc(hidden)] |
| 230 | pub fn inner(&self, _private: Private) -> &InnerRepeated { |
| 231 | &self.inner |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | impl<T> IntoProxied<Repeated<T>> for Repeated<T> |
| 236 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 237 | T: ProxiedInRepeated, |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 238 | { |
Marcel Hlopko | fe69639 | 2024-06-12 05:26:09 -0700 | [diff] [blame] | 239 | fn into_proxied(self, _private: Private) -> Repeated<T> { |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 240 | self |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedView<'msg, T> |
| 245 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 246 | T: 'msg + ProxiedInRepeated, |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 247 | { |
Marcel Hlopko | fe69639 | 2024-06-12 05:26:09 -0700 | [diff] [blame] | 248 | fn into_proxied(self, _private: Private) -> Repeated<T> { |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 249 | let mut repeated: Repeated<T> = Repeated::new(); |
| 250 | T::repeated_copy_from(self, repeated.as_mut()); |
| 251 | repeated |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedMut<'msg, T> |
| 256 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 257 | T: 'msg + ProxiedInRepeated, |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 258 | { |
Marcel Hlopko | fe69639 | 2024-06-12 05:26:09 -0700 | [diff] [blame] | 259 | fn into_proxied(self, _private: Private) -> Repeated<T> { |
| 260 | IntoProxied::into_proxied(self.as_view(), _private) |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 264 | /// Types that can appear in a `Repeated<T>`. |
| 265 | /// |
| 266 | /// This trait is implemented by generated code to communicate how the proxied |
| 267 | /// type can be manipulated for a repeated field. |
| 268 | /// |
| 269 | /// Scalars and messages implement `ProxiedInRepeated`. |
| 270 | /// |
| 271 | /// # Safety |
| 272 | /// - It must be sound to call `*_unchecked*(x)` with an `index` less than |
| 273 | /// `repeated_len(x)`. |
| 274 | pub unsafe trait ProxiedInRepeated: Proxied { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 275 | /// Constructs a new owned `Repeated` field. |
| 276 | #[doc(hidden)] |
| 277 | fn repeated_new(_private: Private) -> Repeated<Self> { |
| 278 | unimplemented!("not required") |
| 279 | } |
| 280 | |
Alyssa Haroldsen | f51182b | 2023-12-14 14:07:40 -0800 | [diff] [blame] | 281 | /// Frees the repeated field in-place, for use in `Drop`. |
| 282 | /// |
| 283 | /// # Safety |
| 284 | /// - After `repeated_free`, no other methods on the input are safe to call. |
| 285 | #[doc(hidden)] |
| 286 | unsafe fn repeated_free(_private: Private, _repeated: &mut Repeated<Self>) { |
| 287 | unimplemented!("not required") |
| 288 | } |
| 289 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 290 | /// Gets the length of the repeated field. |
| 291 | fn repeated_len(repeated: View<Repeated<Self>>) -> usize; |
| 292 | |
| 293 | /// Appends a new element to the end of the repeated field. |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 294 | fn repeated_push(repeated: Mut<Repeated<Self>>, val: impl IntoProxied<Self>); |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 295 | |
Alyssa Haroldsen | f51182b | 2023-12-14 14:07:40 -0800 | [diff] [blame] | 296 | /// Clears the repeated field of elements. |
| 297 | fn repeated_clear(repeated: Mut<Repeated<Self>>); |
| 298 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 299 | /// # Safety |
| 300 | /// `index` must be less than `Self::repeated_len(repeated)` |
| 301 | unsafe fn repeated_get_unchecked(repeated: View<Repeated<Self>>, index: usize) -> View<Self>; |
| 302 | |
| 303 | /// # Safety |
| 304 | /// `index` must be less than `Self::repeated_len(repeated)` |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 305 | unsafe fn repeated_set_unchecked( |
| 306 | repeated: Mut<Repeated<Self>>, |
| 307 | index: usize, |
| 308 | val: impl IntoProxied<Self>, |
| 309 | ); |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 310 | |
| 311 | /// Copies the values in the `src` repeated field into `dest`. |
| 312 | fn repeated_copy_from(src: View<Repeated<Self>>, dest: Mut<Repeated<Self>>); |
Derek Benson | f2d8c2b | 2024-05-13 05:48:10 -0700 | [diff] [blame] | 313 | |
| 314 | /// Ensures that the repeated field has enough space allocated to insert at |
| 315 | /// least `additional` values without an allocation. |
| 316 | fn repeated_reserve(repeated: Mut<Repeated<Self>>, additional: usize); |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /// An iterator over the values inside of a [`View<Repeated<T>>`](RepeatedView). |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 320 | pub struct RepeatedIter<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 321 | view: RepeatedView<'msg, T>, |
| 322 | current_index: usize, |
| 323 | } |
| 324 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 325 | impl<'msg, T> Debug for RepeatedIter<'msg, T> { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 326 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 327 | f.debug_struct("RepeatedIter") |
| 328 | .field("view", &self.view) |
| 329 | .field("current_index", &self.current_index) |
| 330 | .finish() |
| 331 | } |
| 332 | } |
| 333 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 334 | /// A `repeated` field of `T`, used as the owned target for `Proxied`. |
| 335 | /// |
| 336 | /// Users will generally write [`View<Repeated<T>>`](RepeatedView) or |
| 337 | /// [`Mut<Repeated<T>>`](RepeatedMut) to access the repeated elements |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 338 | pub struct Repeated<T: ProxiedInRepeated> { |
Jakob Buchgraber | b6e0a48 | 2024-05-06 05:16:05 -0700 | [diff] [blame] | 339 | pub(crate) inner: InnerRepeated, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 340 | _phantom: PhantomData<T>, |
| 341 | } |
| 342 | |
Jakob Buchgraber | 3c9978d | 2024-06-04 01:27:57 -0700 | [diff] [blame] | 343 | // SAFETY: `Repeated` is Sync because it does not implement interior mutability. |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 344 | unsafe impl<T: ProxiedInRepeated> Sync for Repeated<T> {} |
Jakob Buchgraber | 3c9978d | 2024-06-04 01:27:57 -0700 | [diff] [blame] | 345 | |
| 346 | // SAFETY: `Repeated` is Send because it's not bound to a specific thread e.g. |
| 347 | // it does not use thread-local data or similar. |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 348 | unsafe impl<T: ProxiedInRepeated> Send for Repeated<T> {} |
Jakob Buchgraber | 3c9978d | 2024-06-04 01:27:57 -0700 | [diff] [blame] | 349 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 350 | impl<T: ProxiedInRepeated> Repeated<T> { |
Jakob Buchgraber | ad5e55a | 2024-03-12 05:45:32 -0700 | [diff] [blame] | 351 | pub fn new() -> Self { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 352 | T::repeated_new(Private) |
| 353 | } |
| 354 | |
Protobuf Team Bot | 9ff062c | 2024-03-12 04:48:15 -0700 | [diff] [blame] | 355 | pub(crate) fn from_inner(inner: InnerRepeated) -> Self { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 356 | Self { inner, _phantom: PhantomData } |
| 357 | } |
| 358 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 359 | pub(crate) fn as_mut(&mut self) -> RepeatedMut<'_, T> { |
Protobuf Team Bot | 9ff062c | 2024-03-12 04:48:15 -0700 | [diff] [blame] | 360 | RepeatedMut { inner: self.inner.as_mut(), _phantom: PhantomData } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 364 | impl<T: ProxiedInRepeated> Default for Repeated<T> { |
Jakob Buchgraber | ad5e55a | 2024-03-12 05:45:32 -0700 | [diff] [blame] | 365 | fn default() -> Self { |
| 366 | Repeated::new() |
| 367 | } |
| 368 | } |
| 369 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 370 | impl<T: ProxiedInRepeated> Drop for Repeated<T> { |
Alyssa Haroldsen | f51182b | 2023-12-14 14:07:40 -0800 | [diff] [blame] | 371 | fn drop(&mut self) { |
| 372 | // SAFETY: only called once |
| 373 | unsafe { T::repeated_free(Private, self) } |
| 374 | } |
| 375 | } |
| 376 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 377 | impl<T> Proxied for Repeated<T> |
| 378 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 379 | T: ProxiedInRepeated, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 380 | { |
| 381 | type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg; |
Jakob Buchgraber | 1a7ce61 | 2024-04-24 06:21:54 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | impl<T> MutProxied for Repeated<T> |
| 385 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 386 | T: ProxiedInRepeated, |
Jakob Buchgraber | 1a7ce61 | 2024-04-24 06:21:54 -0700 | [diff] [blame] | 387 | { |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 388 | type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg; |
| 389 | } |
| 390 | |
| 391 | impl<'msg, T> ViewProxy<'msg> for RepeatedView<'msg, T> |
| 392 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 393 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 394 | { |
| 395 | type Proxied = Repeated<T>; |
| 396 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 397 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 398 | fn as_view(&self) -> View<'_, Self::Proxied> { |
| 399 | *self |
| 400 | } |
| 401 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 402 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 403 | fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied> |
| 404 | where |
| 405 | 'msg: 'shorter, |
| 406 | { |
| 407 | RepeatedView { raw: self.raw, _phantom: PhantomData } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | impl<'msg, T> ViewProxy<'msg> for RepeatedMut<'msg, T> |
| 412 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 413 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 414 | { |
| 415 | type Proxied = Repeated<T>; |
| 416 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 417 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 418 | fn as_view(&self) -> View<'_, Self::Proxied> { |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 419 | RepeatedView { raw: self.inner.raw, _phantom: PhantomData } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 422 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 423 | fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied> |
| 424 | where |
| 425 | 'msg: 'shorter, |
| 426 | { |
Alyssa Haroldsen | b6ea6f9 | 2024-01-17 16:50:36 -0800 | [diff] [blame] | 427 | RepeatedView { raw: self.inner.raw, _phantom: PhantomData } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 428 | } |
| 429 | } |
| 430 | |
| 431 | impl<'msg, T> MutProxy<'msg> for RepeatedMut<'msg, T> |
| 432 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 433 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 434 | { |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 435 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 436 | fn as_mut(&mut self) -> Mut<'_, Self::Proxied> { |
| 437 | RepeatedMut { inner: self.inner, _phantom: PhantomData } |
| 438 | } |
| 439 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 440 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 441 | fn into_mut<'shorter>(self) -> Mut<'shorter, Self::Proxied> |
| 442 | where |
| 443 | 'msg: 'shorter, |
| 444 | { |
| 445 | RepeatedMut { inner: self.inner, _phantom: PhantomData } |
| 446 | } |
| 447 | } |
| 448 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 449 | impl<'msg, T> iter::Iterator for RepeatedIter<'msg, T> |
| 450 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 451 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 452 | { |
| 453 | type Item = View<'msg, T>; |
| 454 | |
Derek Benson | 904266d | 2024-05-13 07:16:57 -0700 | [diff] [blame] | 455 | #[inline] |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 456 | fn next(&mut self) -> Option<Self::Item> { |
| 457 | let val = self.view.get(self.current_index); |
| 458 | if val.is_some() { |
| 459 | self.current_index += 1; |
| 460 | } |
| 461 | val |
| 462 | } |
Alyssa Haroldsen | 3985082 | 2024-02-06 11:06:14 -0800 | [diff] [blame] | 463 | |
| 464 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 465 | let len = self.len(); |
| 466 | (len, Some(len)) |
| 467 | } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 468 | } |
| 469 | |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 470 | impl<'msg, T: ProxiedInRepeated> ExactSizeIterator for RepeatedIter<'msg, T> { |
Protobuf Team Bot | 14dd8e9 | 2023-12-15 11:05:56 -0800 | [diff] [blame] | 471 | fn len(&self) -> usize { |
Alyssa Haroldsen | 3985082 | 2024-02-06 11:06:14 -0800 | [diff] [blame] | 472 | self.view.len() - self.current_index |
Protobuf Team Bot | 14dd8e9 | 2023-12-15 11:05:56 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
Alyssa Haroldsen | 3985082 | 2024-02-06 11:06:14 -0800 | [diff] [blame] | 476 | // TODO: impl DoubleEndedIterator |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 477 | impl<'msg, T: ProxiedInRepeated> FusedIterator for RepeatedIter<'msg, T> {} |
Protobuf Team Bot | 14dd8e9 | 2023-12-15 11:05:56 -0800 | [diff] [blame] | 478 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 479 | impl<'msg, T> iter::IntoIterator for RepeatedView<'msg, T> |
| 480 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 481 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 482 | { |
| 483 | type Item = View<'msg, T>; |
| 484 | type IntoIter = RepeatedIter<'msg, T>; |
| 485 | |
| 486 | fn into_iter(self) -> Self::IntoIter { |
| 487 | RepeatedIter { view: self, current_index: 0 } |
| 488 | } |
| 489 | } |
| 490 | |
Alyssa Haroldsen | e16dd47 | 2024-01-17 14:02:06 -0800 | [diff] [blame] | 491 | impl<'msg, T> iter::IntoIterator for &'_ RepeatedView<'msg, T> |
| 492 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 493 | T: ProxiedInRepeated + 'msg, |
Alyssa Haroldsen | e16dd47 | 2024-01-17 14:02:06 -0800 | [diff] [blame] | 494 | { |
| 495 | type Item = View<'msg, T>; |
| 496 | type IntoIter = RepeatedIter<'msg, T>; |
| 497 | |
| 498 | fn into_iter(self) -> Self::IntoIter { |
| 499 | RepeatedIter { view: *self, current_index: 0 } |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | impl<'borrow, T> iter::IntoIterator for &'borrow RepeatedMut<'_, T> |
| 504 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 505 | T: ProxiedInRepeated + 'borrow, |
Alyssa Haroldsen | e16dd47 | 2024-01-17 14:02:06 -0800 | [diff] [blame] | 506 | { |
| 507 | type Item = View<'borrow, T>; |
| 508 | type IntoIter = RepeatedIter<'borrow, T>; |
| 509 | |
| 510 | fn into_iter(self) -> Self::IntoIter { |
| 511 | RepeatedIter { view: self.as_view(), current_index: 0 } |
| 512 | } |
| 513 | } |
| 514 | |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 515 | impl<'msg, 'view, T, ViewT> Extend<ViewT> for RepeatedMut<'msg, T> |
| 516 | where |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 517 | T: ProxiedInRepeated + 'view, |
| 518 | ViewT: IntoProxied<T>, |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 519 | { |
| 520 | fn extend<I: IntoIterator<Item = ViewT>>(&mut self, iter: I) { |
Derek Benson | f2d8c2b | 2024-05-13 05:48:10 -0700 | [diff] [blame] | 521 | let iter = iter.into_iter(); |
| 522 | T::repeated_reserve(self.as_mut(), iter.size_hint().0); |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 523 | for item in iter { |
Jakob Buchgraber | 0d6e979 | 2024-07-09 04:44:21 -0700 | [diff] [blame] | 524 | self.push(item); |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 529 | #[cfg(test)] |
| 530 | mod tests { |
| 531 | use super::*; |
| 532 | use googletest::prelude::*; |
| 533 | |
| 534 | #[test] |
| 535 | fn test_primitive_repeated() { |
| 536 | macro_rules! primitive_repeated_tests { |
| 537 | ($($t:ty => [$($vals:expr),* $(,)?]),* $(,)?) => { |
| 538 | $({ |
| 539 | // Constructs a new, owned, `Repeated`, only used for tests. |
| 540 | let mut r = Repeated::<$t>::new(); |
| 541 | let mut r = r.as_mut(); |
| 542 | assert_that!(r.len(), eq(0)); |
| 543 | assert!(r.iter().next().is_none(), "starts with empty iter"); |
| 544 | assert!(r.iter().next().is_none(), "starts with empty mut iter"); |
| 545 | assert!(r.is_empty(), "starts is_empty"); |
| 546 | |
| 547 | let mut expected_len = 0usize; |
| 548 | $( |
| 549 | let val: View<$t> = $vals; |
| 550 | r.push(val); |
| 551 | assert_that!(r.get(expected_len), eq(Some(val))); |
| 552 | expected_len += 1; |
| 553 | assert_that!(r.len(), eq(expected_len)); |
| 554 | |
| 555 | )* |
Bastien Jacot-Guillarmod | 7b3682f | 2024-05-03 09:50:45 -0700 | [diff] [blame] | 556 | assert_that!(r, elements_are![$(eq($vals)),*]); |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 557 | r.set(0, <$t as Default>::default()); |
| 558 | assert_that!(r.get(0).expect("elem 0"), eq(<$t as Default>::default())); |
Alyssa Haroldsen | f51182b | 2023-12-14 14:07:40 -0800 | [diff] [blame] | 559 | |
| 560 | r.clear(); |
| 561 | assert!(r.is_empty(), "is_empty after clear"); |
| 562 | assert!(r.iter().next().is_none(), "iter empty after clear"); |
| 563 | assert!(r.into_iter().next().is_none(), "mut iter empty after clear"); |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 564 | })* |
| 565 | } |
| 566 | } |
| 567 | primitive_repeated_tests!( |
| 568 | u32 => [1,2,3], |
| 569 | i32 => [1,2], |
| 570 | f64 => [10.0, 0.1234f64], |
| 571 | bool => [false, true, true, false], |
| 572 | ); |
| 573 | } |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 574 | |
| 575 | #[test] |
| 576 | fn test_repeated_extend() { |
| 577 | let mut r = Repeated::<i32>::new(); |
Protobuf Team Bot | 063c198 | 2024-04-02 06:48:42 -0700 | [diff] [blame] | 578 | |
| 579 | r.as_mut().extend([0; 0]); |
| 580 | assert_that!(r.as_mut().len(), eq(0)); |
| 581 | |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 582 | r.as_mut().extend([0, 1]); |
Bastien Jacot-Guillarmod | 7b3682f | 2024-05-03 09:50:45 -0700 | [diff] [blame] | 583 | assert_that!(r.as_mut(), elements_are![eq(0), eq(1)]); |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 584 | let mut x = Repeated::<i32>::new(); |
| 585 | x.as_mut().extend([2, 3]); |
| 586 | |
| 587 | r.as_mut().extend(&x.as_mut()); |
| 588 | |
Bastien Jacot-Guillarmod | 7b3682f | 2024-05-03 09:50:45 -0700 | [diff] [blame] | 589 | assert_that!(r.as_mut(), elements_are![eq(0), eq(1), eq(2), eq(3)]); |
Protobuf Team Bot | 7f6a0ba | 2024-03-29 10:26:49 -0700 | [diff] [blame] | 590 | } |
Alyssa Haroldsen | 6ae76fd | 2023-12-14 11:44:00 -0800 | [diff] [blame] | 591 | } |