Skip to content

Commit

Permalink
Add ProtoString/Bytes owned types
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 644999527
  • Loading branch information
hlopko authored and copybara-github committed Jun 20, 2024
1 parent 8ed10a9 commit 6b7e814
Show file tree
Hide file tree
Showing 13 changed files with 640 additions and 173 deletions.
324 changes: 245 additions & 79 deletions Cargo.bazel.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ fuzzing_py_deps_install_deps()

http_archive(
name = "rules_rust",
sha256 = "9ecd0f2144f0a24e6bc71ebcc50a1ee5128cedeceb32187004532c9710cb2334",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.29.1/rules_rust-v0.29.1.tar.gz"],
integrity = "sha256-F8U7+AC5MvMtPKGdLLnorVM84cDXKfDRgwd7/dq3rUY=",
urls = ["https://github.com/bazelbuild/rules_rust/releases/download/0.46.0/rules_rust-v0.46.0.tar.gz"],
)

load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains")
Expand Down
48 changes: 28 additions & 20 deletions rust/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

use crate::__internal::{Enum, Private};
use crate::{
Map, MapIter, Mut, ProtoStr, Proxied, ProxiedInMapValue, ProxiedInRepeated, Repeated,
RepeatedMut, RepeatedView, View,
Map, MapIter, Mut, ProtoBytes, ProtoStr, ProtoString, Proxied, ProxiedInMapValue,
ProxiedInRepeated, Repeated, RepeatedMut, RepeatedView, View,
};
use core::fmt::Debug;
use paste::paste;
Expand Down Expand Up @@ -75,6 +75,25 @@ pub type RawRepeatedField = NonNull<_opaque_pointees::RawRepeatedFieldData>;
/// A raw pointer to the underlying arena for this runtime.
pub type RawMap = NonNull<_opaque_pointees::RawMapData>;

/// Kernel-specific owned `string` and `bytes` field type.
// TODO - b/334788521: Allocate this on the C++ side (maybe as a std::string), and move the
// std::string instead of copying the string_view (which we currently do).
#[derive(Debug)]
pub struct InnerProtoString(Box<[u8]>);

impl InnerProtoString {
pub(crate) fn as_bytes(&self) -> &[u8] {
self.0.as_ref()
}
}

impl From<&[u8]> for InnerProtoString {
fn from(val: &[u8]) -> Self {
let owned_copy: Box<[u8]> = val.into();
InnerProtoString(owned_copy)
}
}

/// Represents an ABI-stable version of `NonNull<[u8]>`/`string_view` (a
/// borrowed slice of bytes) for FFI use only.
///
Expand Down Expand Up @@ -197,13 +216,6 @@ impl Deref for SerializedData {
}
}

// TODO: remove after IntoProxied has been implemented for bytes.
impl AsRef<[u8]> for SerializedData {
fn as_ref(&self) -> &[u8] {
self
}
}

impl Drop for SerializedData {
fn drop(&mut self) {
// SAFETY: `data` was allocated by the Rust global allocator with a
Expand Down Expand Up @@ -376,26 +388,22 @@ macro_rules! impl_cpp_type_conversions_for_scalars {

impl_cpp_type_conversions_for_scalars!(i32, u32, i64, u64, f32, f64, bool);

impl CppTypeConversions for ProtoStr {
impl CppTypeConversions for ProtoString {
type ElemType = PtrAndLen;

fn elem_to_view<'msg>(v: PtrAndLen) -> View<'msg, ProtoStr> {
fn elem_to_view<'msg>(v: PtrAndLen) -> View<'msg, ProtoString> {
ptrlen_to_str(v)
}
}

impl CppTypeConversions for [u8] {
impl CppTypeConversions for ProtoBytes {
type ElemType = PtrAndLen;

fn elem_to_view<'msg>(v: Self::ElemType) -> View<'msg, Self> {
ptrlen_to_bytes(v)
}
}

// This type alias is used so macros can generate valid extern "C" symbol names
// for functions working with [u8] types.
type Bytes = [u8];

macro_rules! impl_repeated_primitives {
(@impl $($t:ty => [
$new_thunk:ident,
Expand Down Expand Up @@ -492,7 +500,7 @@ macro_rules! impl_repeated_primitives {
};
}

impl_repeated_primitives!(i32, u32, i64, u64, f32, f64, bool, ProtoStr, Bytes);
impl_repeated_primitives!(i32, u32, i64, u64, f32, f64, bool, ProtoString, ProtoBytes);

/// Cast a `RepeatedView<SomeEnum>` to `RepeatedView<c_int>`.
pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
Expand Down Expand Up @@ -786,8 +794,8 @@ macro_rules! impl_ProxiedInMapValue_for_key_types {
i64, i64, identity, identity;
u64, u64, identity, identity;
bool, bool, identity, identity;
ProtoStr, PtrAndLen, str_to_ptrlen, ptrlen_to_str;
Bytes, PtrAndLen, bytes_to_ptrlen, ptrlen_to_bytes;
ProtoString, PtrAndLen, str_to_ptrlen, ptrlen_to_str;
ProtoBytes, PtrAndLen, bytes_to_ptrlen, ptrlen_to_bytes;
);
)*
}
Expand All @@ -800,7 +808,7 @@ impl_ProxiedInMapValue_for_key_types!(
i64, i64, identity, identity;
u64, u64, identity, identity;
bool, bool, identity, identity;
ProtoStr, PtrAndLen, str_to_ptrlen, ptrlen_to_str;
ProtoString, PtrAndLen, str_to_ptrlen, ptrlen_to_str;
);

#[cfg(test)]
Expand Down
8 changes: 4 additions & 4 deletions rust/cpp_kernel/cpp_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ expose_repeated_field_methods(int64_t, i64);
r->Reserve(r->size() + additional); \
}

expose_repeated_ptr_field_methods(ProtoStr);
expose_repeated_ptr_field_methods(Bytes);
expose_repeated_ptr_field_methods(ProtoString);
expose_repeated_ptr_field_methods(ProtoBytes);
#undef expose_repeated_field_methods

#undef expose_repeated_ptr_field_methods
Expand All @@ -126,11 +126,11 @@ __PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(uint64_t, u64, uint64_t,
__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(int64_t, i64, int64_t, value,
cpp_value);
__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(
std::string, Bytes, google::protobuf::rust_internal::PtrAndLen,
std::string, ProtoBytes, google::protobuf::rust_internal::PtrAndLen,
std::string(value.ptr, value.len),
google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size()));
__PB_RUST_EXPOSE_SCALAR_MAP_METHODS_FOR_VALUE_TYPE(
std::string, ProtoStr, google::protobuf::rust_internal::PtrAndLen,
std::string, ProtoString, google::protobuf::rust_internal::PtrAndLen,
std::string(value.ptr, value.len),
google::protobuf::rust_internal::PtrAndLen(cpp_value.data(), cpp_value.size()));

Expand Down
2 changes: 1 addition & 1 deletion rust/cpp_kernel/cpp_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct PtrAndLen {
value_ty, rust_value_ty, ffi_value_ty, \
to_cpp_value, to_ffi_value); \
__PB_RUST_EXPOSE_SCALAR_MAP_METHODS( \
std::string, ProtoStr, google::protobuf::rust_internal::PtrAndLen, \
std::string, ProtoString, google::protobuf::rust_internal::PtrAndLen, \
std::string(key.ptr, key.len), \
google::protobuf::rust_internal::PtrAndLen(cpp_key.data(), cpp_key.size()), \
value_ty, rust_value_ty, ffi_value_ty, to_cpp_value, to_ffi_value);
Expand Down
20 changes: 10 additions & 10 deletions rust/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use crate::ProtoStr;
use crate::{ProtoBytes, ProtoStr, ProtoString};
use googletest::prelude::*;

#[test]
Expand Down Expand Up @@ -489,7 +489,7 @@ mod tests {

#[test]
fn test_proxied_str() {
let mut map: Map<ProtoStr, ProtoStr> = Map::new();
let mut map: Map<ProtoString, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.insert("a", "b");

Expand All @@ -514,7 +514,7 @@ mod tests {

#[test]
fn test_proxied_iter() {
let mut map: Map<i32, ProtoStr> = Map::new();
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.insert(15, "fizzbuzz");
map_mut.insert(5, "buzz");
Expand Down Expand Up @@ -561,7 +561,7 @@ mod tests {

#[test]
fn test_overwrite_insert() {
let mut map: Map<i32, ProtoStr> = Map::new();
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
assert!(map_mut.insert(0, "fizz"));
// insert should return false when the key is already present
Expand All @@ -571,7 +571,7 @@ mod tests {

#[test]
fn test_extend() {
let mut map: Map<i32, ProtoStr> = Map::new();
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();

map_mut.extend([(0, ""); 0]);
Expand All @@ -588,7 +588,7 @@ mod tests {
]
);

let mut map_2: Map<i32, ProtoStr> = Map::new();
let mut map_2: Map<i32, ProtoString> = Map::new();
let mut map_2_mut = map_2.as_mut();
map_2_mut.extend([(2, "bing"), (3, "bong")]);

Expand All @@ -608,7 +608,7 @@ mod tests {

#[test]
fn test_copy_from() {
let mut map: Map<i32, ProtoStr> = Map::new();
let mut map: Map<i32, ProtoString> = Map::new();
let mut map_mut = map.as_mut();
map_mut.copy_from([(0, "fizz"), (1, "buzz"), (2, "fizzbuzz")]);

Expand All @@ -621,7 +621,7 @@ mod tests {
]
);

let mut map_2: Map<i32, ProtoStr> = Map::new();
let mut map_2: Map<i32, ProtoString> = Map::new();
let mut map_2_mut = map_2.as_mut();
map_2_mut.copy_from([(2, "bing"), (3, "bong")]);

Expand Down Expand Up @@ -651,12 +651,12 @@ mod tests {
macro_rules! gen_proto_keys {
($($key_t:ty),*) => {
$(
gen_proto_values!($key_t, f32, f64, i32, u32, i64, bool, ProtoStr, [u8]);
gen_proto_values!($key_t, f32, f64, i32, u32, i64, bool, ProtoString, ProtoBytes);
)*
}
}

gen_proto_keys!(i32, u32, i64, u64, bool, ProtoStr);
gen_proto_keys!(i32, u32, i64, u64, bool, ProtoString);
}

#[test]
Expand Down
1 change: 0 additions & 1 deletion rust/proxied.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,6 @@ pub trait IntoProxied<T: Proxied> {
mod tests {
use super::*;
use googletest::prelude::*;
use std::borrow::Cow;

#[derive(Debug, Default, PartialEq)]
struct MyProxied {
Expand Down
2 changes: 1 addition & 1 deletion rust/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub mod __public {
pub use crate::repeated::{
ProxiedInRepeated, Repeated, RepeatedIter, RepeatedMut, RepeatedView,
};
pub use crate::string::ProtoStr;
pub use crate::string::{ProtoBytes, ProtoStr, ProtoString};
pub use crate::{ParseError, SerializeError};
}
pub use __public::*;
Expand Down
Loading

0 comments on commit 6b7e814

Please sign in to comment.