Expose primitive internals just enough for enums

PiperOrigin-RevId: 591933966
pull/15118/head
Alyssa Haroldsen 1 year ago committed by Copybara-Service
parent bacf61a3ef
commit 9e9c727881
  1. 2
      rust/internal.rs
  2. 28
      rust/primitive.rs
  3. 8
      rust/vtable.rs

@ -11,7 +11,7 @@
pub use crate::vtable::{
new_vtable_field_entry, BytesMutVTable, BytesOptionalMutVTable, PrimitiveOptionalMutVTable,
PrimitiveVTable, PrimitiveWithRawVTable, RawVTableMutator,
PrimitiveVTable, PrimitiveWithRawVTable, RawVTableMutator, RawVTableOptionalMutatorData,
};
use std::ptr::NonNull;
use std::slice;

@ -45,12 +45,20 @@ impl<'msg, T> PrimitiveMut<'msg, T>
where
T: PrimitiveWithRawVTable,
{
/// Gets the current value of the field.
pub fn get(&self) -> View<'_, T> {
T::make_view(Private, self.inner)
}
/// Sets a new value for the field.
pub fn set(&mut self, val: impl SettableValue<T>) {
MutProxy::set(self, val)
val.set_on(Private, self.as_mut())
}
#[doc(hidden)]
pub fn set_primitive(&mut self, _private: Private, value: T) {
// SAFETY: the raw mutator is valid for `'msg` as enforced by `Mut`
unsafe { self.inner.set(value) }
}
}
@ -106,9 +114,17 @@ macro_rules! impl_singular_primitives {
}
impl SettableValue<$t> for $t {
fn set_on<'msg>(self, _private: Private, mutator: Mut<'msg, $t>) where $t: 'msg {
// SAFETY: the raw mutator is valid for `'msg` as enforced by `Mut`
unsafe { mutator.inner.set(self) }
fn set_on<'msg>(self, private: Private, mut mutator: Mut<'msg, $t>) where $t: 'msg {
mutator.set_primitive(private, self)
}
fn set_on_absent(
self,
_private: Private,
absent_mutator: <$t as ProxiedWithPresence>::PresentMutData<'_>,
) -> <$t as ProxiedWithPresence>::AbsentMutData<'_>
{
absent_mutator.set(Private, self)
}
}
@ -119,13 +135,13 @@ macro_rules! impl_singular_primitives {
fn clear_present_field(
present_mutator: Self::PresentMutData<'_>,
) -> Self::AbsentMutData<'_> {
present_mutator.clear()
present_mutator.clear(Private)
}
fn set_absent_to_default(
absent_mutator: Self::AbsentMutData<'_>,
) -> Self::PresentMutData<'_> {
absent_mutator.set_absent_to_default()
absent_mutator.set_absent_to_default(Private)
}
}

@ -482,14 +482,14 @@ impl<T: PrimitiveWithRawVTable> RawVTableMutator<'_, T> {
}
impl<'msg, T: PrimitiveWithRawVTable> RawVTableOptionalMutatorData<'msg, T> {
pub(crate) fn set_absent_to_default(self) -> Self {
pub fn set_absent_to_default(self, private: Private) -> Self {
// SAFETY:
// - `msg_ref` is valid for the lifetime of `RawVTableOptionalMutatorData` as
// promised by the caller of `new`.
self.set(self.optional_vtable().default)
self.set(private, self.optional_vtable().default)
}
pub(crate) fn set(self, val: T) -> Self {
pub fn set(self, _private: Private, val: T) -> Self {
// SAFETY:
// - `msg_ref` is valid for the lifetime of `RawVTableOptionalMutatorData` as
// promised by the caller of `new`.
@ -497,7 +497,7 @@ impl<'msg, T: PrimitiveWithRawVTable> RawVTableOptionalMutatorData<'msg, T> {
self
}
pub(crate) fn clear(self) -> Self {
pub fn clear(self, _private: Private) -> Self {
// SAFETY:
// - `msg_ref` is valid for the lifetime of `RawVTableOptionalMutatorData` as
// promised by the caller of `new`.

Loading…
Cancel
Save