|
|
@ -30,78 +30,28 @@ |
|
|
|
|
|
|
|
|
|
|
|
// Rust Protobuf runtime using the C++ kernel.
|
|
|
|
// Rust Protobuf runtime using the C++ kernel.
|
|
|
|
|
|
|
|
|
|
|
|
pub use common::ParseError; |
|
|
|
|
|
|
|
pub use common::PtrAndLen; |
|
|
|
|
|
|
|
use std::fmt; |
|
|
|
|
|
|
|
use std::ops::Deref; |
|
|
|
|
|
|
|
use std::ptr::NonNull; |
|
|
|
|
|
|
|
use std::slice; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use std::alloc; |
|
|
|
use std::alloc; |
|
|
|
use std::alloc::Layout; |
|
|
|
use std::alloc::Layout; |
|
|
|
|
|
|
|
use std::boxed::Box; |
|
|
|
use std::cell::UnsafeCell; |
|
|
|
use std::cell::UnsafeCell; |
|
|
|
|
|
|
|
use std::fmt; |
|
|
|
use std::marker::PhantomData; |
|
|
|
use std::marker::PhantomData; |
|
|
|
use std::mem::MaybeUninit; |
|
|
|
use std::mem::MaybeUninit; |
|
|
|
|
|
|
|
use std::ops::Deref; |
|
|
|
/// Represents serialized Protobuf wire format data. It's typically produced
|
|
|
|
use std::ptr::NonNull; |
|
|
|
/// by `<Message>.serialize()`.
|
|
|
|
use std::slice; |
|
|
|
///
|
|
|
|
|
|
|
|
/// This struct is ABI compatible with the equivalent struct on the C++
|
|
|
|
|
|
|
|
/// side. It owns (and drops) its data.
|
|
|
|
|
|
|
|
// copybara:strip_begin
|
|
|
|
|
|
|
|
// LINT.IfChange
|
|
|
|
|
|
|
|
// copybara:strip_end
|
|
|
|
|
|
|
|
#[repr(C)] |
|
|
|
|
|
|
|
pub struct SerializedData { |
|
|
|
|
|
|
|
/// Owns the memory.
|
|
|
|
|
|
|
|
data: NonNull<u8>, |
|
|
|
|
|
|
|
len: usize, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// copybara:strip_begin
|
|
|
|
|
|
|
|
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/cpp_kernel/cpp_api.
|
|
|
|
|
|
|
|
// h) copybara:strip_end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl SerializedData { |
|
|
|
|
|
|
|
pub unsafe fn from_raw_parts(data: NonNull<u8>, len: usize) -> Self { |
|
|
|
|
|
|
|
Self { data, len } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Deref for SerializedData { |
|
|
|
|
|
|
|
type Target = [u8]; |
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
|
|
|
|
|
unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for SerializedData { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
alloc::dealloc(self.data.as_ptr(), Layout::array::<u8>(self.len).unwrap()); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for SerializedData { |
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
|
|
|
|
|
|
fmt::Debug::fmt(self.deref(), f) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub mod __runtime { |
|
|
|
|
|
|
|
use super::*; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A wrapper over a `proto2::Arena`.
|
|
|
|
/// A wrapper over a `proto2::Arena`.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This is not a safe wrapper per se, because the allocation functions
|
|
|
|
/// This is not a safe wrapper per se, because the allocation functions still
|
|
|
|
/// still have sharp edges (see their safety docs for more info).
|
|
|
|
/// have sharp edges (see their safety docs for more info).
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// This is an owning type and will automatically free the arena when
|
|
|
|
/// This is an owning type and will automatically free the arena when
|
|
|
|
/// dropped.
|
|
|
|
/// dropped.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// Note that this type is neither `Sync` nor `Send`.
|
|
|
|
/// Note that this type is neither `Sync` nor `Send`.
|
|
|
|
pub struct Arena { |
|
|
|
pub struct Arena { |
|
|
|
_ptr: NonNull<u8>, |
|
|
|
ptr: NonNull<u8>, |
|
|
|
_not_sync: PhantomData<UnsafeCell<()>>, |
|
|
|
_not_sync: PhantomData<UnsafeCell<()>>, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -109,7 +59,7 @@ pub mod __runtime { |
|
|
|
/// Allocates a fresh arena.
|
|
|
|
/// Allocates a fresh arena.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub fn new() -> Self { |
|
|
|
pub fn new() -> Self { |
|
|
|
Self { _ptr: NonNull::dangling(), _not_sync: PhantomData } |
|
|
|
Self { ptr: NonNull::dangling(), _not_sync: PhantomData } |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns the raw, C++-managed pointer to the arena.
|
|
|
|
/// Returns the raw, C++-managed pointer to the arena.
|
|
|
@ -124,7 +74,7 @@ pub mod __runtime { |
|
|
|
///
|
|
|
|
///
|
|
|
|
/// `layout`'s alignment must be less than `UPB_MALLOC_ALIGN`.
|
|
|
|
/// `layout`'s alignment must be less than `UPB_MALLOC_ALIGN`.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub unsafe fn alloc(&self, _layout: Layout) -> &mut [MaybeUninit<u8>] { |
|
|
|
pub unsafe fn alloc(&self, layout: Layout) -> &mut [MaybeUninit<u8>] { |
|
|
|
unimplemented!() |
|
|
|
unimplemented!() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -133,15 +83,10 @@ pub mod __runtime { |
|
|
|
/// # Safety
|
|
|
|
/// # Safety
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// After calling this function, `ptr` is essentially zapped. `old` must
|
|
|
|
/// After calling this function, `ptr` is essentially zapped. `old` must
|
|
|
|
/// be the layout `ptr` was allocated with via [`Arena::alloc()`].
|
|
|
|
/// be the layout `ptr` was allocated with via [`Arena::alloc()`]. `new`'s
|
|
|
|
/// `new`'s alignment must be less than `UPB_MALLOC_ALIGN`.
|
|
|
|
/// alignment must be less than `UPB_MALLOC_ALIGN`.
|
|
|
|
#[inline] |
|
|
|
#[inline] |
|
|
|
pub unsafe fn resize( |
|
|
|
pub unsafe fn resize(&self, ptr: *mut u8, old: Layout, new: Layout) -> &[MaybeUninit<u8>] { |
|
|
|
&self, |
|
|
|
|
|
|
|
_ptr: *mut u8, |
|
|
|
|
|
|
|
_old: Layout, |
|
|
|
|
|
|
|
_new: Layout, |
|
|
|
|
|
|
|
) -> &[MaybeUninit<u8>] { |
|
|
|
|
|
|
|
unimplemented!() |
|
|
|
unimplemented!() |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -152,12 +97,55 @@ pub mod __runtime { |
|
|
|
// unimplemented
|
|
|
|
// unimplemented
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} // mod __runtime
|
|
|
|
|
|
|
|
|
|
|
|
/// Represents serialized Protobuf wire format data. It's typically produced by
|
|
|
|
|
|
|
|
/// `<Message>.serialize()`.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This struct is ABI compatible with the equivalent struct on the C++ side. It
|
|
|
|
|
|
|
|
/// owns (and drops) its data.
|
|
|
|
|
|
|
|
// copybara:strip_begin
|
|
|
|
|
|
|
|
// LINT.IfChange
|
|
|
|
|
|
|
|
// copybara:strip_end
|
|
|
|
|
|
|
|
#[repr(C)] |
|
|
|
|
|
|
|
pub struct SerializedData { |
|
|
|
|
|
|
|
/// Owns the memory.
|
|
|
|
|
|
|
|
data: NonNull<u8>, |
|
|
|
|
|
|
|
len: usize, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
// copybara:strip_begin
|
|
|
|
|
|
|
|
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/cpp_kernel/cpp_api.
|
|
|
|
|
|
|
|
// h) copybara:strip_end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl SerializedData { |
|
|
|
|
|
|
|
pub unsafe fn from_raw_parts(data: NonNull<u8>, len: usize) -> Self { |
|
|
|
|
|
|
|
Self { data, len } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Deref for SerializedData { |
|
|
|
|
|
|
|
type Target = [u8]; |
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target { |
|
|
|
|
|
|
|
unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Drop for SerializedData { |
|
|
|
|
|
|
|
fn drop(&mut self) { |
|
|
|
|
|
|
|
unsafe { |
|
|
|
|
|
|
|
alloc::dealloc(self.data.as_ptr(), Layout::array::<u8>(self.len).unwrap()); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl fmt::Debug for SerializedData { |
|
|
|
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
|
|
|
|
|
|
fmt::Debug::fmt(self.deref(), f) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[cfg(test)] |
|
|
|
#[cfg(test)] |
|
|
|
mod tests { |
|
|
|
mod tests { |
|
|
|
use super::*; |
|
|
|
use super::*; |
|
|
|
use std::boxed::Box; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// We need to allocate the byte array so SerializedData can own it and
|
|
|
|
// We need to allocate the byte array so SerializedData can own it and
|
|
|
|
// deallocate it in its drop. This function makes it easier to do so for our
|
|
|
|
// deallocate it in its drop. This function makes it easier to do so for our
|
|
|
|