mirror of
https://github.com/protocolbuffers/protobuf
synced 2026-08-26 02:23:14 -04:00
Trait clarity effort: introduce Singular trait, for types which are allowed as simple fields
At this moment it is a simple rename of ProxiedInRepeated, but treated as a generalization since the same class of types are usable as singular fields, in Repeated<T> and in Map<*, T> In a subsequent change, we may be able to merge the MapValue<K: MapKey> trait into Singular since it should be 1:1, its only complex to remove locally because of the generic value. PiperOrigin-RevId: 861713109
This commit is contained in:
parent
09a505f198
commit
cdbfaf118e
10 changed files with 163 additions and 147 deletions
|
|
@ -62,6 +62,7 @@ PROTOBUF_SHARED = [
|
|||
"proxied.rs",
|
||||
"repeated.rs",
|
||||
"shared.rs",
|
||||
"singular.rs",
|
||||
"string.rs",
|
||||
# go/keep-sorted end
|
||||
]
|
||||
|
|
|
|||
27
rust/cpp.rs
27
rust/cpp.rs
|
|
@ -11,8 +11,8 @@ use crate::__internal::{Enum, MatcherEq, Private, SealedInternal};
|
|||
use crate::{
|
||||
AsMut, AsView, Clear, ClearAndParse, CopyFrom, IntoProxied, Map, MapIter, MapKey, MapMut,
|
||||
MapValue, MapView, MergeFrom, Message, MessageMutInterop, Mut, MutProxied, ParseError,
|
||||
ProtoBytes, ProtoStr, ProtoString, Proxied, ProxiedInRepeated, Repeated, RepeatedMut,
|
||||
RepeatedView, Serialize, SerializeError, TakeFrom, View,
|
||||
ProtoBytes, ProtoStr, ProtoString, Proxied, Repeated, RepeatedMut, RepeatedView, Serialize,
|
||||
SerializeError, Singular, TakeFrom, View,
|
||||
};
|
||||
use core::fmt::Debug;
|
||||
use paste::paste;
|
||||
|
|
@ -549,7 +549,7 @@ impl CppTypeConversions for ProtoBytes {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> ProxiedInRepeated for T
|
||||
unsafe impl<T> Singular for T
|
||||
where
|
||||
Self: MutProxied + CppGetRawMessage + Message,
|
||||
for<'a> View<'a, Self>:
|
||||
|
|
@ -695,7 +695,7 @@ macro_rules! impl_repeated_primitives {
|
|||
additional: usize);
|
||||
}
|
||||
|
||||
unsafe impl ProxiedInRepeated for $t {
|
||||
unsafe impl Singular for $t {
|
||||
#[allow(dead_code)]
|
||||
#[inline]
|
||||
fn repeated_new(_: Private) -> Repeated<$t> {
|
||||
|
|
@ -786,7 +786,7 @@ unsafe extern "C" {
|
|||
}
|
||||
|
||||
/// Cast a `RepeatedView<SomeEnum>` to `RepeatedView<c_int>`.
|
||||
pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
|
||||
pub fn cast_enum_repeated_view<E: Enum + Singular>(
|
||||
repeated: RepeatedView<E>,
|
||||
) -> RepeatedView<c_int> {
|
||||
// SAFETY: the implementer of `Enum` has promised that this
|
||||
|
|
@ -798,7 +798,7 @@ pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
|
|||
///
|
||||
/// Writing an unknown value is sound because all enums
|
||||
/// are representationally open.
|
||||
pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
|
||||
pub fn cast_enum_repeated_mut<E: Enum + Singular>(
|
||||
mut repeated: RepeatedMut<E>,
|
||||
) -> RepeatedMut<c_int> {
|
||||
// SAFETY: the implementer of `Enum` has promised that this
|
||||
|
|
@ -813,15 +813,12 @@ pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
|
|||
|
||||
/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<c_int>` and call
|
||||
/// repeated_reserve.
|
||||
pub fn reserve_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
|
||||
repeated: RepeatedMut<E>,
|
||||
additional: usize,
|
||||
) {
|
||||
pub fn reserve_enum_repeated_mut<E: Enum + Singular>(repeated: RepeatedMut<E>, additional: usize) {
|
||||
let int_repeated = cast_enum_repeated_mut(repeated);
|
||||
ProxiedInRepeated::repeated_reserve(Private, int_repeated, additional);
|
||||
Singular::repeated_reserve(Private, int_repeated, additional);
|
||||
}
|
||||
|
||||
pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
|
||||
pub fn new_enum_repeated<E: Enum + Singular>() -> Repeated<E> {
|
||||
let int_repeated = Repeated::<c_int>::new();
|
||||
let raw = int_repeated.inner.raw();
|
||||
std::mem::forget(int_repeated);
|
||||
|
|
@ -833,11 +830,11 @@ pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
|
|||
/// # Safety
|
||||
/// - The passed in `&mut Repeated<E>` must not be used after this function is
|
||||
/// called.
|
||||
pub unsafe fn free_enum_repeated<E: Enum + ProxiedInRepeated>(repeated: &mut Repeated<E>) {
|
||||
pub unsafe fn free_enum_repeated<E: Enum + Singular>(repeated: &mut Repeated<E>) {
|
||||
unsafe {
|
||||
let mut int_r: Repeated<c_int> =
|
||||
Repeated::from_inner(Private, InnerRepeated::from_raw(repeated.inner.raw()));
|
||||
ProxiedInRepeated::repeated_free(Private, &mut int_r);
|
||||
Singular::repeated_free(Private, &mut int_r);
|
||||
std::mem::forget(int_r);
|
||||
}
|
||||
}
|
||||
|
|
@ -1273,7 +1270,7 @@ generate_map_key_impl!(
|
|||
impl<Key, Value> MapValue<Key> for Value
|
||||
where
|
||||
Key: MapKey + FfiMapKey + CppMapTypeConversions,
|
||||
Value: Proxied + CppMapTypeConversions,
|
||||
Value: Singular + CppMapTypeConversions,
|
||||
{
|
||||
fn map_new(_private: Private) -> Map<Key, Self> {
|
||||
unsafe {
|
||||
|
|
|
|||
|
|
@ -14,7 +14,8 @@ pub use paste::paste;
|
|||
|
||||
use crate::map;
|
||||
pub use crate::r#enum::Enum;
|
||||
use crate::repeated;
|
||||
use crate::repeated::RepeatedView;
|
||||
use crate::singular::Singular;
|
||||
use crate::MapKey;
|
||||
pub use crate::ProtoStr;
|
||||
pub use std::fmt::Debug;
|
||||
|
|
@ -47,10 +48,7 @@ pub trait MatcherEq: SealedInternal + Debug {
|
|||
}
|
||||
|
||||
/// Used by the proto! macro to get a default value for a repeated field.
|
||||
pub fn get_repeated_default_value<T: repeated::ProxiedInRepeated + Default>(
|
||||
_: Private,
|
||||
_: repeated::RepeatedView<'_, T>,
|
||||
) -> T {
|
||||
pub fn get_repeated_default_value<T: Singular + Default>(_: Private, _: RepeatedView<'_, T>) -> T {
|
||||
Default::default()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,8 @@
|
|||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
use crate::{
|
||||
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View,
|
||||
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, Singular,
|
||||
View,
|
||||
__internal::runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
|
||||
__internal::{Private, SealedInternal},
|
||||
};
|
||||
|
|
@ -84,9 +85,11 @@ impl<K: MapKey, V: MapValue<K>> Drop for Map<K, V> {
|
|||
|
||||
/// A trait implemented by types which are allowed as keys in maps.
|
||||
/// This is all types for fields except for repeated, maps, bytes, messages and enums.
|
||||
pub trait MapKey: Proxied {}
|
||||
pub trait MapKey: Proxied + SealedInternal {}
|
||||
|
||||
pub trait MapValue<K: MapKey>: Proxied {
|
||||
/// A trait implemented by types which are allowed as values in maps, which is all Singular types.
|
||||
/// This trait is distinct from `Singular` only because of the generic `K: MapKey`.
|
||||
pub trait MapValue<K: MapKey>: Singular + SealedInternal {
|
||||
#[doc(hidden)]
|
||||
fn map_new(_private: Private) -> Map<K, Self>;
|
||||
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ macro_rules! impl_singular_primitives {
|
|||
}
|
||||
}
|
||||
|
||||
// ProxiedInRepeated is implemented in {cpp,upb}.rs
|
||||
// Singular is implemented in {cpp,upb}.rs
|
||||
)*
|
||||
}
|
||||
}
|
||||
|
|
|
|||
145
rust/repeated.rs
145
rust/repeated.rs
|
|
@ -15,7 +15,8 @@ use std::iter::FusedIterator;
|
|||
use std::marker::PhantomData;
|
||||
|
||||
use crate::{
|
||||
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View,
|
||||
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, Singular,
|
||||
View,
|
||||
__internal::runtime::{InnerRepeated, InnerRepeatedMut, RawRepeatedField},
|
||||
__internal::{Private, SealedInternal},
|
||||
};
|
||||
|
|
@ -77,7 +78,7 @@ impl<'msg, T> RepeatedView<'msg, T> {
|
|||
|
||||
impl<'msg, T> RepeatedView<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
/// Gets the length of the repeated field.
|
||||
#[inline]
|
||||
|
|
@ -140,7 +141,7 @@ impl<'msg, T> RepeatedMut<'msg, T> {
|
|||
|
||||
impl<'msg, T> RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
/// Gets the length of the repeated field.
|
||||
#[inline]
|
||||
|
|
@ -248,7 +249,7 @@ where
|
|||
|
||||
impl<T> Repeated<T>
|
||||
where
|
||||
T: ProxiedInRepeated,
|
||||
T: Singular,
|
||||
{
|
||||
pub fn as_view(&self) -> View<'_, Repeated<T>> {
|
||||
RepeatedView { raw: self.inner.raw(), _phantom: PhantomData }
|
||||
|
|
@ -262,7 +263,7 @@ where
|
|||
|
||||
impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedView<'msg, T>
|
||||
where
|
||||
T: 'msg + ProxiedInRepeated,
|
||||
T: 'msg + Singular,
|
||||
{
|
||||
fn into_proxied(self, _private: Private) -> Repeated<T> {
|
||||
let mut repeated: Repeated<T> = Repeated::new();
|
||||
|
|
@ -273,7 +274,7 @@ where
|
|||
|
||||
impl<'msg, T> IntoProxied<Repeated<T>> for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: 'msg + ProxiedInRepeated,
|
||||
T: 'msg + Singular,
|
||||
{
|
||||
fn into_proxied(self, _private: Private) -> Repeated<T> {
|
||||
IntoProxied::into_proxied(self.as_view(), _private)
|
||||
|
|
@ -283,7 +284,7 @@ where
|
|||
impl<'msg, T, I, U> IntoProxied<Repeated<T>> for I
|
||||
where
|
||||
I: Iterator<Item = U>,
|
||||
T: 'msg + ProxiedInRepeated,
|
||||
T: 'msg + Singular,
|
||||
U: IntoProxied<T>,
|
||||
{
|
||||
fn into_proxied(self, _private: Private) -> Repeated<T> {
|
||||
|
|
@ -293,84 +294,6 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
/// Types that can appear in a `Repeated<T>`.
|
||||
///
|
||||
/// This trait is implemented by generated code to communicate how the proxied
|
||||
/// type can be manipulated for a repeated field.
|
||||
///
|
||||
/// Scalars and messages implement `ProxiedInRepeated`.
|
||||
///
|
||||
/// # Safety
|
||||
/// - It must be sound to call `*_unchecked*(x)` with an `index` less than
|
||||
/// `repeated_len(x)`.
|
||||
pub unsafe trait ProxiedInRepeated: Proxied + SealedInternal {
|
||||
/// Constructs a new owned `Repeated` field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_new(_private: Private) -> Repeated<Self>;
|
||||
|
||||
/// Frees the repeated field in-place, for use in `Drop`.
|
||||
///
|
||||
/// # Safety
|
||||
/// - After `repeated_free`, no other methods on the input are safe to call.
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_free(_private: Private, _repeated: &mut Repeated<Self>);
|
||||
|
||||
/// Gets the length of the repeated field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_len(_private: Private, repeated: View<Repeated<Self>>) -> usize;
|
||||
|
||||
/// Appends a new element to the end of the repeated field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_push(_private: Private, repeated: Mut<Repeated<Self>>, val: impl IntoProxied<Self>);
|
||||
|
||||
/// Clears the repeated field of elements.
|
||||
#[doc(hidden)]
|
||||
fn repeated_clear(_private: Private, repeated: Mut<Repeated<Self>>);
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_get_unchecked(
|
||||
_private: Private,
|
||||
repeated: View<Repeated<Self>>,
|
||||
index: usize,
|
||||
) -> View<Self>;
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[allow(unused_variables)]
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_get_mut_unchecked(
|
||||
_private: Private,
|
||||
repeated: Mut<Repeated<Self>>,
|
||||
index: usize,
|
||||
) -> Mut<Self>
|
||||
where
|
||||
Self: Message,
|
||||
{
|
||||
panic!("repeated_get_mut_unchecked is only implemented for messages");
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_set_unchecked(
|
||||
_private: Private,
|
||||
repeated: Mut<Repeated<Self>>,
|
||||
index: usize,
|
||||
val: impl IntoProxied<Self>,
|
||||
);
|
||||
|
||||
/// Copies the values in the `src` repeated field into `dest`.
|
||||
#[doc(hidden)]
|
||||
fn repeated_copy_from(_private: Private, src: View<Repeated<Self>>, dest: Mut<Repeated<Self>>);
|
||||
|
||||
/// Ensures that the repeated field has enough space allocated to insert at
|
||||
/// least `additional` values without an allocation.
|
||||
#[doc(hidden)]
|
||||
fn repeated_reserve(_private: Private, repeated: Mut<Repeated<Self>>, additional: usize);
|
||||
}
|
||||
|
||||
/// An iterator over the values inside of a [`View<Repeated<T>>`](RepeatedView).
|
||||
#[derive(Clone)]
|
||||
pub struct RepeatedIter<'msg, T> {
|
||||
|
|
@ -391,19 +314,19 @@ impl<'msg, T> Debug for RepeatedIter<'msg, T> {
|
|||
///
|
||||
/// Users will generally write [`View<Repeated<T>>`](RepeatedView) or
|
||||
/// [`Mut<Repeated<T>>`](RepeatedMut) to access the repeated elements
|
||||
pub struct Repeated<T: ProxiedInRepeated> {
|
||||
pub struct Repeated<T: Singular> {
|
||||
pub(crate) inner: InnerRepeated,
|
||||
_phantom: PhantomData<T>,
|
||||
}
|
||||
|
||||
// SAFETY: `Repeated` is Sync because it does not implement interior mutability.
|
||||
unsafe impl<T: ProxiedInRepeated> Sync for Repeated<T> {}
|
||||
unsafe impl<T: Singular> Sync for Repeated<T> {}
|
||||
|
||||
// SAFETY: `Repeated` is Send because it's not bound to a specific thread e.g.
|
||||
// it does not use thread-local data or similar.
|
||||
unsafe impl<T: ProxiedInRepeated> Send for Repeated<T> {}
|
||||
unsafe impl<T: Singular> Send for Repeated<T> {}
|
||||
|
||||
impl<T: ProxiedInRepeated> Repeated<T> {
|
||||
impl<T: Singular> Repeated<T> {
|
||||
pub fn new() -> Self {
|
||||
T::repeated_new(Private)
|
||||
}
|
||||
|
|
@ -417,13 +340,13 @@ impl<T: ProxiedInRepeated> Repeated<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: ProxiedInRepeated> Default for Repeated<T> {
|
||||
impl<T: Singular> Default for Repeated<T> {
|
||||
fn default() -> Self {
|
||||
Repeated::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ProxiedInRepeated> Drop for Repeated<T> {
|
||||
impl<T: Singular> Drop for Repeated<T> {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: only called once
|
||||
unsafe { T::repeated_free(Private, self) }
|
||||
|
|
@ -432,7 +355,7 @@ impl<T: ProxiedInRepeated> Drop for Repeated<T> {
|
|||
|
||||
impl<T> Proxied for Repeated<T>
|
||||
where
|
||||
T: ProxiedInRepeated,
|
||||
T: Singular,
|
||||
{
|
||||
type View<'msg>
|
||||
= RepeatedView<'msg, T>
|
||||
|
|
@ -440,11 +363,11 @@ where
|
|||
Repeated<T>: 'msg;
|
||||
}
|
||||
|
||||
impl<T> SealedInternal for Repeated<T> where T: ProxiedInRepeated {}
|
||||
impl<T> SealedInternal for Repeated<T> where T: Singular {}
|
||||
|
||||
impl<T> AsView for Repeated<T>
|
||||
where
|
||||
T: ProxiedInRepeated,
|
||||
T: Singular,
|
||||
{
|
||||
type Proxied = Self;
|
||||
|
||||
|
|
@ -455,7 +378,7 @@ where
|
|||
|
||||
impl<T> MutProxied for Repeated<T>
|
||||
where
|
||||
T: ProxiedInRepeated,
|
||||
T: Singular,
|
||||
{
|
||||
type Mut<'msg>
|
||||
= RepeatedMut<'msg, T>
|
||||
|
|
@ -465,7 +388,7 @@ where
|
|||
|
||||
impl<T> AsMut for Repeated<T>
|
||||
where
|
||||
T: ProxiedInRepeated,
|
||||
T: Singular,
|
||||
{
|
||||
type MutProxied = Self;
|
||||
|
||||
|
|
@ -474,11 +397,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'msg, T> SealedInternal for RepeatedView<'msg, T> where T: ProxiedInRepeated + 'msg {}
|
||||
impl<'msg, T> SealedInternal for RepeatedView<'msg, T> where T: Singular + 'msg {}
|
||||
|
||||
impl<'msg, T> AsView for RepeatedView<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type Proxied = Repeated<T>;
|
||||
|
||||
|
|
@ -490,7 +413,7 @@ where
|
|||
|
||||
impl<'msg, T> IntoView<'msg> for RepeatedView<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
#[inline]
|
||||
fn into_view<'shorter>(self) -> View<'shorter, Self::Proxied>
|
||||
|
|
@ -501,11 +424,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'msg, T> SealedInternal for RepeatedMut<'msg, T> where T: ProxiedInRepeated + 'msg {}
|
||||
impl<'msg, T> SealedInternal for RepeatedMut<'msg, T> where T: Singular + 'msg {}
|
||||
|
||||
impl<'msg, T> AsView for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type Proxied = Repeated<T>;
|
||||
|
||||
|
|
@ -517,7 +440,7 @@ where
|
|||
|
||||
impl<'msg, T> IntoView<'msg> for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
#[inline]
|
||||
fn into_view<'shorter>(self) -> RepeatedView<'shorter, T>
|
||||
|
|
@ -530,7 +453,7 @@ where
|
|||
|
||||
impl<'msg, T> AsMut for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type MutProxied = Repeated<T>;
|
||||
|
||||
|
|
@ -542,7 +465,7 @@ where
|
|||
|
||||
impl<'msg, T> IntoMut<'msg> for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
#[inline]
|
||||
fn into_mut<'shorter>(self) -> RepeatedMut<'shorter, T>
|
||||
|
|
@ -555,7 +478,7 @@ where
|
|||
|
||||
impl<'msg, T> iter::Iterator for RepeatedIter<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type Item = View<'msg, T>;
|
||||
|
||||
|
|
@ -574,18 +497,18 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
impl<'msg, T: ProxiedInRepeated> ExactSizeIterator for RepeatedIter<'msg, T> {
|
||||
impl<'msg, T: Singular> ExactSizeIterator for RepeatedIter<'msg, T> {
|
||||
fn len(&self) -> usize {
|
||||
self.view.len() - self.current_index
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: impl DoubleEndedIterator
|
||||
impl<'msg, T: ProxiedInRepeated> FusedIterator for RepeatedIter<'msg, T> {}
|
||||
impl<'msg, T: Singular> FusedIterator for RepeatedIter<'msg, T> {}
|
||||
|
||||
impl<'msg, T> iter::IntoIterator for RepeatedView<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type Item = View<'msg, T>;
|
||||
type IntoIter = RepeatedIter<'msg, T>;
|
||||
|
|
@ -597,7 +520,7 @@ where
|
|||
|
||||
impl<'msg, T> iter::IntoIterator for &'_ RepeatedView<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'msg,
|
||||
T: Singular + 'msg,
|
||||
{
|
||||
type Item = View<'msg, T>;
|
||||
type IntoIter = RepeatedIter<'msg, T>;
|
||||
|
|
@ -609,7 +532,7 @@ where
|
|||
|
||||
impl<'borrow, T> iter::IntoIterator for &'borrow RepeatedMut<'_, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'borrow,
|
||||
T: Singular + 'borrow,
|
||||
{
|
||||
type Item = View<'borrow, T>;
|
||||
type IntoIter = RepeatedIter<'borrow, T>;
|
||||
|
|
@ -621,7 +544,7 @@ where
|
|||
|
||||
impl<'msg, 'view, T, ViewT> Extend<ViewT> for RepeatedMut<'msg, T>
|
||||
where
|
||||
T: ProxiedInRepeated + 'view,
|
||||
T: Singular + 'view,
|
||||
ViewT: IntoProxied<T>,
|
||||
{
|
||||
fn extend<I: IntoIterator<Item = ViewT>>(&mut self, iter: I) {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ pub use crate::codegen_traits::{
|
|||
pub use crate::cord::{ProtoBytesCow, ProtoStringCow};
|
||||
pub use crate::map::{Map, MapIter, MapKey, MapMut, MapValue, MapView};
|
||||
|
||||
// TODO: Remove this alias once we have confirmed theres no incoming references.
|
||||
// TODO: Remove this alias once we have confirmed there are no incoming references.
|
||||
pub use MapValue as ProxiedInMapValue;
|
||||
|
||||
pub use crate::optional::Optional;
|
||||
|
|
@ -35,10 +35,14 @@ pub use crate::proxied::{
|
|||
AsMut, AsView, IntoMut, IntoProxied, IntoView, Mut, MutProxied, Proxied, View,
|
||||
};
|
||||
pub use crate::r#enum::{Enum, UnknownEnumValue};
|
||||
pub use crate::repeated::{ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView};
|
||||
pub use crate::repeated::{Repeated, RepeatedIter, RepeatedMut, RepeatedView};
|
||||
pub use crate::singular::Singular;
|
||||
pub use crate::string::{ProtoBytes, ProtoStr, ProtoString, Utf8Error};
|
||||
pub use protobuf_macros::proto_proc as proto;
|
||||
|
||||
// TODO: Remove this alias once we have confirmed there are no incoming references.
|
||||
pub use Singular as ProxiedInRepeated;
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
/// The `__internal` module is for necessary encapsulation breaks between
|
||||
|
|
@ -68,6 +72,7 @@ mod optional;
|
|||
mod primitive;
|
||||
mod proxied;
|
||||
mod repeated;
|
||||
mod singular;
|
||||
mod string;
|
||||
|
||||
#[cfg(not(bzl))]
|
||||
|
|
|
|||
93
rust/singular.rs
Normal file
93
rust/singular.rs
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
// Protocol Buffers - Google's data interchange format
|
||||
// Copyright 2026 Google LLC. All rights reserved.
|
||||
//
|
||||
// Use of this source code is governed by a BSD-style
|
||||
// license that can be found in the LICENSE file or at
|
||||
// https://developers.google.com/open-source/licenses/bsd
|
||||
|
||||
use crate::{
|
||||
IntoProxied, Message, Mut, Proxied, Repeated, View,
|
||||
__internal::{Private, SealedInternal},
|
||||
};
|
||||
|
||||
/// Singular types are types which are allowed as a simple field, or in a repeated, or as a map
|
||||
/// value.
|
||||
///
|
||||
/// In typical Protobuf terminology, 'singular' refers to a property of individual field (namely
|
||||
/// that it is not a repeated or map field), but in this case this Singular trait is implemented
|
||||
/// for any type which is usable in that position, which is also the same types usable as a repeated
|
||||
/// or map value.
|
||||
///
|
||||
/// Note that a subset of Singular types are permitted as Map keys: messages, bytes and enums are
|
||||
/// not allowed in that position.
|
||||
///
|
||||
/// # Safety
|
||||
/// - It must be sound to call `*_unchecked*(x)` with an `index` less than
|
||||
/// `repeated_len(x)`.
|
||||
pub unsafe trait Singular: Proxied + SealedInternal {
|
||||
/// Constructs a new owned `Repeated` field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_new(_private: Private) -> Repeated<Self>;
|
||||
|
||||
/// Frees the repeated field in-place, for use in `Drop`.
|
||||
///
|
||||
/// # Safety
|
||||
/// - After `repeated_free`, no other methods on the input are safe to call.
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_free(_private: Private, _repeated: &mut Repeated<Self>);
|
||||
|
||||
/// Gets the length of the repeated field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_len(_private: Private, repeated: View<Repeated<Self>>) -> usize;
|
||||
|
||||
/// Appends a new element to the end of the repeated field.
|
||||
#[doc(hidden)]
|
||||
fn repeated_push(_private: Private, repeated: Mut<Repeated<Self>>, val: impl IntoProxied<Self>);
|
||||
|
||||
/// Clears the repeated field of elements.
|
||||
#[doc(hidden)]
|
||||
fn repeated_clear(_private: Private, repeated: Mut<Repeated<Self>>);
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_get_unchecked(
|
||||
_private: Private,
|
||||
repeated: View<Repeated<Self>>,
|
||||
index: usize,
|
||||
) -> View<Self>;
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[allow(unused_variables)]
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_get_mut_unchecked(
|
||||
_private: Private,
|
||||
repeated: Mut<Repeated<Self>>,
|
||||
index: usize,
|
||||
) -> Mut<Self>
|
||||
where
|
||||
Self: Message,
|
||||
{
|
||||
panic!("repeated_get_mut_unchecked is only implemented for messages");
|
||||
}
|
||||
|
||||
/// # Safety
|
||||
/// `index` must be less than `Self::repeated_len(repeated)`
|
||||
#[doc(hidden)]
|
||||
unsafe fn repeated_set_unchecked(
|
||||
_private: Private,
|
||||
repeated: Mut<Repeated<Self>>,
|
||||
index: usize,
|
||||
val: impl IntoProxied<Self>,
|
||||
);
|
||||
|
||||
/// Copies the values in the `src` repeated field into `dest`.
|
||||
#[doc(hidden)]
|
||||
fn repeated_copy_from(_private: Private, src: View<Repeated<Self>>, dest: Mut<Repeated<Self>>);
|
||||
|
||||
/// Ensures that the repeated field has enough space allocated to insert at
|
||||
/// least `additional` values without an allocation.
|
||||
#[doc(hidden)]
|
||||
fn repeated_reserve(_private: Private, repeated: Mut<Repeated<Self>>, additional: usize);
|
||||
}
|
||||
14
rust/upb.rs
14
rust/upb.rs
|
|
@ -12,8 +12,8 @@ use crate::{
|
|||
AsMut, AsView, Clear, ClearAndParse, CopyFrom, IntoProxied, Map, MapIter, MapKey, MapMut,
|
||||
MapValue, MapView, MergeFrom, Message, MessageMut, MessageMutInterop, MessageView,
|
||||
MessageViewInterop, Mut, OwnedMessageInterop, ParseError, ProtoBytes, ProtoStr, ProtoString,
|
||||
Proxied, ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, Serialize, SerializeError,
|
||||
TakeFrom, View,
|
||||
Proxied, Repeated, RepeatedMut, RepeatedView, Serialize, SerializeError, Singular, TakeFrom,
|
||||
View,
|
||||
};
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
|
|
@ -393,7 +393,7 @@ impl<'msg> InnerRepeatedMut<'msg> {
|
|||
}
|
||||
}
|
||||
|
||||
unsafe impl<T> ProxiedInRepeated for T
|
||||
unsafe impl<T> Singular for T
|
||||
where
|
||||
T: EntityType + UpbTypeConversions<T::Tag>,
|
||||
{
|
||||
|
|
@ -534,7 +534,7 @@ impl<'msg, T> RepeatedMut<'msg, T> {
|
|||
}
|
||||
|
||||
/// Returns a static empty RepeatedView.
|
||||
pub fn empty_array<T: ProxiedInRepeated>() -> RepeatedView<'static, T> {
|
||||
pub fn empty_array<T: Singular>() -> RepeatedView<'static, T> {
|
||||
// TODO: Consider creating a static empty array in C.
|
||||
|
||||
// Use `i32` for a shared empty repeated for all repeated types in the program.
|
||||
|
|
@ -1362,11 +1362,7 @@ pub unsafe fn message_set_bytes_field<'msg, P: Message + AssociatedMiniTable>(
|
|||
|
||||
/// # Safety
|
||||
/// - The field at `index` must be a repeated field of `T`.
|
||||
pub unsafe fn message_set_repeated_field<
|
||||
'msg,
|
||||
P: Message + AssociatedMiniTable,
|
||||
T: ProxiedInRepeated,
|
||||
>(
|
||||
pub unsafe fn message_set_repeated_field<'msg, P: Message + AssociatedMiniTable, T: Singular>(
|
||||
parent: MessageMutInner<'msg, P>,
|
||||
index: u32,
|
||||
val: impl IntoProxied<Repeated<T>>,
|
||||
|
|
|
|||
|
|
@ -311,7 +311,7 @@ void GenerateEnumDefinition(Context& ctx, const EnumDescriptor& desc,
|
|||
{"name", name},
|
||||
},
|
||||
R"rs(
|
||||
unsafe impl $pb$::ProxiedInRepeated for $name$ {
|
||||
unsafe impl $pb$::Singular for $name$ {
|
||||
fn repeated_new(_private: $pbi$::Private) -> $pb$::Repeated<Self> {
|
||||
$pbr$::new_enum_repeated()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue