Skip to content

Commit cdbfaf1

Browse files
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
1 parent 09a505f commit cdbfaf1

10 files changed

Lines changed: 163 additions & 147 deletions

File tree

rust/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ PROTOBUF_SHARED = [
6262
"proxied.rs",
6363
"repeated.rs",
6464
"shared.rs",
65+
"singular.rs",
6566
"string.rs",
6667
# go/keep-sorted end
6768
]

rust/cpp.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use crate::__internal::{Enum, MatcherEq, Private, SealedInternal};
1111
use crate::{
1212
AsMut, AsView, Clear, ClearAndParse, CopyFrom, IntoProxied, Map, MapIter, MapKey, MapMut,
1313
MapValue, MapView, MergeFrom, Message, MessageMutInterop, Mut, MutProxied, ParseError,
14-
ProtoBytes, ProtoStr, ProtoString, Proxied, ProxiedInRepeated, Repeated, RepeatedMut,
15-
RepeatedView, Serialize, SerializeError, TakeFrom, View,
14+
ProtoBytes, ProtoStr, ProtoString, Proxied, Repeated, RepeatedMut, RepeatedView, Serialize,
15+
SerializeError, Singular, TakeFrom, View,
1616
};
1717
use core::fmt::Debug;
1818
use paste::paste;
@@ -549,7 +549,7 @@ impl CppTypeConversions for ProtoBytes {
549549
}
550550
}
551551

552-
unsafe impl<T> ProxiedInRepeated for T
552+
unsafe impl<T> Singular for T
553553
where
554554
Self: MutProxied + CppGetRawMessage + Message,
555555
for<'a> View<'a, Self>:
@@ -695,7 +695,7 @@ macro_rules! impl_repeated_primitives {
695695
additional: usize);
696696
}
697697

698-
unsafe impl ProxiedInRepeated for $t {
698+
unsafe impl Singular for $t {
699699
#[allow(dead_code)]
700700
#[inline]
701701
fn repeated_new(_: Private) -> Repeated<$t> {
@@ -786,7 +786,7 @@ unsafe extern "C" {
786786
}
787787

788788
/// Cast a `RepeatedView<SomeEnum>` to `RepeatedView<c_int>`.
789-
pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
789+
pub fn cast_enum_repeated_view<E: Enum + Singular>(
790790
repeated: RepeatedView<E>,
791791
) -> RepeatedView<c_int> {
792792
// SAFETY: the implementer of `Enum` has promised that this
@@ -798,7 +798,7 @@ pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
798798
///
799799
/// Writing an unknown value is sound because all enums
800800
/// are representationally open.
801-
pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
801+
pub fn cast_enum_repeated_mut<E: Enum + Singular>(
802802
mut repeated: RepeatedMut<E>,
803803
) -> RepeatedMut<c_int> {
804804
// SAFETY: the implementer of `Enum` has promised that this
@@ -813,15 +813,12 @@ pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
813813

814814
/// Cast a `RepeatedMut<SomeEnum>` to `RepeatedMut<c_int>` and call
815815
/// repeated_reserve.
816-
pub fn reserve_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
817-
repeated: RepeatedMut<E>,
818-
additional: usize,
819-
) {
816+
pub fn reserve_enum_repeated_mut<E: Enum + Singular>(repeated: RepeatedMut<E>, additional: usize) {
820817
let int_repeated = cast_enum_repeated_mut(repeated);
821-
ProxiedInRepeated::repeated_reserve(Private, int_repeated, additional);
818+
Singular::repeated_reserve(Private, int_repeated, additional);
822819
}
823820

824-
pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
821+
pub fn new_enum_repeated<E: Enum + Singular>() -> Repeated<E> {
825822
let int_repeated = Repeated::<c_int>::new();
826823
let raw = int_repeated.inner.raw();
827824
std::mem::forget(int_repeated);
@@ -833,11 +830,11 @@ pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
833830
/// # Safety
834831
/// - The passed in `&mut Repeated<E>` must not be used after this function is
835832
/// called.
836-
pub unsafe fn free_enum_repeated<E: Enum + ProxiedInRepeated>(repeated: &mut Repeated<E>) {
833+
pub unsafe fn free_enum_repeated<E: Enum + Singular>(repeated: &mut Repeated<E>) {
837834
unsafe {
838835
let mut int_r: Repeated<c_int> =
839836
Repeated::from_inner(Private, InnerRepeated::from_raw(repeated.inner.raw()));
840-
ProxiedInRepeated::repeated_free(Private, &mut int_r);
837+
Singular::repeated_free(Private, &mut int_r);
841838
std::mem::forget(int_r);
842839
}
843840
}
@@ -1273,7 +1270,7 @@ generate_map_key_impl!(
12731270
impl<Key, Value> MapValue<Key> for Value
12741271
where
12751272
Key: MapKey + FfiMapKey + CppMapTypeConversions,
1276-
Value: Proxied + CppMapTypeConversions,
1273+
Value: Singular + CppMapTypeConversions,
12771274
{
12781275
fn map_new(_private: Private) -> Map<Key, Self> {
12791276
unsafe {

rust/internal.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ pub use paste::paste;
1414

1515
use crate::map;
1616
pub use crate::r#enum::Enum;
17-
use crate::repeated;
17+
use crate::repeated::RepeatedView;
18+
use crate::singular::Singular;
1819
use crate::MapKey;
1920
pub use crate::ProtoStr;
2021
pub use std::fmt::Debug;
@@ -47,10 +48,7 @@ pub trait MatcherEq: SealedInternal + Debug {
4748
}
4849

4950
/// Used by the proto! macro to get a default value for a repeated field.
50-
pub fn get_repeated_default_value<T: repeated::ProxiedInRepeated + Default>(
51-
_: Private,
52-
_: repeated::RepeatedView<'_, T>,
53-
) -> T {
51+
pub fn get_repeated_default_value<T: Singular + Default>(_: Private, _: RepeatedView<'_, T>) -> T {
5452
Default::default()
5553
}
5654

rust/map.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
// https://developers.google.com/open-source/licenses/bsd
77

88
use crate::{
9-
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, View,
9+
AsMut, AsView, IntoMut, IntoProxied, IntoView, Message, Mut, MutProxied, Proxied, Singular,
10+
View,
1011
__internal::runtime::{InnerMap, InnerMapMut, RawMap, RawMapIter},
1112
__internal::{Private, SealedInternal},
1213
};
@@ -84,9 +85,11 @@ impl<K: MapKey, V: MapValue<K>> Drop for Map<K, V> {
8485

8586
/// A trait implemented by types which are allowed as keys in maps.
8687
/// This is all types for fields except for repeated, maps, bytes, messages and enums.
87-
pub trait MapKey: Proxied {}
88+
pub trait MapKey: Proxied + SealedInternal {}
8889

89-
pub trait MapValue<K: MapKey>: Proxied {
90+
/// A trait implemented by types which are allowed as values in maps, which is all Singular types.
91+
/// This trait is distinct from `Singular` only because of the generic `K: MapKey`.
92+
pub trait MapValue<K: MapKey>: Singular + SealedInternal {
9093
#[doc(hidden)]
9194
fn map_new(_private: Private) -> Map<K, Self>;
9295

rust/primitive.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ macro_rules! impl_singular_primitives {
3434
}
3535
}
3636

37-
// ProxiedInRepeated is implemented in {cpp,upb}.rs
37+
// Singular is implemented in {cpp,upb}.rs
3838
)*
3939
}
4040
}

0 commit comments

Comments
 (0)