Skip to content

Commit

Permalink
Use visitation to implement allocation/initialization/deallocation of…
Browse files Browse the repository at this point in the history
… nodes in generic code.

This simplifies Rust bindings, and the table-driven parser.

PiperOrigin-RevId: 700702876
  • Loading branch information
protobuf-github-bot authored and copybara-github committed Nov 27, 2024
1 parent f66e49c commit 7588e51
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 302 deletions.
40 changes: 12 additions & 28 deletions rust/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,21 +1019,11 @@ where

unsafe fn insert(m: RawMap, key: View<'_, Self>, value: MapValue) -> bool;

unsafe fn get(
m: RawMap,
prototype: MapValue,
key: View<'_, Self>,
value: *mut MapValue,
) -> bool;

unsafe fn iter_get(
iter: &mut UntypedMapIterator,
prototype: MapValue,
key: *mut Self::FfiKey,
value: *mut MapValue,
);
unsafe fn get(m: RawMap, key: View<'_, Self>, value: *mut MapValue) -> bool;

unsafe fn iter_get(iter: &mut UntypedMapIterator, key: *mut Self::FfiKey, value: *mut MapValue);

unsafe fn remove(m: RawMap, prototype: MapValue, key: View<'_, Self>) -> bool;
unsafe fn remove(m: RawMap, key: View<'_, Self>) -> bool;
}

macro_rules! generate_map_key_impl {
Expand All @@ -1060,26 +1050,24 @@ macro_rules! generate_map_key_impl {
#[inline]
unsafe fn get(
m: RawMap,
prototype: MapValue,
key: View<'_, Self>,
value: *mut MapValue,
) -> bool {
unsafe { [< proto2_rust_map_get_ $key >](m, prototype, $to_ffi(key), value) }
unsafe { [< proto2_rust_map_get_ $key >](m, $to_ffi(key), value) }
}

#[inline]
unsafe fn iter_get(
iter: &mut UntypedMapIterator,
prototype: MapValue,
key: *mut Self::FfiKey,
value: *mut MapValue,
) {
unsafe { [< proto2_rust_map_iter_get_ $key >](iter, prototype, key, value) }
unsafe { [< proto2_rust_map_iter_get_ $key >](iter, key, value) }
}

#[inline]
unsafe fn remove(m: RawMap, prototype: MapValue, key: View<'_, Self>) -> bool {
unsafe { [< proto2_rust_map_remove_ $key >](m, prototype, $to_ffi(key)) }
unsafe fn remove(m: RawMap, key: View<'_, Self>) -> bool {
unsafe { [< proto2_rust_map_remove_ $key >](m, $to_ffi(key)) }
}
}
)*
Expand Down Expand Up @@ -1136,17 +1124,15 @@ where

fn map_get<'a>(map: MapView<'a, Key, Self>, key: View<'_, Key>) -> Option<View<'a, Self>> {
let mut value = std::mem::MaybeUninit::uninit();
let found = unsafe {
Key::get(map.as_raw(Private), Self::get_prototype(), key, value.as_mut_ptr())
};
let found = unsafe { Key::get(map.as_raw(Private), key, value.as_mut_ptr()) };
if !found {
return None;
}
unsafe { Some(Self::from_map_value(value.assume_init())) }
}

fn map_remove(mut map: MapMut<Key, Self>, key: View<'_, Key>) -> bool {
unsafe { Key::remove(map.as_raw(Private), Self::get_prototype(), key) }
unsafe { Key::remove(map.as_raw(Private), key) }
}

fn map_iter(map: MapView<Key, Self>) -> MapIter<Key, Self> {
Expand All @@ -1169,7 +1155,7 @@ where
// - The thunk does not increment the iterator.
unsafe {
iter.as_raw_mut(Private).next_unchecked::<Key, Self, _, _>(
|iter, key, value| Key::iter_get(iter, Self::get_prototype(), key, value),
|iter, key, value| Key::iter_get(iter, key, value),
|ffi_key| Key::to_view(ffi_key),
|value| Self::from_map_value(value),
)
Expand All @@ -1193,17 +1179,15 @@ macro_rules! impl_map_primitives {
) -> bool;
pub fn $get_thunk(
m: RawMap,
prototype: MapValue,
key: $cpp_type,
value: *mut MapValue,
) -> bool;
pub fn $iter_get_thunk(
iter: &mut UntypedMapIterator,
prototype: MapValue,
key: *mut $cpp_type,
value: *mut MapValue,
);
pub fn $remove_thunk(m: RawMap, prototype: MapValue, key: $cpp_type) -> bool;
pub fn $remove_thunk(m: RawMap, key: $cpp_type) -> bool;
}
)*
};
Expand Down
2 changes: 1 addition & 1 deletion rust/cpp_kernel/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ cc_library(
"//src/google/protobuf",
"//src/google/protobuf:protobuf_lite",
"//src/google/protobuf/io",
"@com_google_absl//absl/functional:overload",
"@com_google_absl//absl/log:absl_check",
"@com_google_absl//absl/log:absl_log",
"@com_google_absl//absl/strings:string_view",
],
)

Expand Down
Loading

0 comments on commit 7588e51

Please sign in to comment.