use crate::__internal::{Enum, MatcherEq, Private, SealedInternal};
use crate::{
AsMut, AsView, Clear, ClearAndParse, CopyFrom, IntoProxied, Map, MapIter, MapMut, MapView,
MergeFrom, Message, MessageMutInterop, Mut, MutProxied, ParseError, ProtoBytes, ProtoStr,
ProtoString, Proxied, ProxiedInMapValue, ProxiedInRepeated, Repeated, RepeatedMut,
RepeatedView, Serialize, SerializeError, TakeFrom, View,
};
use core::fmt::Debug;
use paste::paste;
use std::convert::identity;
use std::ffi::{c_int, c_void};
use std::fmt;
use std::marker::PhantomData;
use std::mem::{ManuallyDrop, MaybeUninit};
use std::ops::Deref;
use std::ptr::{self, NonNull};
use std::slice;
#[doc(hidden)]
mod _opaque_pointees {
#[repr(C)]
pub struct RawMessageData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
#[repr(C)]
pub struct RawRepeatedFieldData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
#[repr(C)]
pub struct RawMapData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
#[repr(C)]
pub struct CppStdStringData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
}
#[doc(hidden)]
pub type RawMessage = NonNull<_opaque_pointees::RawMessageData>;
#[doc(hidden)]
pub type RawRepeatedField = NonNull<_opaque_pointees::RawRepeatedFieldData>;
#[doc(hidden)]
pub type RawMap = NonNull<_opaque_pointees::RawMapData>;
#[doc(hidden)]
pub type CppStdString = NonNull<_opaque_pointees::CppStdStringData>;
#[derive(Debug)]
#[doc(hidden)]
pub struct InnerProtoString {
owned_ptr: CppStdString,
}
extern "C" {
pub fn proto2_rust_Message_delete(m: RawMessage);
pub fn proto2_rust_Message_clear(m: RawMessage);
pub fn proto2_rust_Message_parse(m: RawMessage, input: PtrAndLen) -> bool;
pub fn proto2_rust_Message_parse_dont_enforce_required(m: RawMessage, input: PtrAndLen)
-> bool;
pub fn proto2_rust_Message_serialize(m: RawMessage, output: &mut SerializedData) -> bool;
pub fn proto2_rust_Message_copy_from(dst: RawMessage, src: RawMessage) -> bool;
pub fn proto2_rust_Message_merge_from(dst: RawMessage, src: RawMessage) -> bool;
}
impl Drop for InnerProtoString {
fn drop(&mut self) {
unsafe {
proto2_rust_cpp_delete_string(self.owned_ptr);
}
}
}
impl InnerProtoString {
pub(crate) fn as_bytes(&self) -> &[u8] {
unsafe { proto2_rust_cpp_string_to_view(self.owned_ptr).as_ref() }
}
pub fn into_raw(self) -> CppStdString {
let s = ManuallyDrop::new(self);
s.owned_ptr
}
pub unsafe fn from_raw(src: CppStdString) -> InnerProtoString {
InnerProtoString { owned_ptr: src }
}
}
impl From<&[u8]> for InnerProtoString {
fn from(val: &[u8]) -> Self {
let owned_ptr: CppStdString = unsafe { proto2_rust_cpp_new_string(val.into()) };
InnerProtoString { owned_ptr }
}
}
extern "C" {
fn proto2_rust_cpp_new_string(src: PtrAndLen) -> CppStdString;
fn proto2_rust_cpp_delete_string(src: CppStdString);
fn proto2_rust_cpp_string_to_view(src: CppStdString) -> PtrAndLen;
}
#[repr(C)]
#[derive(Copy, Clone)]
#[doc(hidden)]
pub struct PtrAndLen {
pub ptr: *const u8,
pub len: usize,
}
impl PtrAndLen {
pub unsafe fn as_ref<'a>(self) -> &'a [u8] {
if self.ptr.is_null() {
assert_eq!(self.len, 0, "Non-empty slice with null data pointer");
&[]
} else {
unsafe { slice::from_raw_parts(self.ptr, self.len) }
}
}
}
impl From<&[u8]> for PtrAndLen {
fn from(slice: &[u8]) -> Self {
Self { ptr: slice.as_ptr(), len: slice.len() }
}
}
impl From<&ProtoStr> for PtrAndLen {
fn from(s: &ProtoStr) -> Self {
let bytes = s.as_bytes();
Self { ptr: bytes.as_ptr(), len: bytes.len() }
}
}
#[repr(C)]
#[doc(hidden)]
pub struct SerializedData {
data: NonNull<u8>,
len: usize,
}
impl SerializedData {
pub fn new() -> Self {
Self { data: NonNull::dangling(), len: 0 }
}
pub unsafe fn from_raw_parts(data: NonNull<u8>, len: usize) -> Self {
Self { data, len }
}
pub fn as_ptr(&self) -> *const [u8] {
ptr::slice_from_raw_parts(self.data.as_ptr(), self.len)
}
fn as_mut_ptr(&mut self) -> *mut [u8] {
ptr::slice_from_raw_parts_mut(self.data.as_ptr(), self.len)
}
pub fn into_vec(self) -> Vec<u8> {
let s = ManuallyDrop::new(self);
unsafe {
Vec::<u8>::from_raw_parts(s.data.as_ptr(), s.len, s.len)
}
}
}
impl Deref for SerializedData {
type Target = [u8];
fn deref(&self) -> &Self::Target {
unsafe { &*self.as_ptr() }
}
}
impl Drop for SerializedData {
fn drop(&mut self) {
unsafe { drop(Box::from_raw(self.as_mut_ptr())) }
}
}
impl fmt::Debug for SerializedData {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self.deref(), f)
}
}
#[repr(C)]
#[doc(hidden)]
pub struct RustStringRawParts {
data: *const u8,
len: usize,
}
impl From<RustStringRawParts> for String {
fn from(value: RustStringRawParts) -> Self {
if value.data.is_null() {
return String::new();
}
unsafe { String::from_raw_parts(value.data as *mut u8, value.len, value.len) }
}
}
extern "C" {
fn proto2_rust_utf8_debug_string(raw: RawMessage) -> RustStringRawParts;
}
pub fn debug_string(raw: RawMessage, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let dbg_str: String = unsafe { proto2_rust_utf8_debug_string(raw) }.into();
write!(f, "{dbg_str}")
}
extern "C" {
#[link_name = "proto2_rust_messagelite_equals"]
pub fn raw_message_equals(raw1: RawMessage, raw2: RawMessage) -> bool;
}
pub type RawMapIter = UntypedMapIterator;
#[derive(Debug)]
#[doc(hidden)]
#[repr(transparent)]
pub struct OwnedMessageInner<T> {
raw: RawMessage,
_phantom: PhantomData<T>,
}
impl<T: Message> OwnedMessageInner<T> {
pub unsafe fn wrap_raw(raw: RawMessage) -> Self {
OwnedMessageInner { raw, _phantom: PhantomData }
}
pub fn raw(&self) -> RawMessage {
self.raw
}
}
#[derive(Debug)]
#[doc(hidden)]
#[repr(transparent)]
pub struct MessageMutInner<'msg, T> {
raw: RawMessage,
_phantom: PhantomData<(&'msg mut (), T)>,
}
impl<'msg, T: Message> Clone for MessageMutInner<'msg, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'msg, T: Message> Copy for MessageMutInner<'msg, T> {}
impl<'msg, T: Message> MessageMutInner<'msg, T> {
#[allow(clippy::needless_pass_by_ref_mut)] pub fn mut_of_owned(msg: &'msg mut OwnedMessageInner<T>) -> Self {
MessageMutInner { raw: msg.raw, _phantom: PhantomData }
}
pub unsafe fn wrap_raw(raw: RawMessage) -> Self {
MessageMutInner { raw, _phantom: PhantomData }
}
pub fn from_parent<ParentT: Message>(
_parent_msg: MessageMutInner<'msg, ParentT>,
message_field_ptr: RawMessage,
) -> Self {
Self { raw: message_field_ptr, _phantom: PhantomData }
}
pub fn raw(&self) -> RawMessage {
self.raw
}
}
#[derive(Debug)]
#[doc(hidden)]
#[repr(transparent)]
pub struct MessageViewInner<'msg, T> {
raw: RawMessage,
_phantom: PhantomData<(&'msg (), T)>,
}
impl<'msg, T: Message> Clone for MessageViewInner<'msg, T> {
fn clone(&self) -> Self {
*self
}
}
impl<'msg, T: Message> Copy for MessageViewInner<'msg, T> {}
impl<'msg, T: Message> MessageViewInner<'msg, T> {
pub unsafe fn wrap_raw(raw: RawMessage) -> Self {
MessageViewInner { raw, _phantom: PhantomData }
}
pub fn view_of_owned(msg: &'msg OwnedMessageInner<T>) -> Self {
MessageViewInner { raw: msg.raw, _phantom: PhantomData }
}
pub fn view_of_mut(msg: MessageMutInner<'msg, T>) -> Self {
MessageViewInner { raw: msg.raw, _phantom: PhantomData }
}
pub fn raw(&self) -> RawMessage {
self.raw
}
}
#[derive(Debug)]
#[doc(hidden)]
pub struct InnerRepeated {
raw: RawRepeatedField,
}
impl InnerRepeated {
pub fn as_mut(&mut self) -> InnerRepeatedMut<'_> {
InnerRepeatedMut::new(self.raw)
}
pub fn raw(&self) -> RawRepeatedField {
self.raw
}
pub unsafe fn from_raw(raw: RawRepeatedField) -> Self {
Self { raw }
}
}
#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub struct InnerRepeatedMut<'msg> {
pub(crate) raw: RawRepeatedField,
_phantom: PhantomData<&'msg ()>,
}
impl<'msg> InnerRepeatedMut<'msg> {
#[doc(hidden)]
pub fn new(raw: RawRepeatedField) -> Self {
InnerRepeatedMut { raw, _phantom: PhantomData }
}
}
trait CppTypeConversions: Proxied {
type InsertElemType;
type ElemType;
fn elem_to_view<'msg>(v: Self::ElemType) -> View<'msg, Self>;
fn into_insertelem(v: Self) -> Self::InsertElemType;
}
macro_rules! impl_cpp_type_conversions_for_scalars {
($($t:ty),* $(,)?) => {
$(
impl CppTypeConversions for $t {
type InsertElemType = Self;
type ElemType = Self;
fn elem_to_view<'msg>(v: Self) -> View<'msg, Self> {
v
}
fn into_insertelem(v: Self) -> Self {
v
}
}
)*
}
}
impl_cpp_type_conversions_for_scalars!(i32, u32, i64, u64, f32, f64, bool);
impl CppTypeConversions for ProtoString {
type InsertElemType = CppStdString;
type ElemType = PtrAndLen;
fn elem_to_view<'msg>(v: PtrAndLen) -> View<'msg, ProtoString> {
ptrlen_to_str(v)
}
fn into_insertelem(v: Self) -> CppStdString {
v.into_inner(Private).into_raw()
}
}
impl CppTypeConversions for ProtoBytes {
type InsertElemType = CppStdString;
type ElemType = PtrAndLen;
fn elem_to_view<'msg>(v: Self::ElemType) -> View<'msg, Self> {
ptrlen_to_bytes(v)
}
fn into_insertelem(v: Self) -> CppStdString {
v.into_inner(Private).into_raw()
}
}
unsafe impl<T> ProxiedInRepeated for T
where
Self: MutProxied + CppGetRawMessage + Message,
for<'a> View<'a, Self>:
From<MessageViewInner<'a, Self>> + std::default::Default + CppGetRawMessage,
for<'a> Mut<'a, Self>: From<MessageMutInner<'a, Self>>,
{
fn repeated_new(_private: Private) -> Repeated<Self> {
unsafe {
Repeated::from_inner(
Private,
InnerRepeated::from_raw(proto2_rust_RepeatedField_Message_new()),
)
}
}
unsafe fn repeated_free(_private: Private, f: &mut Repeated<Self>) {
unsafe { proto2_rust_RepeatedField_Message_free(f.as_view().as_raw(Private)) }
}
fn repeated_len(f: View<Repeated<Self>>) -> usize {
unsafe { proto2_rust_RepeatedField_Message_size(f.as_raw(Private)) }
}
unsafe fn repeated_set_unchecked(
mut f: Mut<Repeated<Self>>,
i: usize,
v: impl IntoProxied<Self>,
) {
unsafe {
proto2_rust_Message_copy_from(
proto2_rust_RepeatedField_Message_get_mut(f.as_raw(Private), i),
v.into_proxied(Private).get_raw_message(Private),
);
}
}
unsafe fn repeated_get_unchecked(f: View<Repeated<Self>>, i: usize) -> View<Self> {
let msg = unsafe { proto2_rust_RepeatedField_Message_get(f.as_raw(Private), i) };
let inner = unsafe { MessageViewInner::wrap_raw(msg) };
inner.into()
}
unsafe fn repeated_get_mut_unchecked(mut f: Mut<Repeated<Self>>, i: usize) -> Mut<Self> {
let msg = unsafe { proto2_rust_RepeatedField_Message_get_mut(f.as_raw(Private), i) };
let inner = unsafe { MessageMutInner::wrap_raw(msg) };
inner.into()
}
fn repeated_clear(mut f: Mut<Repeated<Self>>) {
unsafe { proto2_rust_RepeatedField_Message_clear(f.as_raw(Private)) };
}
fn repeated_push(mut f: Mut<Repeated<Self>>, v: impl IntoProxied<Self>) {
unsafe {
let prototype =
<View<Self> as std::default::Default>::default().get_raw_message(Private);
let new_elem = proto2_rust_RepeatedField_Message_add(f.as_raw(Private), prototype);
proto2_rust_Message_copy_from(
new_elem,
v.into_proxied(Private).get_raw_message(Private),
);
}
}
fn repeated_copy_from(src: View<Repeated<Self>>, mut dest: Mut<Repeated<Self>>) {
unsafe {
proto2_rust_RepeatedField_Message_copy_from(dest.as_raw(Private), src.as_raw(Private));
}
}
fn repeated_reserve(mut f: Mut<Repeated<Self>>, additional: usize) {
unsafe { proto2_rust_RepeatedField_Message_reserve(f.as_raw(Private), additional) }
}
}
macro_rules! impl_repeated_primitives {
(@impl $($t:ty => [
$new_thunk:ident,
$free_thunk:ident,
$add_thunk:ident,
$size_thunk:ident,
$get_thunk:ident,
$set_thunk:ident,
$clear_thunk:ident,
$copy_from_thunk:ident,
$reserve_thunk:ident $(,)?
]),* $(,)?) => {
$(
extern "C" {
fn $new_thunk() -> RawRepeatedField;
fn $free_thunk(f: RawRepeatedField);
fn $add_thunk(f: RawRepeatedField, v: <$t as CppTypeConversions>::InsertElemType);
fn $size_thunk(f: RawRepeatedField) -> usize;
fn $get_thunk(
f: RawRepeatedField,
i: usize) -> <$t as CppTypeConversions>::ElemType;
fn $set_thunk(
f: RawRepeatedField,
i: usize,
v: <$t as CppTypeConversions>::InsertElemType);
fn $clear_thunk(f: RawRepeatedField);
fn $copy_from_thunk(src: RawRepeatedField, dst: RawRepeatedField);
fn $reserve_thunk(
f: RawRepeatedField,
additional: usize);
}
unsafe impl ProxiedInRepeated for $t {
#[allow(dead_code)]
#[inline]
fn repeated_new(_: Private) -> Repeated<$t> {
Repeated::from_inner(Private, InnerRepeated {
raw: unsafe { $new_thunk() }
})
}
#[allow(dead_code)]
#[inline]
unsafe fn repeated_free(_: Private, f: &mut Repeated<$t>) {
unsafe { $free_thunk(f.as_mut().as_raw(Private)) }
}
#[inline]
fn repeated_len(f: View<Repeated<$t>>) -> usize {
unsafe { $size_thunk(f.as_raw(Private)) }
}
#[inline]
fn repeated_push(mut f: Mut<Repeated<$t>>, v: impl IntoProxied<$t>) {
unsafe { $add_thunk(f.as_raw(Private), <$t as CppTypeConversions>::into_insertelem(v.into_proxied(Private))) }
}
#[inline]
fn repeated_clear(mut f: Mut<Repeated<$t>>) {
unsafe { $clear_thunk(f.as_raw(Private)) }
}
#[inline]
unsafe fn repeated_get_unchecked(f: View<Repeated<$t>>, i: usize) -> View<$t> {
<$t as CppTypeConversions>::elem_to_view(
unsafe { $get_thunk(f.as_raw(Private), i) })
}
#[inline]
unsafe fn repeated_set_unchecked(mut f: Mut<Repeated<$t>>, i: usize, v: impl IntoProxied<$t>) {
unsafe { $set_thunk(f.as_raw(Private), i, <$t as CppTypeConversions>::into_insertelem(v.into_proxied(Private))) }
}
#[inline]
fn repeated_copy_from(src: View<Repeated<$t>>, mut dest: Mut<Repeated<$t>>) {
unsafe { $copy_from_thunk(src.as_raw(Private), dest.as_raw(Private)) }
}
#[inline]
fn repeated_reserve(mut f: Mut<Repeated<$t>>, additional: usize) {
unsafe { $reserve_thunk(f.as_raw(Private), additional) }
}
}
)*
};
($($t:ty),* $(,)?) => {
paste!{
impl_repeated_primitives!(@impl $(
$t => [
[< proto2_rust_RepeatedField_ $t _new >],
[< proto2_rust_RepeatedField_ $t _free >],
[< proto2_rust_RepeatedField_ $t _add >],
[< proto2_rust_RepeatedField_ $t _size >],
[< proto2_rust_RepeatedField_ $t _get >],
[< proto2_rust_RepeatedField_ $t _set >],
[< proto2_rust_RepeatedField_ $t _clear >],
[< proto2_rust_RepeatedField_ $t _copy_from >],
[< proto2_rust_RepeatedField_ $t _reserve >],
],
)*);
}
};
}
impl_repeated_primitives!(i32, u32, i64, u64, f32, f64, bool, ProtoString, ProtoBytes);
extern "C" {
pub fn proto2_rust_RepeatedField_Message_new() -> RawRepeatedField;
pub fn proto2_rust_RepeatedField_Message_free(field: RawRepeatedField);
pub fn proto2_rust_RepeatedField_Message_size(field: RawRepeatedField) -> usize;
pub fn proto2_rust_RepeatedField_Message_get(
field: RawRepeatedField,
index: usize,
) -> RawMessage;
pub fn proto2_rust_RepeatedField_Message_get_mut(
field: RawRepeatedField,
index: usize,
) -> RawMessage;
pub fn proto2_rust_RepeatedField_Message_add(
field: RawRepeatedField,
prototype: RawMessage,
) -> RawMessage;
pub fn proto2_rust_RepeatedField_Message_clear(field: RawRepeatedField);
pub fn proto2_rust_RepeatedField_Message_copy_from(
dst: RawRepeatedField,
src: RawRepeatedField,
);
pub fn proto2_rust_RepeatedField_Message_reserve(field: RawRepeatedField, additional: usize);
}
pub fn cast_enum_repeated_view<E: Enum + ProxiedInRepeated>(
repeated: RepeatedView<E>,
) -> RepeatedView<c_int> {
unsafe { RepeatedView::from_raw(Private, repeated.as_raw(Private)) }
}
pub fn cast_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
mut repeated: RepeatedMut<E>,
) -> RepeatedMut<c_int> {
unsafe {
RepeatedMut::from_inner(
Private,
InnerRepeatedMut { raw: repeated.as_raw(Private), _phantom: PhantomData },
)
}
}
pub fn reserve_enum_repeated_mut<E: Enum + ProxiedInRepeated>(
repeated: RepeatedMut<E>,
additional: usize,
) {
let int_repeated = cast_enum_repeated_mut(repeated);
ProxiedInRepeated::repeated_reserve(int_repeated, additional);
}
pub fn new_enum_repeated<E: Enum + ProxiedInRepeated>() -> Repeated<E> {
let int_repeated = Repeated::<c_int>::new();
let raw = int_repeated.inner.raw();
std::mem::forget(int_repeated);
unsafe { Repeated::from_inner(Private, InnerRepeated::from_raw(raw)) }
}
pub unsafe fn free_enum_repeated<E: Enum + ProxiedInRepeated>(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);
std::mem::forget(int_r);
}
}
#[derive(Debug)]
#[doc(hidden)]
pub struct InnerMap {
pub(crate) raw: RawMap,
}
impl InnerMap {
pub fn new(raw: RawMap) -> Self {
Self { raw }
}
pub fn as_mut(&mut self) -> InnerMapMut<'_> {
InnerMapMut { raw: self.raw, _phantom: PhantomData }
}
}
#[derive(Clone, Copy, Debug)]
#[doc(hidden)]
pub struct InnerMapMut<'msg> {
pub(crate) raw: RawMap,
_phantom: PhantomData<&'msg ()>,
}
#[doc(hidden)]
impl<'msg> InnerMapMut<'msg> {
pub fn new(raw: RawMap) -> Self {
InnerMapMut { raw, _phantom: PhantomData }
}
pub fn as_raw(&self) -> RawMap {
self.raw
}
}
#[repr(C)]
#[doc(hidden)]
pub struct UntypedMapIterator {
node: *mut c_void,
map: *const c_void,
bucket_index: u32,
}
impl UntypedMapIterator {
fn at_end(&self) -> bool {
self.node.is_null()
}
#[inline(always)]
pub unsafe fn next_unchecked<'a, K, V, FfiKey, FfiValue>(
&mut self,
iter_get_thunk: unsafe fn(
iter: &mut UntypedMapIterator,
key: *mut FfiKey,
value: *mut FfiValue,
),
from_ffi_key: impl FnOnce(FfiKey) -> View<'a, K>,
from_ffi_value: impl FnOnce(FfiValue) -> View<'a, V>,
) -> Option<(View<'a, K>, View<'a, V>)>
where
K: Proxied + 'a,
V: ProxiedInMapValue<K> + 'a,
{
if self.at_end() {
return None;
}
let mut ffi_key = MaybeUninit::uninit();
let mut ffi_value = MaybeUninit::uninit();
unsafe { (iter_get_thunk)(self, ffi_key.as_mut_ptr(), ffi_value.as_mut_ptr()) }
unsafe { proto2_rust_thunk_UntypedMapIterator_increment(self) }
unsafe {
Some((from_ffi_key(ffi_key.assume_init()), from_ffi_value(ffi_value.assume_init())))
}
}
}
#[doc(hidden)]
#[repr(u8)]
#[derive(Debug, PartialEq)]
pub enum MapValueTag {
Bool,
U32,
U64,
F32,
F64,
String,
Message,
}
#[doc(hidden)]
#[repr(C)]
pub union MapValueUnion {
pub b: bool,
pub u: u32,
pub uu: u64,
pub f: f32,
pub ff: f64,
pub s: Option<CppStdString>,
pub m: RawMessage,
}
#[doc(hidden)]
#[repr(C)]
pub struct MapValue {
pub tag: MapValueTag,
pub val: MapValueUnion,
}
impl MapValue {
fn make_bool(b: bool) -> Self {
MapValue { tag: MapValueTag::Bool, val: MapValueUnion { b } }
}
pub fn make_u32(u: u32) -> Self {
MapValue { tag: MapValueTag::U32, val: MapValueUnion { u } }
}
fn make_u64(uu: u64) -> Self {
MapValue { tag: MapValueTag::U64, val: MapValueUnion { uu } }
}
pub fn make_f32(f: f32) -> Self {
MapValue { tag: MapValueTag::F32, val: MapValueUnion { f } }
}
fn make_f64(ff: f64) -> Self {
MapValue { tag: MapValueTag::F64, val: MapValueUnion { ff } }
}
fn make_string(s: CppStdString) -> Self {
MapValue { tag: MapValueTag::String, val: MapValueUnion { s: Some(s) } }
}
pub fn make_message(m: RawMessage) -> Self {
MapValue { tag: MapValueTag::Message, val: MapValueUnion { m } }
}
}
pub trait CppMapTypeConversions: Proxied {
fn get_prototype() -> MapValue;
fn to_map_value(self) -> MapValue;
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self>;
#[allow(unused_variables)]
unsafe fn mut_from_map_value<'a>(value: MapValue) -> Mut<'a, Self>
where
Self: Message,
{
panic!("mut_from_map_value is only implemented for messages")
}
}
impl CppMapTypeConversions for u32 {
fn get_prototype() -> MapValue {
MapValue::make_u32(0)
}
fn to_map_value(self) -> MapValue {
MapValue::make_u32(self)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::U32);
unsafe { value.val.u }
}
}
impl CppMapTypeConversions for i32 {
fn get_prototype() -> MapValue {
MapValue::make_u32(0)
}
fn to_map_value(self) -> MapValue {
MapValue::make_u32(self as u32)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::U32);
unsafe { value.val.u as i32 }
}
}
impl CppMapTypeConversions for u64 {
fn get_prototype() -> MapValue {
MapValue::make_u64(0)
}
fn to_map_value(self) -> MapValue {
MapValue::make_u64(self)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::U64);
unsafe { value.val.uu }
}
}
impl CppMapTypeConversions for i64 {
fn get_prototype() -> MapValue {
MapValue::make_u64(0)
}
fn to_map_value(self) -> MapValue {
MapValue::make_u64(self as u64)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::U64);
unsafe { value.val.uu as i64 }
}
}
impl CppMapTypeConversions for f32 {
fn get_prototype() -> MapValue {
MapValue::make_f32(0f32)
}
fn to_map_value(self) -> MapValue {
MapValue::make_f32(self)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::F32);
unsafe { value.val.f }
}
}
impl CppMapTypeConversions for f64 {
fn get_prototype() -> MapValue {
MapValue::make_f64(0.0)
}
fn to_map_value(self) -> MapValue {
MapValue::make_f64(self)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::F64);
unsafe { value.val.ff }
}
}
impl CppMapTypeConversions for bool {
fn get_prototype() -> MapValue {
MapValue::make_bool(false)
}
fn to_map_value(self) -> MapValue {
MapValue::make_bool(self)
}
unsafe fn from_map_value<'a>(value: MapValue) -> View<'a, Self> {
debug_assert_eq!(value.tag, MapValueTag::Bool);
unsafe { value.val.b }
}
}
impl CppMapTypeConversions for ProtoString {
fn get_prototype() -> MapValue {
MapValue { tag: MapValueTag::String, val: MapValueUnion { s: None } }
}
fn to_map_value(self) -> MapValue {
MapValue::make_string(protostr_into_cppstdstring(self))
}
unsafe fn from_map_value<'a>(value: MapValue) -> &'a ProtoStr {
debug_assert_eq!(value.tag, MapValueTag::String);
unsafe {
ProtoStr::from_utf8_unchecked(
ptrlen_to_str(proto2_rust_cpp_string_to_view(value.val.s.unwrap())).into(),
)
}
}
}
impl CppMapTypeConversions for ProtoBytes {
fn get_prototype() -> MapValue {
MapValue { tag: MapValueTag::String, val: MapValueUnion { s: None } }
}
fn to_map_value(self) -> MapValue {
MapValue::make_string(protobytes_into_cppstdstring(self))
}
unsafe fn from_map_value<'a>(value: MapValue) -> &'a [u8] {
debug_assert_eq!(value.tag, MapValueTag::String);
unsafe { proto2_rust_cpp_string_to_view(value.val.s.unwrap()).as_ref() }
}
}
#[doc(hidden)]
pub trait MapKey
where
Self: Proxied,
{
type FfiKey;
fn to_view<'a>(key: Self::FfiKey) -> View<'a, Self>;
unsafe fn insert(m: RawMap, key: View<'_, Self>, value: MapValue) -> bool;
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, key: View<'_, Self>) -> bool;
}
macro_rules! generate_map_key_impl {
( $($key:ty, $mutable_ffi_key:ty, $to_ffi:expr, $from_ffi:expr;)* ) => {
paste! {
$(
impl MapKey for $key {
type FfiKey = $mutable_ffi_key;
#[inline]
fn to_view<'a>(key: Self::FfiKey) -> View<'a, Self> {
$from_ffi(key)
}
#[inline]
unsafe fn insert(
m: RawMap,
key: View<'_, Self>,
value: MapValue,
) -> bool {
unsafe { [< proto2_rust_map_insert_ $key >](m, $to_ffi(key), value) }
}
#[inline]
unsafe fn get(
m: RawMap,
key: View<'_, Self>,
value: *mut MapValue,
) -> bool {
unsafe { [< proto2_rust_map_get_ $key >](m, $to_ffi(key), value) }
}
#[inline]
unsafe fn iter_get(
iter: &mut UntypedMapIterator,
key: *mut Self::FfiKey,
value: *mut MapValue,
) {
unsafe { [< proto2_rust_map_iter_get_ $key >](iter, key, value) }
}
#[inline]
unsafe fn remove(m: RawMap, key: View<'_, Self>) -> bool {
unsafe { [< proto2_rust_map_remove_ $key >](m, $to_ffi(key)) }
}
}
)*
}
}
}
generate_map_key_impl!(
bool, bool, identity, identity;
i32, i32, identity, identity;
u32, u32, identity, identity;
i64, i64, identity, identity;
u64, u64, identity, identity;
ProtoString, PtrAndLen, str_to_ptrlen, ptrlen_to_str;
);
impl<Key, Value> ProxiedInMapValue<Key> for Value
where
Key: Proxied + MapKey + CppMapTypeConversions,
Value: Proxied + CppMapTypeConversions,
{
fn map_new(_private: Private) -> Map<Key, Self> {
unsafe {
Map::from_inner(
Private,
InnerMap::new(proto2_rust_map_new(Key::get_prototype(), Value::get_prototype())),
)
}
}
unsafe fn map_free(_private: Private, map: &mut Map<Key, Self>) {
unsafe {
proto2_rust_map_free(map.as_raw(Private));
}
}
fn map_clear(mut map: MapMut<Key, Self>) {
unsafe {
proto2_rust_map_clear(map.as_raw(Private));
}
}
fn map_len(map: MapView<Key, Self>) -> usize {
unsafe { proto2_rust_map_size(map.as_raw(Private)) }
}
fn map_insert(
mut map: MapMut<Key, Self>,
key: View<'_, Key>,
value: impl IntoProxied<Self>,
) -> bool {
unsafe { Key::insert(map.as_raw(Private), key, value.into_proxied(Private).to_map_value()) }
}
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), key, value.as_mut_ptr()) };
if !found {
return None;
}
unsafe { Some(Self::from_map_value(value.assume_init())) }
}
fn map_get_mut<'a>(mut map: MapMut<'a, Key, Self>, key: View<'_, Key>) -> Option<Mut<'a, Self>>
where
Value: Message,
{
let mut value = std::mem::MaybeUninit::uninit();
let found = unsafe { Key::get(map.as_raw(Private), key, value.as_mut_ptr()) };
if !found {
return None;
}
unsafe { Some(Self::mut_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), key) }
}
fn map_iter(map: MapView<Key, Self>) -> MapIter<Key, Self> {
unsafe { MapIter::from_raw(Private, proto2_rust_map_iter(map.as_raw(Private))) }
}
fn map_iter_next<'a>(
iter: &mut MapIter<'a, Key, Self>,
) -> Option<(View<'a, Key>, View<'a, Self>)> {
unsafe {
iter.as_raw_mut(Private).next_unchecked::<Key, Self, _, _>(
|iter, key, value| Key::iter_get(iter, key, value),
|ffi_key| Key::to_view(ffi_key),
|value| Self::from_map_value(value),
)
}
}
}
macro_rules! impl_map_primitives {
(@impl $(($rust_type:ty, $cpp_type:ty) => [
$insert_thunk:ident,
$get_thunk:ident,
$iter_get_thunk:ident,
$remove_thunk:ident,
]),* $(,)?) => {
$(
extern "C" {
pub fn $insert_thunk(
m: RawMap,
key: $cpp_type,
value: MapValue,
) -> bool;
pub fn $get_thunk(
m: RawMap,
key: $cpp_type,
value: *mut MapValue,
) -> bool;
pub fn $iter_get_thunk(
iter: &mut UntypedMapIterator,
key: *mut $cpp_type,
value: *mut MapValue,
);
pub fn $remove_thunk(m: RawMap, key: $cpp_type) -> bool;
}
)*
};
($($rust_type:ty, $cpp_type:ty;)* $(,)?) => {
paste!{
impl_map_primitives!(@impl $(
($rust_type, $cpp_type) => [
[< proto2_rust_map_insert_ $rust_type >],
[< proto2_rust_map_get_ $rust_type >],
[< proto2_rust_map_iter_get_ $rust_type >],
[< proto2_rust_map_remove_ $rust_type >],
],
)*);
}
};
}
impl_map_primitives!(
i32, i32;
u32, u32;
i64, i64;
u64, u64;
bool, bool;
ProtoString, PtrAndLen;
);
extern "C" {
fn proto2_rust_thunk_UntypedMapIterator_increment(iter: &mut UntypedMapIterator);
pub fn proto2_rust_map_new(key_prototype: MapValue, value_prototype: MapValue) -> RawMap;
pub fn proto2_rust_map_free(m: RawMap);
pub fn proto2_rust_map_clear(m: RawMap);
pub fn proto2_rust_map_size(m: RawMap) -> usize;
pub fn proto2_rust_map_iter(m: RawMap) -> UntypedMapIterator;
}
fn str_to_ptrlen<'msg>(val: impl Into<&'msg ProtoStr>) -> PtrAndLen {
val.into().as_bytes().into()
}
fn ptrlen_to_str<'msg>(val: PtrAndLen) -> &'msg ProtoStr {
unsafe { ProtoStr::from_utf8_unchecked(val.as_ref()) }
}
fn protostr_into_cppstdstring(val: ProtoString) -> CppStdString {
val.into_inner(Private).into_raw()
}
fn protobytes_into_cppstdstring(val: ProtoBytes) -> CppStdString {
val.into_inner(Private).into_raw()
}
fn ptrlen_to_bytes<'msg>(val: PtrAndLen) -> &'msg [u8] {
unsafe { val.as_ref() }
}
#[doc(hidden)]
pub unsafe trait CppGetRawMessage: SealedInternal {
fn get_raw_message(&self, _private: Private) -> RawMessage;
}
unsafe impl<T> CppGetRawMessage for T
where
Self: AsMut + AsView,
for<'a> View<'a, <Self as AsView>::Proxied>: CppGetRawMessage,
{
fn get_raw_message(&self, _private: Private) -> RawMessage {
self.as_view().get_raw_message(_private)
}
}
#[doc(hidden)]
pub unsafe trait CppGetRawMessageMut: SealedInternal {
fn get_raw_message_mut(&mut self, _private: Private) -> RawMessage;
}
unsafe impl<T> CppGetRawMessageMut for T
where
Self: MutProxied,
for<'a> Mut<'a, Self>: CppGetRawMessageMut,
{
fn get_raw_message_mut(&mut self, _private: Private) -> RawMessage {
self.as_mut().get_raw_message_mut(_private)
}
}
impl<'a, T> MessageMutInterop<'a> for T
where
Self: AsMut + CppGetRawMessageMut + From<MessageMutInner<'a, <Self as AsMut>::MutProxied>>,
<Self as AsMut>::MutProxied: Message,
{
unsafe fn __unstable_wrap_raw_message_mut(msg: &'a mut *mut std::ffi::c_void) -> Self {
let raw = RawMessage::new(*msg as *mut _).unwrap();
let inner = unsafe { MessageMutInner::wrap_raw(raw) };
inner.into()
}
unsafe fn __unstable_wrap_raw_message_mut_unchecked_lifetime(
msg: *mut std::ffi::c_void,
) -> Self {
let raw = RawMessage::new(msg as *mut _).unwrap();
let inner = unsafe { MessageMutInner::wrap_raw(raw) };
inner.into()
}
fn __unstable_as_raw_message_mut(&mut self) -> *mut std::ffi::c_void {
self.get_raw_message_mut(Private).as_ptr() as *mut _
}
}
impl<T> MatcherEq for T
where
Self: AsView + Debug,
for<'a> View<'a, <Self as AsView>::Proxied>: CppGetRawMessage,
{
fn matches(&self, o: &Self) -> bool {
unsafe {
raw_message_equals(
self.as_view().get_raw_message(Private),
o.as_view().get_raw_message(Private),
)
}
}
}
impl<T: CppGetRawMessageMut> Clear for T {
fn clear(&mut self) {
unsafe { proto2_rust_Message_clear(self.get_raw_message_mut(Private)) }
}
}
impl<T: CppGetRawMessageMut> ClearAndParse for T {
fn clear_and_parse(&mut self, data: &[u8]) -> Result<(), ParseError> {
unsafe { proto2_rust_Message_parse(self.get_raw_message_mut(Private), data.into()) }
.then_some(())
.ok_or(ParseError)
}
fn clear_and_parse_dont_enforce_required(&mut self, data: &[u8]) -> Result<(), ParseError> {
unsafe {
proto2_rust_Message_parse_dont_enforce_required(
self.get_raw_message_mut(Private),
data.into(),
)
}
.then_some(())
.ok_or(ParseError)
}
}
impl<T: CppGetRawMessage> Serialize for T {
fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
let mut serialized_data = SerializedData::new();
let success = unsafe {
proto2_rust_Message_serialize(self.get_raw_message(Private), &mut serialized_data)
};
if success {
Ok(serialized_data.into_vec())
} else {
Err(SerializeError)
}
}
}
impl<T> TakeFrom for T
where
Self: CopyFrom + AsMut,
for<'a> Mut<'a, <Self as AsMut>::MutProxied>: Clear,
{
fn take_from(&mut self, mut src: impl AsMut<MutProxied = Self::Proxied>) {
let mut src = src.as_mut();
CopyFrom::copy_from(self, AsView::as_view(&src));
Clear::clear(&mut src);
}
}
impl<T> CopyFrom for T
where
Self: AsMut,
for<'a> View<'a, Self::Proxied>: CppGetRawMessage,
for<'a> Mut<'a, Self::Proxied>: CppGetRawMessageMut,
{
fn copy_from(&mut self, src: impl AsView<Proxied = Self::Proxied>) {
unsafe {
proto2_rust_Message_copy_from(
self.as_mut().get_raw_message_mut(Private),
src.as_view().get_raw_message(Private),
);
}
}
}
impl<T> MergeFrom for T
where
Self: AsMut,
for<'a> View<'a, Self::Proxied>: CppGetRawMessage,
for<'a> Mut<'a, Self::Proxied>: CppGetRawMessageMut,
{
fn merge_from(&mut self, src: impl AsView<Proxied = Self::Proxied>) {
unsafe {
proto2_rust_Message_merge_from(
self.as_mut().get_raw_message_mut(Private),
src.as_view().get_raw_message(Private),
);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use googletest::prelude::*;
fn allocate_byte_array(content: &'static [u8]) -> (*mut u8, usize) {
let content: &mut [u8] = Box::leak(content.into());
(content.as_mut_ptr(), content.len())
}
#[gtest]
fn test_serialized_data_roundtrip() {
let (ptr, len) = allocate_byte_array(b"Hello world");
let serialized_data = SerializedData { data: NonNull::new(ptr).unwrap(), len };
assert_that!(&*serialized_data, eq(b"Hello world"));
}
#[gtest]
fn test_empty_string() {
let empty_str: String = RustStringRawParts { data: std::ptr::null(), len: 0 }.into();
assert_that!(empty_str, eq(""));
}
}