Skip to content

Commit

Permalink
Stop using double underscores for our C function names and standardiz…
Browse files Browse the repository at this point in the history
…e on the 'rust_proto_' prefix.

Besides unnecessary inconsistency on our C symbols, double underscores anywhere in the name are reserved for stdlib use.  In practice its unlikely these symbols would ever hit a collision problem (maybe the prior name 'utf8_debug_string' with no prefix as having some risk), but safer to just standardize on this and have no concerns going forward.

PiperOrigin-RevId: 648709299
  • Loading branch information
protobuf-github-bot authored and Copybara-Service committed Jul 2, 2024
1 parent dba1aa1 commit a9bc366
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 107 deletions.
66 changes: 33 additions & 33 deletions rust/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,17 +261,17 @@ impl From<RustStringRawParts> for String {
}

extern "C" {
fn utf8_debug_string(msg: RawMessage) -> RustStringRawParts;
fn utf8_debug_string_lite(msg: RawMessage) -> RustStringRawParts;
fn rust_proto_utf8_debug_string(msg: RawMessage) -> RustStringRawParts;
fn rust_proto_utf8_debug_string_lite(msg: RawMessage) -> RustStringRawParts;
}

pub fn debug_string(_private: Private, msg: RawMessage, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// SAFETY:
// - `msg` is a valid protobuf message.
#[cfg(not(lite_runtime))]
let dbg_str: String = unsafe { utf8_debug_string(msg) }.into();
let dbg_str: String = unsafe { rust_proto_utf8_debug_string(msg) }.into();
#[cfg(lite_runtime)]
let dbg_str: String = unsafe { utf8_debug_string_lite(msg) }.into();
let dbg_str: String = unsafe { rust_proto_utf8_debug_string_lite(msg) }.into();

write!(f, "{dbg_str}")
}
Expand Down Expand Up @@ -485,15 +485,15 @@ macro_rules! impl_repeated_primitives {
paste!{
impl_repeated_primitives!(@impl $(
$t => [
[< __pb_rust_RepeatedField_ $t _new >],
[< __pb_rust_RepeatedField_ $t _free >],
[< __pb_rust_RepeatedField_ $t _add >],
[< __pb_rust_RepeatedField_ $t _size >],
[< __pb_rust_RepeatedField_ $t _get >],
[< __pb_rust_RepeatedField_ $t _set >],
[< __pb_rust_RepeatedField_ $t _clear >],
[< __pb_rust_RepeatedField_ $t _copy_from >],
[< __pb_rust_RepeatedField_ $t _reserve >],
[< rust_proto_RepeatedField_ $t _new >],
[< rust_proto_RepeatedField_ $t _free >],
[< rust_proto_RepeatedField_ $t _add >],
[< rust_proto_RepeatedField_ $t _size >],
[< rust_proto_RepeatedField_ $t _get >],
[< rust_proto_RepeatedField_ $t _set >],
[< rust_proto_RepeatedField_ $t _clear >],
[< rust_proto_RepeatedField_ $t _copy_from >],
[< rust_proto_RepeatedField_ $t _reserve >],
],
)*);
}
Expand Down Expand Up @@ -644,7 +644,7 @@ impl UntypedMapIterator {
// - Standard layout.
// - The size and alignment of the Rust type above.
// - With the `node_` field first.
unsafe { __rust_proto_thunk__UntypedMapIterator_increment(self) }
unsafe { rust_proto_thunk_UntypedMapIterator_increment(self) }

// SAFETY:
// - The `get` function always writes valid values to `ffi_key` and `ffi_value`
Expand All @@ -656,22 +656,22 @@ impl UntypedMapIterator {
}

extern "C" {
fn __rust_proto_thunk__UntypedMapIterator_increment(iter: &mut UntypedMapIterator);
fn rust_proto_thunk_UntypedMapIterator_increment(iter: &mut UntypedMapIterator);
}

macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
($key_t:ty, $ffi_key_t:ty, $to_ffi_key:expr, $from_ffi_key:expr, for $($t:ty, $ffi_t:ty, $to_ffi_value:expr, $from_ffi_value:expr;)*) => {
paste! { $(
extern "C" {
fn [< __rust_proto_thunk__Map_ $key_t _ $t _new >]() -> RawMap;
fn [< __rust_proto_thunk__Map_ $key_t _ $t _free >](m: RawMap);
fn [< __rust_proto_thunk__Map_ $key_t _ $t _clear >](m: RawMap);
fn [< __rust_proto_thunk__Map_ $key_t _ $t _size >](m: RawMap) -> usize;
fn [< __rust_proto_thunk__Map_ $key_t _ $t _insert >](m: RawMap, key: $ffi_key_t, value: $ffi_t) -> bool;
fn [< __rust_proto_thunk__Map_ $key_t _ $t _get >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_t) -> bool;
fn [< __rust_proto_thunk__Map_ $key_t _ $t _iter >](m: RawMap) -> UntypedMapIterator;
fn [< __rust_proto_thunk__Map_ $key_t _ $t _iter_get >](iter: &mut UntypedMapIterator, key: *mut $ffi_key_t, value: *mut $ffi_t);
fn [< __rust_proto_thunk__Map_ $key_t _ $t _remove >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_t) -> bool;
fn [< rust_proto_thunk_Map_ $key_t _ $t _new >]() -> RawMap;
fn [< rust_proto_thunk_Map_ $key_t _ $t _free >](m: RawMap);
fn [< rust_proto_thunk_Map_ $key_t _ $t _clear >](m: RawMap);
fn [< rust_proto_thunk_Map_ $key_t _ $t _size >](m: RawMap) -> usize;
fn [< rust_proto_thunk_Map_ $key_t _ $t _insert >](m: RawMap, key: $ffi_key_t, value: $ffi_t) -> bool;
fn [< rust_proto_thunk_Map_ $key_t _ $t _get >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_t) -> bool;
fn [< rust_proto_thunk_Map_ $key_t _ $t _iter >](m: RawMap) -> UntypedMapIterator;
fn [< rust_proto_thunk_Map_ $key_t _ $t _iter_get >](iter: &mut UntypedMapIterator, key: *mut $ffi_key_t, value: *mut $ffi_t);
fn [< rust_proto_thunk_Map_ $key_t _ $t _remove >](m: RawMap, key: $ffi_key_t, value: *mut $ffi_t) -> bool;
}

impl ProxiedInMapValue<$key_t> for $t {
Expand All @@ -680,7 +680,7 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
Map::from_inner(
Private,
InnerMap {
raw: [< __rust_proto_thunk__Map_ $key_t _ $t _new >](),
raw: [< rust_proto_thunk_Map_ $key_t _ $t _new >](),
}
)
}
Expand All @@ -690,28 +690,28 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
// SAFETY:
// - `map.inner.raw` is a live `RawMap`
// - This function is only called once for `map` in `Drop`.
unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _free >](map.as_mut().as_raw(Private)); }
unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _free >](map.as_mut().as_raw(Private)); }
}


fn map_clear(mut map: Mut<'_, Map<$key_t, Self>>) {
unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _clear >](map.as_raw(Private)); }
unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _clear >](map.as_raw(Private)); }
}

fn map_len(map: View<'_, Map<$key_t, Self>>) -> usize {
unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _size >](map.as_raw(Private)) }
unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _size >](map.as_raw(Private)) }
}

fn map_insert(mut map: Mut<'_, Map<$key_t, Self>>, key: View<'_, $key_t>, value: View<'_, Self>) -> bool {
let ffi_key = $to_ffi_key(key);
let ffi_value = $to_ffi_value(value);
unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _insert >](map.as_raw(Private), ffi_key, ffi_value) }
unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _insert >](map.as_raw(Private), ffi_key, ffi_value) }
}

fn map_get<'a>(map: View<'a, Map<$key_t, Self>>, key: View<'_, $key_t>) -> Option<View<'a, Self>> {
let ffi_key = $to_ffi_key(key);
let mut ffi_value = MaybeUninit::uninit();
let found = unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _get >](map.as_raw(Private), ffi_key, ffi_value.as_mut_ptr()) };
let found = unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _get >](map.as_raw(Private), ffi_key, ffi_value.as_mut_ptr()) };

if !found {
return None;
Expand All @@ -723,7 +723,7 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
fn map_remove(mut map: Mut<'_, Map<$key_t, Self>>, key: View<'_, $key_t>) -> bool {
let ffi_key = $to_ffi_key(key);
let mut ffi_value = MaybeUninit::uninit();
unsafe { [< __rust_proto_thunk__Map_ $key_t _ $t _remove >](map.as_raw(Private), ffi_key, ffi_value.as_mut_ptr()) }
unsafe { [< rust_proto_thunk_Map_ $key_t _ $t _remove >](map.as_raw(Private), ffi_key, ffi_value.as_mut_ptr()) }
}

fn map_iter(map: View<'_, Map<$key_t, Self>>) -> MapIter<'_, $key_t, Self> {
Expand All @@ -735,7 +735,7 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
unsafe {
MapIter::from_raw(
Private,
[< __rust_proto_thunk__Map_ $key_t _ $t _iter >](map.as_raw(Private))
[< rust_proto_thunk_Map_ $key_t _ $t _iter >](map.as_raw(Private))
)
}
}
Expand All @@ -750,7 +750,7 @@ macro_rules! impl_ProxiedInMapValue_for_non_generated_value_types {
unsafe {
iter.as_raw_mut(Private).next_unchecked::<$key_t, Self, _, _>(
Private,
[< __rust_proto_thunk__Map_ $key_t _ $t _iter_get >],
[< rust_proto_thunk_Map_ $key_t _ $t _iter_get >],
$from_ffi_key,
$from_ffi_value,
)
Expand Down
4 changes: 2 additions & 2 deletions rust/cpp_kernel/debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

extern "C" {

google::protobuf::rust_internal::RustStringRawParts utf8_debug_string(
google::protobuf::rust_internal::RustStringRawParts rust_proto_utf8_debug_string(
const google::protobuf::Message* msg) {
std::string text = google::protobuf::Utf8Format(*msg);
return google::protobuf::rust_internal::RustStringRawParts(text);
}

google::protobuf::rust_internal::RustStringRawParts utf8_debug_string_lite(
google::protobuf::rust_internal::RustStringRawParts rust_proto_utf8_debug_string_lite(
const google::protobuf::MessageLite* msg) {
std::string text = google::protobuf::Utf8Format(*msg);
return google::protobuf::rust_internal::RustStringRawParts(text);
Expand Down
4 changes: 2 additions & 2 deletions rust/cpp_kernel/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

extern "C" {

google::protobuf::rust_internal::RustStringRawParts utf8_debug_string(
google::protobuf::rust_internal::RustStringRawParts rust_proto_utf8_debug_string(
const google::protobuf::Message* msg);

google::protobuf::rust_internal::RustStringRawParts utf8_debug_string_lite(
google::protobuf::rust_internal::RustStringRawParts rust_proto_utf8_debug_string_lite(
const google::protobuf::MessageLite* msg);

} // extern "C"
Expand Down
2 changes: 1 addition & 1 deletion rust/cpp_kernel/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

extern "C" {

void __rust_proto_thunk__UntypedMapIterator_increment(
void rust_proto_thunk_UntypedMapIterator_increment(
google::protobuf::internal::UntypedMapIterator* iter) {
iter->PlusPlus();
}
Expand Down
18 changes: 9 additions & 9 deletions rust/cpp_kernel/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@
key_ty, rust_key_ty, ffi_key_ty, to_cpp_key, to_ffi_key, value_ty, \
rust_value_ty, ffi_value_ty, to_cpp_value, to_ffi_value) \
google::protobuf::Map<key_ty, value_ty>* \
__rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_new() { \
rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_new() { \
return new google::protobuf::Map<key_ty, value_ty>(); \
} \
void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_free( \
void rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_free( \
google::protobuf::Map<key_ty, value_ty>* m) { \
delete m; \
} \
void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_clear( \
void rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_clear( \
google::protobuf::Map<key_ty, value_ty>* m) { \
m->clear(); \
} \
size_t __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_size( \
size_t rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_size( \
const google::protobuf::Map<key_ty, value_ty>* m) { \
return m->size(); \
} \
bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_insert( \
bool rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_insert( \
google::protobuf::Map<key_ty, value_ty>* m, ffi_key_ty key, ffi_value_ty value) { \
auto iter_and_inserted = m->try_emplace(to_cpp_key, to_cpp_value); \
if (!iter_and_inserted.second) { \
iter_and_inserted.first->second = to_cpp_value; \
} \
return iter_and_inserted.second; \
} \
bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_get( \
bool rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_get( \
const google::protobuf::Map<key_ty, value_ty>* m, ffi_key_ty key, \
ffi_value_ty* value) { \
auto cpp_key = to_cpp_key; \
Expand All @@ -42,11 +42,11 @@
return true; \
} \
google::protobuf::internal::UntypedMapIterator \
__rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter( \
rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_iter( \
const google::protobuf::Map<key_ty, value_ty>* m) { \
return google::protobuf::internal::UntypedMapIterator::FromTyped(m->cbegin()); \
} \
void __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_iter_get( \
void rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_iter_get( \
const google::protobuf::internal::UntypedMapIterator* iter, ffi_key_ty* key, \
ffi_value_ty* value) { \
auto typed_iter = \
Expand All @@ -56,7 +56,7 @@
*key = to_ffi_key; \
*value = to_ffi_value; \
} \
bool __rust_proto_thunk__Map_##rust_key_ty##_##rust_value_ty##_remove( \
bool rust_proto_thunk_Map_##rust_key_ty##_##rust_value_ty##_remove( \
google::protobuf::Map<key_ty, value_ty>* m, ffi_key_ty key, ffi_value_ty* value) { \
auto cpp_key = to_cpp_key; \
auto num_removed = m->erase(cpp_key); \
Expand Down
Loading

0 comments on commit a9bc366

Please sign in to comment.