@ -7,11 +7,10 @@
use crate ::__internal ::Private ;
use crate ::__runtime ::{
copy_bytes_in_arena_if_needed_by_runtime , InnerPrimitiveMut , MutatorMessageRef , PtrAndLen ,
RawMessage ,
copy_bytes_in_arena_if_needed_by_runtime , MutatorMessageRef , PtrAndLen , RawMessage ,
} ;
use crate ::{
AbsentField , FieldEntry , Mut , MutProxied , MutProxy , Optional , PresentField , PrimitiveMut ,
AbsentField , FieldEntry , Mut , MutProxied , MutProxy , Optional , PresentField ,
ProxiedWithPresence , View , ViewProxy ,
} ;
use std ::fmt ::{ self , Debug } ;
@ -281,47 +280,6 @@ impl ProxiedWithRawOptionalVTable for [u8] {
}
}
/// A generic thunk vtable for mutating a present primitive field.
#[ doc(hidden) ]
#[ derive(Debug) ]
pub struct PrimitiveVTable < T > {
pub ( crate ) setter : unsafe extern "C" fn ( msg : RawMessage , val : T ) ,
pub ( crate ) getter : unsafe extern "C" fn ( msg : RawMessage ) -> T ,
}
#[ doc(hidden) ]
#[ derive(Debug) ]
/// A generic thunk vtable for mutating an `optional` primitive field.
pub struct PrimitiveOptionalMutVTable < T > {
pub ( crate ) base : PrimitiveVTable < T > ,
pub ( crate ) clearer : unsafe extern "C" fn ( msg : RawMessage ) ,
pub ( crate ) default : T ,
}
impl < T > PrimitiveVTable < T > {
#[ doc(hidden) ]
pub const fn new (
_private : Private ,
getter : unsafe extern "C" fn ( msg : RawMessage ) -> T ,
setter : unsafe extern "C" fn ( msg : RawMessage , val : T ) ,
) -> Self {
Self { getter , setter }
}
}
impl < T > PrimitiveOptionalMutVTable < T > {
#[ doc(hidden) ]
pub const fn new (
_private : Private ,
getter : unsafe extern "C" fn ( msg : RawMessage ) -> T ,
setter : unsafe extern "C" fn ( msg : RawMessage , val : T ) ,
clearer : unsafe extern "C" fn ( msg : RawMessage ) ,
default : T ,
) -> Self {
Self { base : PrimitiveVTable { getter , setter } , clearer , default }
}
}
/// A generic thunk vtable for mutating a present `bytes` or `string` field.
#[ doc(hidden) ]
#[ derive(Debug) ]
@ -425,92 +383,3 @@ impl<'msg> RawVTableOptionalMutatorData<'msg, [u8]> {
self
}
}
/// Primitive types using a vtable for message access that are trivial to copy
/// and have a `'static` lifetime.
///
/// Implementing this trait automatically implements `ProxiedWithRawVTable`,
/// `ProxiedWithRawOptionalVTable`, and get/set/clear methods on
/// `RawVTableMutator` and `RawVTableOptionalMutatorData` that use the vtable.
///
/// It doesn't implement `Proxied`, `ViewProxy`, `SettableValue` or
/// `ProxiedWithPresence` for `Self` to avoid future conflicting blanket impls
/// on those traits.
pub trait PrimitiveWithRawVTable :
Copy
+ Debug
+ ' static
+ ProxiedWithPresence
+ Sync
+ Send
+ for < ' msg > MutProxied < View < ' msg > = Self , Mut < ' msg > = PrimitiveMut < ' msg , Self > >
{
}
impl < T : PrimitiveWithRawVTable > ProxiedWithRawVTable for T {
type VTable = PrimitiveVTable < T > ;
fn make_view ( _private : Private , mut_inner : InnerPrimitiveMut < ' _ , Self > ) -> Self {
mut_inner . get ( )
}
fn make_mut ( _private : Private , inner : InnerPrimitiveMut < ' _ , Self > ) -> PrimitiveMut < ' _ , Self > {
// SAFETY: `inner` is valid for the necessary lifetime and `T` as promised by
// the caller of `InnerPrimitiveMut::new`.
unsafe { PrimitiveMut ::from_inner ( Private , inner ) }
}
}
impl < T : PrimitiveWithRawVTable > ProxiedWithRawOptionalVTable for T {
type OptionalVTable = PrimitiveOptionalMutVTable < T > ;
fn upcast_vtable (
_private : Private ,
optional_vtable : & ' static Self ::OptionalVTable ,
) -> & ' static Self ::VTable {
& optional_vtable . base
}
}
impl < T : PrimitiveWithRawVTable > RawVTableMutator < ' _ , T > {
pub ( crate ) fn get ( self ) -> T {
// SAFETY:
// - `msg_ref` is valid for the lifetime of `RawVTableMutator` as promised by
// the caller of `new`.
unsafe { ( self . vtable ( ) . getter ) ( self . msg_ref . msg ( ) ) }
}
/// # Safety
/// - `msg_ref` must be valid for the lifetime of `RawVTableMutator`.
pub ( crate ) unsafe fn set ( self , val : T ) {
// SAFETY:
// - `msg_ref` is valid for the lifetime of `RawVTableMutator` as promised by
// the caller of `new`.
unsafe { ( self . vtable ( ) . setter ) ( self . msg_ref . msg ( ) , val ) }
}
}
impl < ' msg , T : PrimitiveWithRawVTable > RawVTableOptionalMutatorData < ' msg , T > {
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 ( private , self . optional_vtable ( ) . default )
}
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`.
unsafe { ( self . optional_vtable ( ) . base . setter ) ( self . msg_ref . msg ( ) , val ) }
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`.
unsafe { ( self . optional_vtable ( ) . clearer ) ( self . msg_ref . msg ( ) ) }
self
}
}