Fix AssociatedMiniTable to be an unsafe trait with a safe fn, instead of having an unsafe fn.

`unsafe trait` means that the _implementation_ has constraints they need to meet for the behavior to be safe (which is the intent here, namely that it should always return the same value and always be a safe to deref value), unsafe on the fn means that the _caller_ of that fn has some constraints they need to meet for it to be safe (of which there are none in this case).

PiperOrigin-RevId: 668028628
pull/17970/head
Protobuf Team Bot 7 months ago committed by Copybara-Service
parent 0056835a0d
commit 5cebdef929
  1. 15
      rust/upb/associated_mini_table.rs
  2. 12
      src/google/protobuf/compiler/rust/message.cc

@ -25,11 +25,12 @@ use super::upb_MiniTable;
/// an extern "C" we cannot do that without the unstable `const_refs_to_static`.
/// After that feature is stabilized (or if we move the MiniTable generation to
/// .rs) this will be switched.
pub trait AssociatedMiniTable {
/// SAFETY:
/// - The MiniTable pointer must be from Protobuf code generation and follow
/// the corresponding invariants associated with upb's C API (the pointer
/// should always have the same non-null value, the underlying pointee
/// should never be modified and should have 'static lifetime).
unsafe fn mini_table() -> *const upb_MiniTable;
///
/// SAFETY:
/// - The MiniTable pointer must be from Protobuf code generation and follow the
/// corresponding invariants associated with upb's C API (the pointer should
/// always have the same non-null value, the underlying pointee should never
/// be modified and should have 'static lifetime).
pub unsafe trait AssociatedMiniTable {
fn mini_table() -> *const upb_MiniTable;
}

@ -299,23 +299,23 @@ void IntoProxiedForMessage(Context& ctx, const Descriptor& msg) {
void UpbGeneratedMessageTraitImpls(Context& ctx, const Descriptor& msg) {
if (ctx.opts().kernel == Kernel::kUpb) {
ctx.Emit({{"minitable", UpbMiniTableName(msg)}}, R"rs(
impl $pbr$::AssociatedMiniTable for $Msg$ {
unsafe impl $pbr$::AssociatedMiniTable for $Msg$ {
#[inline(always)]
unsafe fn mini_table() -> *const $pbr$::upb_MiniTable {
fn mini_table() -> *const $pbr$::upb_MiniTable {
$std$::ptr::addr_of!($minitable$)
}
}
impl $pbr$::AssociatedMiniTable for $Msg$View<'_> {
unsafe impl $pbr$::AssociatedMiniTable for $Msg$View<'_> {
#[inline(always)]
unsafe fn mini_table() -> *const $pbr$::upb_MiniTable {
fn mini_table() -> *const $pbr$::upb_MiniTable {
$std$::ptr::addr_of!($minitable$)
}
}
impl $pbr$::AssociatedMiniTable for $Msg$Mut<'_> {
unsafe impl $pbr$::AssociatedMiniTable for $Msg$Mut<'_> {
#[inline(always)]
unsafe fn mini_table() -> *const $pbr$::upb_MiniTable {
fn mini_table() -> *const $pbr$::upb_MiniTable {
$std$::ptr::addr_of!($minitable$)
}
}

Loading…
Cancel
Save