inline repeated field methods

PiperOrigin-RevId: 633199701
pull/16790/head
Derek Benson 11 months ago committed by Copybara-Service
parent f2d8c2bdd8
commit 904266db57
  1. 9
      rust/cpp.rs
  2. 22
      rust/repeated.rs
  3. 8
      rust/upb.rs

@ -399,34 +399,43 @@ macro_rules! impl_repeated_primitives {
unsafe impl ProxiedInRepeated for $t {
#[allow(dead_code)]
#[inline]
fn repeated_new(_: Private) -> Repeated<$t> {
Repeated::from_inner(InnerRepeated {
raw: unsafe { $new_thunk() }
})
}
#[allow(dead_code)]
#[inline]
unsafe fn repeated_free(_: Private, f: &mut Repeated<$t>) {
unsafe { $free_thunk(f.as_mut().as_raw(Private)) }
}
#[inline]
fn repeated_len(f: View<Repeated<$t>>) -> usize {
unsafe { $size_thunk(f.as_raw(Private)) }
}
#[inline]
fn repeated_push(mut f: Mut<Repeated<$t>>, v: View<$t>) {
unsafe { $add_thunk(f.as_raw(Private), v.into()) }
}
#[inline]
fn repeated_clear(mut f: Mut<Repeated<$t>>) {
unsafe { $clear_thunk(f.as_raw(Private)) }
}
#[inline]
unsafe fn repeated_get_unchecked(f: View<Repeated<$t>>, i: usize) -> View<$t> {
<$t as CppTypeConversions>::elem_to_view(
unsafe { $get_thunk(f.as_raw(Private), i) })
}
#[inline]
unsafe fn repeated_set_unchecked(mut f: Mut<Repeated<$t>>, i: usize, v: View<$t>) {
unsafe { $set_thunk(f.as_raw(Private), i, v.into()) }
}
#[inline]
fn repeated_copy_from(src: View<Repeated<$t>>, mut dest: Mut<Repeated<$t>>) {
unsafe { $copy_from_thunk(src.as_raw(Private), dest.as_raw(Private)) }
}
#[inline]
fn repeated_reserve(mut f: Mut<Repeated<$t>>, additional: usize) {
unsafe { $reserve_thunk(f.as_raw(Private), additional) }
}

@ -61,6 +61,7 @@ impl<'msg, T: ?Sized> Debug for RepeatedMut<'msg, T> {
#[doc(hidden)]
impl<'msg, T: ?Sized> RepeatedView<'msg, T> {
#[doc(hidden)]
#[inline]
pub fn as_raw(&self, _private: Private) -> RawRepeatedField {
self.raw
}
@ -68,6 +69,7 @@ impl<'msg, T: ?Sized> RepeatedView<'msg, T> {
/// # Safety
/// - `inner` must be valid to read from for `'msg`
#[doc(hidden)]
#[inline]
pub unsafe fn from_raw(_private: Private, raw: RawRepeatedField) -> Self {
Self { raw, _phantom: PhantomData }
}
@ -78,11 +80,13 @@ where
T: ProxiedInRepeated + ?Sized + 'msg,
{
/// Gets the length of the repeated field.
#[inline]
pub fn len(&self) -> usize {
T::repeated_len(*self)
}
/// Returns true if the repeated field has no values.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
@ -90,6 +94,7 @@ where
/// Gets the value at `index`.
///
/// Returns `None` if `index > len`.
#[inline]
pub fn get(self, index: usize) -> Option<View<'msg, T>> {
if index >= self.len() {
return None;
@ -102,6 +107,7 @@ where
///
/// # Safety
/// Undefined behavior if `index >= len`
#[inline]
pub unsafe fn get_unchecked(self, index: usize) -> View<'msg, T> {
// SAFETY: in-bounds as promised
unsafe { T::repeated_get_unchecked(self, index) }
@ -120,11 +126,13 @@ impl<'msg, T: ?Sized> RepeatedMut<'msg, T> {
/// - There must be no aliasing references or mutations on the same
/// underlying object.
#[doc(hidden)]
#[inline]
pub unsafe fn from_inner(_private: Private, inner: InnerRepeatedMut<'msg>) -> Self {
Self { inner, _phantom: PhantomData }
}
#[doc(hidden)]
#[inline]
pub fn as_raw(&mut self, _private: Private) -> RawRepeatedField {
self.inner.raw
}
@ -135,11 +143,13 @@ where
T: ProxiedInRepeated + ?Sized + 'msg,
{
/// Gets the length of the repeated field.
#[inline]
pub fn len(&self) -> usize {
self.as_view().len()
}
/// Returns true if the repeated field has no values.
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
@ -147,6 +157,7 @@ where
/// Gets the value at `index`.
///
/// Returns `None` if `index > len`.
#[inline]
pub fn get(&self, index: usize) -> Option<View<T>> {
self.as_view().get(index)
}
@ -155,12 +166,14 @@ where
///
/// # Safety
/// Undefined behavior if `index >= len`
#[inline]
pub unsafe fn get_unchecked(&self, index: usize) -> View<T> {
// SAFETY: in-bounds as promised
unsafe { self.as_view().get_unchecked(index) }
}
/// Appends `val` to the end of the repeated field.
#[inline]
pub fn push(&mut self, val: View<T>) {
// TODO: b/320936046 - Use SettableValue instead of View for added ergonomics.
T::repeated_push(self.as_mut(), val);
@ -170,6 +183,7 @@ where
///
/// # Panics
/// Panics if `index >= len`
#[inline]
pub fn set(&mut self, index: usize, val: View<T>) {
let len = self.len();
if index >= len {
@ -184,6 +198,7 @@ where
///
/// # Safety
/// Undefined behavior if `index >= len`
#[inline]
pub unsafe fn set_unchecked(&mut self, index: usize, val: View<T>) {
// TODO: b/320936046 - Use SettableValue instead of View for added ergonomics.
// SAFETY: `index` is in-bounds as promised by the caller.
@ -376,10 +391,12 @@ where
{
type Proxied = Repeated<T>;
#[inline]
fn as_view(&self) -> View<'_, Self::Proxied> {
*self
}
#[inline]
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied>
where
'msg: 'shorter,
@ -394,10 +411,12 @@ where
{
type Proxied = Repeated<T>;
#[inline]
fn as_view(&self) -> View<'_, Self::Proxied> {
RepeatedView { raw: self.inner.raw, _phantom: PhantomData }
}
#[inline]
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied>
where
'msg: 'shorter,
@ -410,10 +429,12 @@ impl<'msg, T> MutProxy<'msg> for RepeatedMut<'msg, T>
where
T: ProxiedInRepeated + ?Sized + 'msg,
{
#[inline]
fn as_mut(&mut self) -> Mut<'_, Self::Proxied> {
RepeatedMut { inner: self.inner, _phantom: PhantomData }
}
#[inline]
fn into_mut<'shorter>(self) -> Mut<'shorter, Self::Proxied>
where
'msg: 'shorter,
@ -428,6 +449,7 @@ where
{
type Item = View<'msg, T>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
let val = self.view.get(self.current_index);
if val.is_some() {

@ -182,6 +182,7 @@ impl<'msg> InnerRepeatedMut<'msg> {
macro_rules! impl_repeated_base {
($t:ty, $elem_t:ty, $ufield:ident, $upb_tag:expr) => {
#[allow(dead_code)]
#[inline]
fn repeated_new(_: Private) -> Repeated<$t> {
let arena = Arena::new();
Repeated::from_inner(InnerRepeated {
@ -193,9 +194,11 @@ macro_rules! impl_repeated_base {
unsafe fn repeated_free(_: Private, _f: &mut Repeated<$t>) {
// No-op: the memory will be dropped by the arena.
}
#[inline]
fn repeated_len(f: View<Repeated<$t>>) -> usize {
unsafe { upb_Array_Size(f.as_raw(Private)) }
}
#[inline]
fn repeated_push(mut f: Mut<Repeated<$t>>, v: View<$t>) {
let arena = f.raw_arena(Private);
unsafe {
@ -206,16 +209,19 @@ macro_rules! impl_repeated_base {
));
}
}
#[inline]
fn repeated_clear(mut f: Mut<Repeated<$t>>) {
unsafe {
upb_Array_Resize(f.as_raw(Private), 0, f.raw_arena(Private));
}
}
#[inline]
unsafe fn repeated_get_unchecked(f: View<Repeated<$t>>, i: usize) -> View<$t> {
unsafe {
<$t as UpbTypeConversions>::from_message_value(upb_Array_Get(f.as_raw(Private), i))
}
}
#[inline]
unsafe fn repeated_set_unchecked(mut f: Mut<Repeated<$t>>, i: usize, v: View<$t>) {
let arena = f.raw_arena(Private);
unsafe {
@ -226,6 +232,7 @@ macro_rules! impl_repeated_base {
)
}
}
#[inline]
fn repeated_reserve(mut f: Mut<Repeated<$t>>, additional: usize) {
// SAFETY:
// - `upb_Array_Reserve` is unsafe but assumed to be sound when called on a
@ -272,6 +279,7 @@ macro_rules! impl_repeated_bytes {
unsafe impl ProxiedInRepeated for $t {
impl_repeated_base!($t, PtrAndLen, str_val, $upb_tag);
#[inline]
fn repeated_copy_from(src: View<Repeated<$t>>, mut dest: Mut<Repeated<$t>>) {
let len = src.len();
// SAFETY:

Loading…
Cancel
Save