blob: b1a406b3cd5708fa7c5f9a3fbd5d143722160778 [file] [log] [blame]
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -07001// 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 Haroldsen6ae76fd2023-12-14 11:44:00 -08008use std::iter;
Protobuf Team Bot14dd8e92023-12-15 11:05:56 -08009use std::iter::FusedIterator;
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070010/// Repeated scalar fields are implemented around the runtime-specific
11/// `RepeatedField` struct. `RepeatedField` stores an opaque pointer to the
12/// runtime-specific representation of a repeated scalar (`upb_Array*` on upb,
13/// and `RepeatedField<T>*` on cpp).
14use std::marker::PhantomData;
Protobuf Team Bote26ef862024-08-26 05:14:52 -070015use std::fmt::{self, Debug};
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070016
17use crate::{
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -070018 AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, MutProxy, Proxied, Proxy, View,
19 ViewProxy,
Protobuf Team Botc7d46972024-07-30 05:39:53 -070020 __internal::{Private, SealedInternal},
Protobuf Team Bot2678e102024-04-12 13:50:28 -070021 __runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070022};
23
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080024/// Views the elements in a `repeated` field of `T`.
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070025#[repr(transparent)]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070026pub struct RepeatedView<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080027 // This does not need to carry an arena in upb, so it can be just the raw repeated field
28 raw: RawRepeatedField,
29 _phantom: PhantomData<&'msg T>,
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070030}
31
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070032impl<'msg, T> Copy for RepeatedView<'msg, T> {}
33impl<'msg, T> Clone for RepeatedView<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080034 fn clone(&self) -> Self {
35 *self
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070036 }
37}
38
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070039unsafe impl<'msg, T> Sync for RepeatedView<'msg, T> {}
40unsafe impl<'msg, T> Send for RepeatedView<'msg, T> {}
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070041
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070042impl<'msg, T> Debug for RepeatedView<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080043 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.debug_struct("RepeatedView").field("raw", &self.raw).finish()
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070045 }
46}
47
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080048/// Mutates the elements in a `repeated` field of `T`.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070049pub struct RepeatedMut<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080050 pub(crate) inner: InnerRepeatedMut<'msg>,
51 _phantom: PhantomData<&'msg mut T>,
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070052}
53
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070054unsafe impl<'msg, T> Sync for RepeatedMut<'msg, T> {}
Kevin King65cdac42023-10-23 14:50:42 -070055
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070056impl<'msg, T> Debug for RepeatedMut<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080057 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -080058 f.debug_struct("RepeatedMut").field("raw", &self.inner.raw).finish()
Protobuf Team Bote1bb7d62023-10-17 14:15:37 -070059 }
60}
61
Alyssa Haroldsen3657e052024-02-05 12:51:41 -080062#[doc(hidden)]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070063impl<'msg, T> RepeatedView<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080064 #[doc(hidden)]
Derek Benson904266d2024-05-13 07:16:57 -070065 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080066 pub fn as_raw(&self, _private: Private) -> RawRepeatedField {
67 self.raw
68 }
69
70 /// # Safety
71 /// - `inner` must be valid to read from for `'msg`
72 #[doc(hidden)]
Derek Benson904266d2024-05-13 07:16:57 -070073 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080074 pub unsafe fn from_raw(_private: Private, raw: RawRepeatedField) -> Self {
75 Self { raw, _phantom: PhantomData }
76 }
Alyssa Haroldsen3657e052024-02-05 12:51:41 -080077}
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080078
Alyssa Haroldsen3657e052024-02-05 12:51:41 -080079impl<'msg, T> RepeatedView<'msg, T>
80where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -070081 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen3657e052024-02-05 12:51:41 -080082{
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -080083 /// Gets the length of the repeated field.
Derek Benson904266d2024-05-13 07:16:57 -070084 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080085 pub fn len(&self) -> usize {
86 T::repeated_len(*self)
87 }
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -080088
89 /// Returns true if the repeated field has no values.
Derek Benson904266d2024-05-13 07:16:57 -070090 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080091 pub fn is_empty(&self) -> bool {
92 self.len() == 0
93 }
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -080094
95 /// Gets the value at `index`.
96 ///
97 /// Returns `None` if `index > len`.
Derek Benson904266d2024-05-13 07:16:57 -070098 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -080099 pub fn get(self, index: usize) -> Option<View<'msg, T>> {
100 if index >= self.len() {
101 return None;
102 }
103 // SAFETY: `index` has been checked to be in-bounds
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -0800104 Some(unsafe { self.get_unchecked(index) })
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800105 }
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -0800106
107 /// Gets the value at `index` without bounds-checking.
108 ///
109 /// # Safety
110 /// Undefined behavior if `index >= len`
Derek Benson904266d2024-05-13 07:16:57 -0700111 #[inline]
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -0800112 pub unsafe fn get_unchecked(self, index: usize) -> View<'msg, T> {
113 // SAFETY: in-bounds as promised
114 unsafe { T::repeated_get_unchecked(self, index) }
115 }
116
117 /// Iterates over the values in the repeated field.
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800118 pub fn iter(self) -> RepeatedIter<'msg, T> {
119 self.into_iter()
120 }
121}
122
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800123#[doc(hidden)]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700124impl<'msg, T> RepeatedMut<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800125 /// # Safety
126 /// - `inner` must be valid to read and write from for `'msg`
127 /// - There must be no aliasing references or mutations on the same
128 /// underlying object.
129 #[doc(hidden)]
Derek Benson904266d2024-05-13 07:16:57 -0700130 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800131 pub unsafe fn from_inner(_private: Private, inner: InnerRepeatedMut<'msg>) -> Self {
132 Self { inner, _phantom: PhantomData }
133 }
134
135 #[doc(hidden)]
Derek Benson904266d2024-05-13 07:16:57 -0700136 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800137 pub fn as_raw(&mut self, _private: Private) -> RawRepeatedField {
138 self.inner.raw
139 }
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800140}
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800141
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800142impl<'msg, T> RepeatedMut<'msg, T>
143where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700144 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen3657e052024-02-05 12:51:41 -0800145{
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800146 /// Gets the length of the repeated field.
Derek Benson904266d2024-05-13 07:16:57 -0700147 #[inline]
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800148 pub fn len(&self) -> usize {
149 self.as_view().len()
150 }
151
152 /// Returns true if the repeated field has no values.
Derek Benson904266d2024-05-13 07:16:57 -0700153 #[inline]
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800154 pub fn is_empty(&self) -> bool {
155 self.len() == 0
156 }
157
158 /// Gets the value at `index`.
159 ///
160 /// Returns `None` if `index > len`.
Derek Benson904266d2024-05-13 07:16:57 -0700161 #[inline]
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800162 pub fn get(&self, index: usize) -> Option<View<T>> {
163 self.as_view().get(index)
164 }
165
166 /// Gets the value at `index` without bounds-checking.
167 ///
168 /// # Safety
169 /// Undefined behavior if `index >= len`
Derek Benson904266d2024-05-13 07:16:57 -0700170 #[inline]
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800171 pub unsafe fn get_unchecked(&self, index: usize) -> View<T> {
172 // SAFETY: in-bounds as promised
173 unsafe { self.as_view().get_unchecked(index) }
174 }
175
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800176 /// Appends `val` to the end of the repeated field.
Derek Benson904266d2024-05-13 07:16:57 -0700177 #[inline]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700178 pub fn push(&mut self, val: impl IntoProxied<T>) {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800179 T::repeated_push(self.as_mut(), val);
180 }
181
182 /// Sets the value at `index` to the value `val`.
183 ///
184 /// # Panics
185 /// Panics if `index >= len`
Derek Benson904266d2024-05-13 07:16:57 -0700186 #[inline]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700187 pub fn set(&mut self, index: usize, val: impl IntoProxied<T>) {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800188 let len = self.len();
189 if index >= len {
190 panic!("index {index} >= repeated len {len}");
191 }
Alyssa Haroldsenfcf0d012023-12-18 10:13:01 -0800192 unsafe { self.set_unchecked(index, val) }
193 }
194
195 /// Sets the value at `index` to the value `val`.
196 ///
197 /// # Safety
198 /// Undefined behavior if `index >= len`
Derek Benson904266d2024-05-13 07:16:57 -0700199 #[inline]
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700200 pub unsafe fn set_unchecked(&mut self, index: usize, val: impl IntoProxied<T>) {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800201 unsafe { T::repeated_set_unchecked(self.as_mut(), index, val) }
202 }
203
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800204 /// Iterates over the values in the repeated field.
205 pub fn iter(&self) -> RepeatedIter<T> {
206 self.as_view().into_iter()
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800207 }
208
209 /// Copies from the `src` repeated field into this one.
210 ///
211 /// Also provided by [`MutProxy::set`].
212 pub fn copy_from(&mut self, src: RepeatedView<'_, T>) {
213 T::repeated_copy_from(src, self.as_mut())
214 }
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800215
216 /// Clears the repeated field.
217 pub fn clear(&mut self) {
218 T::repeated_clear(self.as_mut())
219 }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800220}
221
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700222impl<T> Repeated<T>
223where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700224 T: ProxiedInRepeated,
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700225{
226 pub fn as_view(&self) -> View<Repeated<T>> {
227 RepeatedView { raw: self.inner.raw(), _phantom: PhantomData }
228 }
229
230 #[doc(hidden)]
231 pub fn inner(&self, _private: Private) -> &InnerRepeated {
232 &self.inner
233 }
234}
235
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700236impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedView<'msg, T>
237where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700238 T: 'msg + ProxiedInRepeated,
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700239{
Marcel Hlopkofe696392024-06-12 05:26:09 -0700240 fn into_proxied(self, _private: Private) -> Repeated<T> {
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700241 let mut repeated: Repeated<T> = Repeated::new();
242 T::repeated_copy_from(self, repeated.as_mut());
243 repeated
244 }
245}
246
247impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedMut<'msg, T>
248where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700249 T: 'msg + ProxiedInRepeated,
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700250{
Marcel Hlopkofe696392024-06-12 05:26:09 -0700251 fn into_proxied(self, _private: Private) -> Repeated<T> {
252 IntoProxied::into_proxied(self.as_view(), _private)
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700253 }
254}
255
Derek Bensoncee9da92024-07-15 11:59:14 -0700256impl<'msg, T, I, U> IntoProxied<Repeated<T>> for I
257where
258 I: Iterator<Item = U>,
259 T: 'msg + ProxiedInRepeated,
260 U: IntoProxied<T>,
261{
262 fn into_proxied(self, _private: Private) -> Repeated<T> {
263 let mut repeated: Repeated<T> = Repeated::new();
264 repeated.as_mut().extend(self);
265 repeated
266 }
267}
268
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800269/// Types that can appear in a `Repeated<T>`.
270///
271/// This trait is implemented by generated code to communicate how the proxied
272/// type can be manipulated for a repeated field.
273///
274/// Scalars and messages implement `ProxiedInRepeated`.
275///
276/// # Safety
277/// - It must be sound to call `*_unchecked*(x)` with an `index` less than
278/// `repeated_len(x)`.
279pub unsafe trait ProxiedInRepeated: Proxied {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800280 /// Constructs a new owned `Repeated` field.
281 #[doc(hidden)]
Derek Benson9c994242024-07-15 12:36:48 -0700282 fn repeated_new(_private: Private) -> Repeated<Self>;
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800283
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800284 /// Frees the repeated field in-place, for use in `Drop`.
285 ///
286 /// # Safety
287 /// - After `repeated_free`, no other methods on the input are safe to call.
288 #[doc(hidden)]
Derek Benson9c994242024-07-15 12:36:48 -0700289 unsafe fn repeated_free(_private: Private, _repeated: &mut Repeated<Self>);
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800290
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800291 /// Gets the length of the repeated field.
292 fn repeated_len(repeated: View<Repeated<Self>>) -> usize;
293
294 /// Appends a new element to the end of the repeated field.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700295 fn repeated_push(repeated: Mut<Repeated<Self>>, val: impl IntoProxied<Self>);
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800296
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800297 /// Clears the repeated field of elements.
298 fn repeated_clear(repeated: Mut<Repeated<Self>>);
299
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800300 /// # Safety
301 /// `index` must be less than `Self::repeated_len(repeated)`
302 unsafe fn repeated_get_unchecked(repeated: View<Repeated<Self>>, index: usize) -> View<Self>;
303
304 /// # Safety
305 /// `index` must be less than `Self::repeated_len(repeated)`
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700306 unsafe fn repeated_set_unchecked(
307 repeated: Mut<Repeated<Self>>,
308 index: usize,
309 val: impl IntoProxied<Self>,
310 );
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800311
312 /// Copies the values in the `src` repeated field into `dest`.
313 fn repeated_copy_from(src: View<Repeated<Self>>, dest: Mut<Repeated<Self>>);
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700314
315 /// Ensures that the repeated field has enough space allocated to insert at
316 /// least `additional` values without an allocation.
317 fn repeated_reserve(repeated: Mut<Repeated<Self>>, additional: usize);
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800318}
319
320/// An iterator over the values inside of a [`View<Repeated<T>>`](RepeatedView).
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700321pub struct RepeatedIter<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800322 view: RepeatedView<'msg, T>,
323 current_index: usize,
324}
325
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700326impl<'msg, T> Debug for RepeatedIter<'msg, T> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800327 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
328 f.debug_struct("RepeatedIter")
329 .field("view", &self.view)
330 .field("current_index", &self.current_index)
331 .finish()
332 }
333}
334
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800335/// A `repeated` field of `T`, used as the owned target for `Proxied`.
336///
337/// Users will generally write [`View<Repeated<T>>`](RepeatedView) or
338/// [`Mut<Repeated<T>>`](RepeatedMut) to access the repeated elements
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700339pub struct Repeated<T: ProxiedInRepeated> {
Jakob Buchgraberb6e0a482024-05-06 05:16:05 -0700340 pub(crate) inner: InnerRepeated,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800341 _phantom: PhantomData<T>,
342}
343
Jakob Buchgraber3c9978d2024-06-04 01:27:57 -0700344// SAFETY: `Repeated` is Sync because it does not implement interior mutability.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700345unsafe impl<T: ProxiedInRepeated> Sync for Repeated<T> {}
Jakob Buchgraber3c9978d2024-06-04 01:27:57 -0700346
347// SAFETY: `Repeated` is Send because it's not bound to a specific thread e.g.
348// it does not use thread-local data or similar.
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700349unsafe impl<T: ProxiedInRepeated> Send for Repeated<T> {}
Jakob Buchgraber3c9978d2024-06-04 01:27:57 -0700350
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700351impl<T: ProxiedInRepeated> Repeated<T> {
Jakob Buchgraberad5e55a2024-03-12 05:45:32 -0700352 pub fn new() -> Self {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800353 T::repeated_new(Private)
354 }
Protobuf Team Bote26ef862024-08-26 05:14:52 -0700355 #[doc(hidden)]
Derek Benson9c994242024-07-15 12:36:48 -0700356 pub fn from_inner(_private: Private, inner: InnerRepeated) -> Self {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800357 Self { inner, _phantom: PhantomData }
358 }
359
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800360 pub(crate) fn as_mut(&mut self) -> RepeatedMut<'_, T> {
Protobuf Team Bot9ff062c2024-03-12 04:48:15 -0700361 RepeatedMut { inner: self.inner.as_mut(), _phantom: PhantomData }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800362 }
363}
364
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700365impl<T: ProxiedInRepeated> Default for Repeated<T> {
Jakob Buchgraberad5e55a2024-03-12 05:45:32 -0700366 fn default() -> Self {
367 Repeated::new()
368 }
369}
370
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700371impl<T: ProxiedInRepeated> Drop for Repeated<T> {
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800372 fn drop(&mut self) {
373 // SAFETY: only called once
374 unsafe { T::repeated_free(Private, self) }
375 }
376}
377
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800378impl<T> Proxied for Repeated<T>
379where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700380 T: ProxiedInRepeated,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800381{
382 type View<'msg> = RepeatedView<'msg, T> where Repeated<T>: 'msg;
Jakob Buchgraber1a7ce612024-04-24 06:21:54 -0700383}
384
Protobuf Team Botc7d46972024-07-30 05:39:53 -0700385impl<T> SealedInternal for Repeated<T> where T: ProxiedInRepeated {}
386
Protobuf Team Bote6304eb2024-07-24 12:28:32 -0700387impl<T> AsView for Repeated<T>
388where
389 T: ProxiedInRepeated,
390{
391 type Proxied = Self;
392
393 fn as_view(&self) -> RepeatedView<'_, T> {
394 self.as_view()
395 }
396}
397
Jakob Buchgraber1a7ce612024-04-24 06:21:54 -0700398impl<T> MutProxied for Repeated<T>
399where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700400 T: ProxiedInRepeated,
Jakob Buchgraber1a7ce612024-04-24 06:21:54 -0700401{
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800402 type Mut<'msg> = RepeatedMut<'msg, T> where Repeated<T>: 'msg;
403}
404
Protobuf Team Bote6304eb2024-07-24 12:28:32 -0700405impl<T> AsMut for Repeated<T>
406where
407 T: ProxiedInRepeated,
408{
409 type MutProxied = Self;
410
411 fn as_mut(&mut self) -> RepeatedMut<'_, T> {
412 self.as_mut()
413 }
414}
415
Protobuf Team Botc7d46972024-07-30 05:39:53 -0700416impl<'msg, T> SealedInternal for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}
417
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700418impl<'msg, T> Proxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}
419
420impl<'msg, T> AsView for RepeatedView<'msg, T>
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800421where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700422 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800423{
424 type Proxied = Repeated<T>;
425
Derek Benson904266d2024-05-13 07:16:57 -0700426 #[inline]
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700427 fn as_view(&self) -> View<'msg, Self::Proxied> {
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800428 *self
429 }
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700430}
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800431
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700432impl<'msg, T> IntoView<'msg> for RepeatedView<'msg, T>
433where
434 T: ProxiedInRepeated + 'msg,
435{
Derek Benson904266d2024-05-13 07:16:57 -0700436 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800437 fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied>
438 where
439 'msg: 'shorter,
440 {
441 RepeatedView { raw: self.raw, _phantom: PhantomData }
442 }
443}
444
Protobuf Team Botcf948e42024-07-18 10:01:55 -0700445impl<'msg, T> ViewProxy<'msg> for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}
446
Protobuf Team Botc7d46972024-07-30 05:39:53 -0700447impl<'msg, T> SealedInternal for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}
448
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700449impl<'msg, T> Proxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}
450
451impl<'msg, T> AsView for RepeatedMut<'msg, T>
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800452where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700453 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800454{
455 type Proxied = Repeated<T>;
456
Derek Benson904266d2024-05-13 07:16:57 -0700457 #[inline]
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700458 fn as_view(&self) -> RepeatedView<'_, T> {
Alyssa Haroldsenb6ea6f92024-01-17 16:50:36 -0800459 RepeatedView { raw: self.inner.raw, _phantom: PhantomData }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800460 }
461}
462
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700463impl<'msg, T> IntoView<'msg> for RepeatedMut<'msg, T>
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800464where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700465 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800466{
Derek Benson904266d2024-05-13 07:16:57 -0700467 #[inline]
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700468 fn into_view<'shorter>(self) -> RepeatedView<'shorter, T>
469 where
470 'msg: 'shorter,
471 {
472 RepeatedView { raw: self.inner.raw, _phantom: PhantomData }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800473 }
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700474}
475
476impl<'msg, T> AsMut for RepeatedMut<'msg, T>
477where
478 T: ProxiedInRepeated + 'msg,
479{
480 type MutProxied = Repeated<T>;
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800481
Derek Benson904266d2024-05-13 07:16:57 -0700482 #[inline]
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700483 fn as_mut(&mut self) -> RepeatedMut<'_, T> {
484 RepeatedMut { inner: self.inner, _phantom: PhantomData }
485 }
486}
487
488impl<'msg, T> IntoMut<'msg> for RepeatedMut<'msg, T>
489where
490 T: ProxiedInRepeated + 'msg,
491{
492 #[inline]
493 fn into_mut<'shorter>(self) -> RepeatedMut<'shorter, T>
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800494 where
495 'msg: 'shorter,
496 {
497 RepeatedMut { inner: self.inner, _phantom: PhantomData }
498 }
499}
500
Protobuf Team Bot3c95fc82024-07-23 07:54:29 -0700501impl<'msg, T> MutProxy<'msg> for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}
502
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800503impl<'msg, T> iter::Iterator for RepeatedIter<'msg, T>
504where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700505 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800506{
507 type Item = View<'msg, T>;
508
Derek Benson904266d2024-05-13 07:16:57 -0700509 #[inline]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800510 fn next(&mut self) -> Option<Self::Item> {
511 let val = self.view.get(self.current_index);
512 if val.is_some() {
513 self.current_index += 1;
514 }
515 val
516 }
Alyssa Haroldsen39850822024-02-06 11:06:14 -0800517
518 fn size_hint(&self) -> (usize, Option<usize>) {
519 let len = self.len();
520 (len, Some(len))
521 }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800522}
523
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700524impl<'msg, T: ProxiedInRepeated> ExactSizeIterator for RepeatedIter<'msg, T> {
Protobuf Team Bot14dd8e92023-12-15 11:05:56 -0800525 fn len(&self) -> usize {
Alyssa Haroldsen39850822024-02-06 11:06:14 -0800526 self.view.len() - self.current_index
Protobuf Team Bot14dd8e92023-12-15 11:05:56 -0800527 }
528}
529
Alyssa Haroldsen39850822024-02-06 11:06:14 -0800530// TODO: impl DoubleEndedIterator
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700531impl<'msg, T: ProxiedInRepeated> FusedIterator for RepeatedIter<'msg, T> {}
Protobuf Team Bot14dd8e92023-12-15 11:05:56 -0800532
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800533impl<'msg, T> iter::IntoIterator for RepeatedView<'msg, T>
534where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700535 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800536{
537 type Item = View<'msg, T>;
538 type IntoIter = RepeatedIter<'msg, T>;
539
540 fn into_iter(self) -> Self::IntoIter {
541 RepeatedIter { view: self, current_index: 0 }
542 }
543}
544
Alyssa Haroldsene16dd472024-01-17 14:02:06 -0800545impl<'msg, T> iter::IntoIterator for &'_ RepeatedView<'msg, T>
546where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700547 T: ProxiedInRepeated + 'msg,
Alyssa Haroldsene16dd472024-01-17 14:02:06 -0800548{
549 type Item = View<'msg, T>;
550 type IntoIter = RepeatedIter<'msg, T>;
551
552 fn into_iter(self) -> Self::IntoIter {
553 RepeatedIter { view: *self, current_index: 0 }
554 }
555}
556
557impl<'borrow, T> iter::IntoIterator for &'borrow RepeatedMut<'_, T>
558where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700559 T: ProxiedInRepeated + 'borrow,
Alyssa Haroldsene16dd472024-01-17 14:02:06 -0800560{
561 type Item = View<'borrow, T>;
562 type IntoIter = RepeatedIter<'borrow, T>;
563
564 fn into_iter(self) -> Self::IntoIter {
565 RepeatedIter { view: self.as_view(), current_index: 0 }
566 }
567}
568
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700569impl<'msg, 'view, T, ViewT> Extend<ViewT> for RepeatedMut<'msg, T>
570where
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700571 T: ProxiedInRepeated + 'view,
572 ViewT: IntoProxied<T>,
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700573{
574 fn extend<I: IntoIterator<Item = ViewT>>(&mut self, iter: I) {
Derek Bensonf2d8c2b2024-05-13 05:48:10 -0700575 let iter = iter.into_iter();
576 T::repeated_reserve(self.as_mut(), iter.size_hint().0);
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700577 for item in iter {
Jakob Buchgraber0d6e9792024-07-09 04:44:21 -0700578 self.push(item);
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700579 }
580 }
581}
582
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800583#[cfg(test)]
584mod tests {
585 use super::*;
586 use googletest::prelude::*;
587
Dmitri Gribenko744c9dd2024-08-12 01:43:18 -0700588 #[gtest]
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800589 fn test_primitive_repeated() {
590 macro_rules! primitive_repeated_tests {
591 ($($t:ty => [$($vals:expr),* $(,)?]),* $(,)?) => {
592 $({
593 // Constructs a new, owned, `Repeated`, only used for tests.
594 let mut r = Repeated::<$t>::new();
595 let mut r = r.as_mut();
596 assert_that!(r.len(), eq(0));
597 assert!(r.iter().next().is_none(), "starts with empty iter");
598 assert!(r.iter().next().is_none(), "starts with empty mut iter");
599 assert!(r.is_empty(), "starts is_empty");
600
601 let mut expected_len = 0usize;
602 $(
603 let val: View<$t> = $vals;
604 r.push(val);
605 assert_that!(r.get(expected_len), eq(Some(val)));
606 expected_len += 1;
607 assert_that!(r.len(), eq(expected_len));
608
609 )*
Bastien Jacot-Guillarmod7b3682f2024-05-03 09:50:45 -0700610 assert_that!(r, elements_are![$(eq($vals)),*]);
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800611 r.set(0, <$t as Default>::default());
612 assert_that!(r.get(0).expect("elem 0"), eq(<$t as Default>::default()));
Alyssa Haroldsenf51182b2023-12-14 14:07:40 -0800613
614 r.clear();
615 assert!(r.is_empty(), "is_empty after clear");
616 assert!(r.iter().next().is_none(), "iter empty after clear");
617 assert!(r.into_iter().next().is_none(), "mut iter empty after clear");
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800618 })*
619 }
620 }
621 primitive_repeated_tests!(
622 u32 => [1,2,3],
623 i32 => [1,2],
624 f64 => [10.0, 0.1234f64],
625 bool => [false, true, true, false],
626 );
627 }
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700628
Dmitri Gribenko744c9dd2024-08-12 01:43:18 -0700629 #[gtest]
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700630 fn test_repeated_extend() {
631 let mut r = Repeated::<i32>::new();
Protobuf Team Bot063c1982024-04-02 06:48:42 -0700632
633 r.as_mut().extend([0; 0]);
634 assert_that!(r.as_mut().len(), eq(0));
635
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700636 r.as_mut().extend([0, 1]);
Bastien Jacot-Guillarmod7b3682f2024-05-03 09:50:45 -0700637 assert_that!(r.as_mut(), elements_are![eq(0), eq(1)]);
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700638 let mut x = Repeated::<i32>::new();
639 x.as_mut().extend([2, 3]);
640
641 r.as_mut().extend(&x.as_mut());
642
Bastien Jacot-Guillarmod7b3682f2024-05-03 09:50:45 -0700643 assert_that!(r.as_mut(), elements_are![eq(0), eq(1), eq(2), eq(3)]);
Protobuf Team Bot7f6a0ba2024-03-29 10:26:49 -0700644 }
Derek Bensoncee9da92024-07-15 11:59:14 -0700645
Dmitri Gribenko744c9dd2024-08-12 01:43:18 -0700646 #[gtest]
Derek Bensoncee9da92024-07-15 11:59:14 -0700647 fn test_repeated_iter_into_proxied() {
648 let r: Repeated<i32> = [0, 1, 2, 3].into_iter().into_proxied(Private);
649 assert_that!(r.as_view(), elements_are![eq(0), eq(1), eq(2), eq(3)]);
650 }
Alyssa Haroldsen6ae76fd2023-12-14 11:44:00 -0800651}