Struct abi_stable::prefix_type::PrefixRef
source · pub struct PrefixRef<P> { /* private fields */ }
Expand description
A reference to a prefix type.
This is the type that all *_Ref
pointer types generated by StableAbi
wrap.
§Example
use abi_stable::{
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
staticref, StableAbi,
};
fn main() {
// `Module_Ref`'s constructor can also be called at compile-time
asserts(Module_Ref(PREFIX_A));
asserts(Module_Ref(PREFIX_B));
}
fn asserts(module: Module_Ref) {
assert_eq!(module.first(), 5);
assert_eq!(module.second(), 8);
// If this Module_Ref had come from a previous version of the library without a
// `third` field it would return `None`.
assert_eq!(module.third(), Some(13));
}
#[repr(C)]
#[derive(StableAbi)]
#[sabi(kind(Prefix(prefix_ref = Module_Ref, prefix_fields = Module_Prefix)))]
struct Module {
first: usize,
// The `#[sabi(last_prefix_field)]` attribute here means that this is
// the last field in this struct that was defined in the
// first compatible version of the library,
// requiring new fields to always be added after it.
// Moving this attribute is a breaking change, it can only be done in a
// major version bump..
#[sabi(last_prefix_field)]
second: usize,
third: usize,
}
const MOD_VAL: Module = Module {
first: 5,
second: 8,
third: 13,
};
/////////////////////////////////////////
// First way to construct a PrefixRef
// This is a way that PrefixRef can be constructed in statics
const PREFIX_A: PrefixRef<Module_Prefix> = {
const S: &WithMetadata<Module> = &WithMetadata::new(MOD_VAL);
S.static_as_prefix()
};
/////////////////////////////////////////
// Second way to construct a PrefixRef
// This is a way that PrefixRef can be constructed in associated constants,
struct WithAssoc;
impl WithAssoc {
// This macro declares a `StaticRef` pointing to the assigned `WithMetadata`.
staticref!{const MOD_WM: WithMetadata<Module> = WithMetadata::new(MOD_VAL)}
}
const PREFIX_B: PrefixRef<Module_Prefix> = WithAssoc::MOD_WM.as_prefix();
/////////////////////////////////////////
Implementations§
source§impl<P> PrefixRef<P>
impl<P> PrefixRef<P>
sourcepub const unsafe fn from_raw<T>(ptr: *const WithMetadata_<T, P>) -> Self
pub const unsafe fn from_raw<T>(ptr: *const WithMetadata_<T, P>) -> Self
Constructs a PrefixRef
from a raw pointer.
§Safety
The pointer must be a non-dangling pointer to a valid, initialized instance of T
,
and live for the rest of the program’s lifetime
(if called at compile-time it means live for the entire program).
T
must implement PrefixTypeTrait<Fields = P>
,
this is automatically true if this is called with
&WithMetadata::new(<value>)
.
§Example
use abi_stable::{
for_examples::{Module, Module_Prefix, Module_Ref},
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
rstr,
std_types::*,
};
const MOD_WM: &WithMetadata<Module> = {
&WithMetadata::new(
Module {
first: RSome(3),
second: rstr!("hello"),
third: 8,
},
)
};
const PREFIX: PrefixRef<Module_Prefix> = unsafe { PrefixRef::from_raw(MOD_WM) };
const MODULE: Module_Ref = Module_Ref(PREFIX);
assert_eq!(MODULE.first(), RSome(3));
assert_eq!(MODULE.second().as_str(), "hello");
// The accessor returns an `Option` because the field comes after the prefix,
// and returning an Option is the default for those.
assert_eq!(MODULE.third(), Some(8));
sourcepub const fn from_staticref<T>(ptr: StaticRef<WithMetadata_<T, P>>) -> Self
pub const fn from_staticref<T>(ptr: StaticRef<WithMetadata_<T, P>>) -> Self
Constructs a PrefixRef
from a StaticRef
.
§Example
use abi_stable::{
for_examples::{Module, Module_Prefix, Module_Ref},
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
rstr, staticref,
std_types::*,
};
struct Foo {}
impl Foo {
// This macro declares a `StaticRef` pointing to the assigned `WithMetadata`.
staticref! {const MOD_WM: WithMetadata<Module> =
WithMetadata::new(Module{
first: RNone,
second: rstr!("world"),
third: 13,
})
}
}
const PREFIX: PrefixRef<Module_Prefix> = PrefixRef::from_staticref(Foo::MOD_WM);
const MODULE: Module_Ref = Module_Ref(PREFIX);
assert_eq!(MODULE.first(), RNone);
assert_eq!(MODULE.second().as_str(), "world");
// The accessor returns an `Option` because the field comes after the prefix,
// and returning an Option is the default for those.
assert_eq!(MODULE.third(), Some(13));
sourcepub const fn from_ref<T>(ptr: &'static WithMetadata_<T, P>) -> Self
pub const fn from_ref<T>(ptr: &'static WithMetadata_<T, P>) -> Self
Constructs a PrefixRef
from a static reference.
§Example
use abi_stable::{
for_examples::{Module, Module_Prefix, Module_Ref},
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
rstr,
std_types::*,
};
const MOD_WM: &WithMetadata<Module> = {
&WithMetadata::new(
Module {
first: RNone,
second: rstr!("foo"),
third: 21,
},
)
};
const PREFIX: PrefixRef<Module_Prefix> = PrefixRef::from_ref(MOD_WM);
const MODULE: Module_Ref = Module_Ref(PREFIX);
assert_eq!(MODULE.first(), RNone);
assert_eq!(MODULE.second().as_str(), "foo");
// The accessor returns an `Option` because the field comes after the prefix,
// and returning an Option is the default for those.
assert_eq!(MODULE.third(), Some(21));
sourcepub const fn field_accessibility(&self) -> FieldAccessibility
pub const fn field_accessibility(&self) -> FieldAccessibility
A bit array that describes the accessibility of each field in P
.
§Example
use abi_stable::{
for_examples::{Module, Module_Prefix},
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
std_types::*,
};
const MOD_WM: &WithMetadata<Module> = {
&WithMetadata::new(
Module {
first: RNone,
second: RStr::empty(),
third: 0,
},
)
};
const PREFIX: PrefixRef<Module_Prefix> = PrefixRef::from_ref(MOD_WM);
let accessibility = PREFIX.field_accessibility();
assert!(accessibility.at(0).is_accessible()); // The `first` field
assert!(accessibility.at(1).is_accessible()); // The `second` field
assert!(accessibility.at(2).is_accessible()); // The `third` field
assert!(!accessibility.at(3).is_accessible()); // There's no field after `third`
sourcepub const fn type_layout(&self) -> &'static PTStructLayout
pub const fn type_layout(&self) -> &'static PTStructLayout
The basic layout of the prefix type, for error messages.
sourcepub const fn prefix<'a>(self) -> &'a P
pub const fn prefix<'a>(self) -> &'a P
Gets a reference to the pointed-to prefix.
§Example
use abi_stable::{
for_examples::{Module, Module_Prefix},
prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
rstr,
std_types::*,
};
const MOD_WM: &WithMetadata<Module> = {
&WithMetadata::new(
Module {
first: RNone,
second: rstr!("foo"),
third: 21,
},
)
};
const PREFIX_REF: PrefixRef<Module_Prefix> = PrefixRef::from_ref(MOD_WM);
let prefix: &Module_Prefix = PREFIX_REF.prefix();
assert_eq!(prefix.first, RNone);
assert_eq!(prefix.second.as_str(), "foo");
// The `third` field is not in the prefix, so it can't be accessed here.
// prefix.third;
sourcepub const fn to_raw_ptr(self) -> *const WithMetadata_<P, P>
pub const fn to_raw_ptr(self) -> *const WithMetadata_<P, P>
Converts this PrefixRef into a raw pointer.
sourcepub const unsafe fn cast<U>(self) -> PrefixRef<U>
pub const unsafe fn cast<U>(self) -> PrefixRef<U>
Casts the pointed-to prefix to another type.
§Safety
This function is intended for casting the PrefixRef<P>
to PrefixRef<U>
,
and then cast back to PrefixRef<P>
to use it again.
The prefix in the returned PrefixRef<U>
must only be accessed
when this PrefixRef
was originally cosntructed with a ẀithMetadata_<_, U>
.
access includes calling prefix
, and reading the value
field in the WithMetadata
that this points to.
Trait Implementations§
source§impl<P> GetPointerKind for PrefixRef<P>
impl<P> GetPointerKind for PrefixRef<P>
source§impl<P> GetStaticEquivalent_ for PrefixRef<P>where
P: GetStaticEquivalent_,
impl<P> GetStaticEquivalent_ for PrefixRef<P>where
P: GetStaticEquivalent_,
source§type StaticEquivalent = PrefixRef<<P as GetStaticEquivalent_>::StaticEquivalent>
type StaticEquivalent = PrefixRef<<P as GetStaticEquivalent_>::StaticEquivalent>
'static
equivalent of Self
source§impl<P> PrefixRefTrait for PrefixRef<P>
impl<P> PrefixRefTrait for PrefixRef<P>
source§type PrefixFields = P
type PrefixFields = P
#[sabi(last_prefix_field)]
inclusive. Read moresource§fn from_prefix_ref(this: PrefixRef<Self::PrefixFields>) -> Self
fn from_prefix_ref(this: PrefixRef<Self::PrefixFields>) -> Self
PrefixRef
to Self
source§fn to_prefix_ref(self) -> PrefixRef<Self::PrefixFields>
fn to_prefix_ref(self) -> PrefixRef<Self::PrefixFields>
Self
to a PrefixRef
source§impl<P> StableAbi for PrefixRef<P>where
P: PrefixStableAbi,
impl<P> StableAbi for PrefixRef<P>where
P: PrefixStableAbi,
source§type IsNonZeroType = True
type IsNonZeroType = True
source§const LAYOUT: &'static TypeLayout = _
const LAYOUT: &'static TypeLayout = _
source§const ABI_CONSTS: AbiConsts = _
const ABI_CONSTS: AbiConsts = _
const
-equivalents of the associated types.impl<P> Copy for PrefixRef<P>
impl<'a, P: 'a> Send for PrefixRef<P>where
&'a WithMetadata_<P, P>: Send,
impl<'a, P: 'a> Sync for PrefixRef<P>where
&'a WithMetadata_<P, P>: Sync,
Auto Trait Implementations§
impl<P> Freeze for PrefixRef<P>
impl<P> RefUnwindSafe for PrefixRef<P>where
P: RefUnwindSafe,
impl<P> Unpin for PrefixRef<P>
impl<P> UnwindSafe for PrefixRef<P>where
P: RefUnwindSafe,
Blanket Implementations§
source§impl<T> AlignerFor<1> for T
impl<T> AlignerFor<1> for T
source§impl<T> AlignerFor<1024> for T
impl<T> AlignerFor<1024> for T
source§type Aligner = AlignTo1024<T>
type Aligner = AlignTo1024<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<128> for T
impl<T> AlignerFor<128> for T
source§type Aligner = AlignTo128<T>
type Aligner = AlignTo128<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<16> for T
impl<T> AlignerFor<16> for T
source§impl<T> AlignerFor<16384> for T
impl<T> AlignerFor<16384> for T
source§type Aligner = AlignTo16384<T>
type Aligner = AlignTo16384<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<2> for T
impl<T> AlignerFor<2> for T
source§impl<T> AlignerFor<2048> for T
impl<T> AlignerFor<2048> for T
source§type Aligner = AlignTo2048<T>
type Aligner = AlignTo2048<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<256> for T
impl<T> AlignerFor<256> for T
source§type Aligner = AlignTo256<T>
type Aligner = AlignTo256<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<32> for T
impl<T> AlignerFor<32> for T
source§impl<T> AlignerFor<32768> for T
impl<T> AlignerFor<32768> for T
source§type Aligner = AlignTo32768<T>
type Aligner = AlignTo32768<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<4> for T
impl<T> AlignerFor<4> for T
source§impl<T> AlignerFor<4096> for T
impl<T> AlignerFor<4096> for T
source§type Aligner = AlignTo4096<T>
type Aligner = AlignTo4096<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<512> for T
impl<T> AlignerFor<512> for T
source§type Aligner = AlignTo512<T>
type Aligner = AlignTo512<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> AlignerFor<64> for T
impl<T> AlignerFor<64> for T
source§impl<T> AlignerFor<8> for T
impl<T> AlignerFor<8> for T
source§impl<T> AlignerFor<8192> for T
impl<T> AlignerFor<8192> for T
source§type Aligner = AlignTo8192<T>
type Aligner = AlignTo8192<T>
AlignTo*
type which aligns Self
to ALIGNMENT
.source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> ImmutableRef for T
impl<T> ImmutableRef for T
source§impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
impl<'a, T> RCowCompatibleRef<'a> for Twhere
T: Clone + 'a,
source§fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
fn as_c_ref(from: &'a T) -> <T as RCowCompatibleRef<'a>>::RefC
source§fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
fn as_rust_ref(from: <T as RCowCompatibleRef<'a>>::RefC) -> &'a T
source§impl<S> ROExtAcc for S
impl<S> ROExtAcc for S
source§fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
fn f_get<F>(&self, offset: FieldOffset<S, F, Aligned>) -> &F
offset
. Read moresource§fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
fn f_get_mut<F>(&mut self, offset: FieldOffset<S, F, Aligned>) -> &mut F
offset
. Read moresource§fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
fn f_get_ptr<F, A>(&self, offset: FieldOffset<S, F, A>) -> *const F
offset
. Read moresource§fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
fn f_get_mut_ptr<F, A>(&mut self, offset: FieldOffset<S, F, A>) -> *mut F
offset
. Read moresource§impl<S> ROExtOps<Aligned> for S
impl<S> ROExtOps<Aligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Aligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Aligned>) -> Fwhere
F: Copy,
source§impl<S> ROExtOps<Unaligned> for S
impl<S> ROExtOps<Unaligned> for S
source§fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
fn f_replace<F>(&mut self, offset: FieldOffset<S, F, Unaligned>, value: F) -> F
offset
) with value
,
returning the previous value of the field. Read moresource§fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
fn f_get_copy<F>(&self, offset: FieldOffset<S, F, Unaligned>) -> Fwhere
F: Copy,
source§impl<T> SelfOps for Twhere
T: ?Sized,
impl<T> SelfOps for Twhere
T: ?Sized,
source§fn piped<F, U>(self, f: F) -> U
fn piped<F, U>(self, f: F) -> U
source§fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
fn piped_ref<'a, F, U>(&'a self, f: F) -> Uwhere
F: FnOnce(&'a Self) -> U,
piped
except that the function takes &Self
Useful for functions that take &Self
instead of Self
. Read moresource§fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
fn piped_mut<'a, F, U>(&'a mut self, f: F) -> Uwhere
F: FnOnce(&'a mut Self) -> U,
piped
, except that the function takes &mut Self
.
Useful for functions that take &mut Self
instead of Self
.source§fn mutated<F>(self, f: F) -> Self
fn mutated<F>(self, f: F) -> Self
source§fn observe<F>(self, f: F) -> Self
fn observe<F>(self, f: F) -> Self
source§fn as_ref_<T>(&self) -> &T
fn as_ref_<T>(&self) -> &T
AsRef
,
using the turbofish .as_ref_::<_>()
syntax. Read more