Remove Opaque Ptr macro: define_opaque_nonnulls

We still retain RawMessage and RawArena, but drop the macro for readability.

In lieu of invoking define_opaque_nonnulls, we spell out RawMessage and RawArena by hand.

PiperOrigin-RevId: 559906043
pull/13679/head
Hong Shin 2 years ago committed by Copybara-Service
parent 836fb65bb7
commit 5c33ed0855
  1. 46
      rust/internal.rs
  2. 39
      rust/macros.rs

@ -32,22 +32,54 @@
//! exposed to through the `protobuf` path but must be public for use by
//! generated code.
use crate::macros::define_opaque_nonnulls;
pub use crate::vtable::{
new_vtable_field_entry, BytesMutVTable, BytesOptionalMutVTable, RawVTableMutator,
};
use std::ptr::NonNull;
use std::slice;
/// Used to protect internal-only items from being used accidentally.
pub struct Private;
define_opaque_nonnulls!(
/// A raw pointer to the underlying arena for this runtime.
pub type RawArena = NonNull<RawArenaData>;
/// Defines a set of opaque, unique, non-accessible pointees.
///
/// The [Rustonomicon][nomicon] currently recommends a zero-sized struct,
/// though this should use [`extern type`] when that is stabilized.
/// [nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
/// [`extern type`]: https://github.com/rust-lang/rust/issues/43467
mod _opaque_pointees {
/// Opaque pointee for [`RawMessage`]
///
/// This type is not meant to be dereferenced in Rust code.
/// It is only meant to provide type safety for raw pointers
/// which are manipulated behind FFI.
///
/// [`RawMessage`]: super::RawMessage
#[repr(C)]
pub struct RawMessageData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
/// Opaque pointee for [`RawArena`]
///
/// This type is not meant to be dereferenced in Rust code.
/// It is only meant to provide type safety for raw pointers
/// which are manipulated behind FFI.
///
/// [`RawArena`]: super::RawArena
#[repr(C)]
pub struct RawArenaData {
_data: [u8; 0],
_marker: std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
}
/// A raw pointer to the underlying message for this runtime.
pub type RawMessage = NonNull<_opaque_pointees::RawMessageData>;
/// A raw pointer to the underlying message for this runtime.
pub type RawMessage = NonNull<RawMessageData>;
);
/// A raw pointer to the underlying arena for this runtime.
pub type RawArena = NonNull<_opaque_pointees::RawArenaData>;
/// Represents an ABI-stable version of `NonNull<[u8]>`/`string_view` (a
/// borrowed slice of bytes) for FFI use only.

@ -30,45 +30,6 @@
//! Runtime-internal macros
/// Defines a set of opaque pointers and a unique non-accessible pointees.
///
/// This provides a type safety benefit over using `NonNull<u8>` everywhere.
/// The [Rustonomicon][nomicon] currently recommends a zero-sized struct,
/// though this should use [`extern type`] when that is stabilized.
///
/// Because this defines a new private module, it can only be called once per
/// module.
///
/// [nomicon]: https://doc.rust-lang.org/nomicon/ffi.html#representing-opaque-structs
/// [`extern type`]: https://github.com/rust-lang/rust/issues/43467
macro_rules! define_opaque_nonnulls {
($($(#[$meta:meta])* $vis:vis type $name:ident = NonNull<$raw_name:ident>;)*) => {
mod _opaque_pointees {
$(
#[doc = concat!("Opaque pointee for [`", stringify!($name), "`][pointer]")]
///
/// This type is not meant to be dereferenced in Rust code.
/// It is only meant to provide type safety for raw pointers
/// which are manipulated behind FFI.
#[doc = concat!("[pointer]: super::", stringify!($name))]
#[repr(C)]
pub struct $raw_name {
_data: [u8; 0],
_marker: ::std::marker::PhantomData<(*mut u8, ::std::marker::PhantomPinned)>,
}
)*
}
$(
$(#[$meta])*
///
/// This is an opaque pointer used for FFI:
/// do not dereference this type in Rust code.
$vis type $name = ::std::ptr::NonNull<_opaque_pointees::$raw_name>;
)*
};
}
pub(crate) use define_opaque_nonnulls;
/// Defines a `impl SettableValue<$proxied> for SomeType` body that forwards to
/// another implementation.
///

Loading…
Cancel
Save