[−][src]Struct abi_stable::prefix_type::PrefixRef
A reference to a prefix type.
This is the type that all *_Ref
pointer types generated by StableAbi
wrap.
Example
use abi_stable::{ StableAbi, staticref, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, }; 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> = { WithMetadata::new(PrefixTypeTrait::METADATA, MOD_VAL) .static_as_prefix() }; ///////////////////////////////////////// // Second way to construct a PrefixRef // This is a way that PrefixRef can be constructed in associated constants, // This macro declares a `StaticRef` pointing to the assigned `WithMetadata`. staticref!(const MOD_WM: WithMetadata<Module> = { WithMetadata::new(PrefixTypeTrait::METADATA, MOD_VAL) }); const PREFIX_B: PrefixRef<Module_Prefix> = MOD_WM.as_prefix(); /////////////////////////////////////////
Implementations
impl<P> PrefixRef<P>
[src]
pub const unsafe fn from_raw<T>(ptr: *const WithMetadata_<T, P>) -> Self
[src]
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(PrefixTypeTrait::METADATA, <value>)
.
Example
use abi_stable::{ for_examples::{Module, Module_Prefix, Module_Ref}, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, std_types::*, rstr, }; const MOD_WM: *const WithMetadata<Module> = { &WithMetadata::new(PrefixTypeTrait::METADATA, 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));
pub const fn from_staticref<T>(ptr: StaticRef<WithMetadata_<T, P>>) -> Self
[src]
Constructs a PrefixRef
from a StaticRef
.
Example
use abi_stable::{ for_examples::{Module, Module_Prefix, Module_Ref}, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, std_types::*, rstr, staticref, }; // This macro invocation declares a `StaticRef<WithMetadata<Module>>` constant staticref!(const MOD_WM: WithMetadata<Module> = { WithMetadata::new(PrefixTypeTrait::METADATA, Module{ first: RNone, second: rstr!("world"), third: 13, }) }); const PREFIX: PrefixRef<Module_Prefix> = PrefixRef::from_staticref(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));
pub const fn from_ref<T>(ptr: &'static WithMetadata_<T, P>) -> Self
[src]
Constructs a PrefixRef
from a static reference.
Example
use abi_stable::{ for_examples::{Module, Module_Prefix, Module_Ref}, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, std_types::*, rstr, }; const MOD_WM: &WithMetadata<Module> = { &WithMetadata::new(PrefixTypeTrait::METADATA, 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));
pub fn metadata(self) -> PrefixMetadata<P, P>
[src]
Gets the metadata about the prefix type, including available fields.
Example
use abi_stable::{ for_examples::{Module, Module_Prefix}, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, std_types::*, }; const MOD_WM: &WithMetadata<Module> = { &WithMetadata::new(PrefixTypeTrait::METADATA, Module{ first: RNone, second: RStr::empty(), third: 0, }) }; const PREFIX: PrefixRef<Module_Prefix> = PrefixRef::from_ref(MOD_WM); let accessibility = PREFIX.metadata().field_accessibility(); assert!( accessibility.is_accessible(0) ); // The `first` field assert!( accessibility.is_accessible(1) ); // The `second` field assert!( accessibility.is_accessible(2) ); // The `third` field assert!( !accessibility.is_accessible(3) ); // There's no field after `third`
pub fn prefix<'a>(self) -> &'a P
[src]
Gets a reference to the pointed-to prefix.
Example
use abi_stable::{ for_examples::{Module, Module_Prefix}, prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata}, std_types::*, rstr, }; const MOD_WM: &WithMetadata<Module> = { &WithMetadata::new(PrefixTypeTrait::METADATA, 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;
pub fn to_raw_ptr(self) -> *const WithMetadata_<P, P>
[src]
Converts this PrefixRef into a raw pointer.
pub const fn const_to_raw_ptr(self) -> *const WithMetadata_<P, P>
[src]
A const-callable version of to_raw_ptr
,
use to_raw_ptr
in non-const code instead of this.
to_raw_ptr
exists for efficiency-in-debug-build reasons.
pub const unsafe fn cast<U>(self) -> PrefixRef<U>
[src]
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
impl<P> Clone for PrefixRef<P>
[src]
pub fn clone(&self) -> Self
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<P> Copy for PrefixRef<P>
[src]
impl<P> Debug for PrefixRef<P>
[src]
impl<P> GetStaticEquivalent_ for PrefixRef<P> where
P: GetStaticEquivalent_,
[src]
P: GetStaticEquivalent_,
type StaticEquivalent = PrefixRef<GetStaticEquivalent<P>>
impl<P> ImmutableRef for PrefixRef<P>
[src]
type Target = WithMetadata_<P, P>
The referent of the pointer, what it points to.
pub const TARGET: ImmutableRefTarget<Self, Self::Target>
[src]
pub fn to_nonnull(self) -> NonNull<Self::Target>
[src]
pub unsafe fn from_nonnull(from: NonNull<Self::Target>) -> Self
[src]
pub fn to_raw_ptr(self) -> *const Self::Target
[src]
pub unsafe fn from_raw_ptr(from: *const Self::Target) -> Option<Self>
[src]
impl<P> PrefixRefTrait for PrefixRef<P>
[src]
type PrefixFields = P
A struct that contains all the fields of some other struct
up to the field annotated with
#[sabi(last_prefix_field)]
inclusive. Read more
pub const PREFIX_FIELDS: PointsToPrefixFields<Self, Self::PrefixFields>
[src]
pub fn from_prefix_ref(this: PrefixRef<Self::PrefixFields>) -> Self
[src]
pub fn to_prefix_ref(self) -> PrefixRef<Self::PrefixFields>
[src]
impl<'a, P: 'a> Send for PrefixRef<P> where
&'a WithMetadata_<P, P>: Send,
[src]
&'a WithMetadata_<P, P>: Send,
impl<P> StableAbi for PrefixRef<P> where
P: PrefixStableAbi,
[src]
P: PrefixStableAbi,
type IsNonZeroType = True
Whether this type has a single invalid bit-pattern. Read more
pub const LAYOUT: &'static TypeLayout
[src]
pub const ABI_CONSTS: AbiConsts
[src]
impl<'a, P: 'a> Sync for PrefixRef<P> where
&'a WithMetadata_<P, P>: Sync,
[src]
&'a WithMetadata_<P, P>: Sync,
Auto Trait Implementations
impl<P> RefUnwindSafe for PrefixRef<P> where
P: RefUnwindSafe,
P: RefUnwindSafe,
impl<P> Unpin for PrefixRef<P>
impl<P> UnwindSafe for PrefixRef<P> where
P: RefUnwindSafe,
P: RefUnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<'a, T> BorrowOwned<'a> for T where
T: 'a + Clone,
[src]
T: 'a + Clone,
type ROwned = T
The owned type, stored in RCow::Owned
type RBorrowed = &'a T
The borrowed type, stored in RCow::Borrowed
pub fn r_borrow(
&'a <T as BorrowOwned<'a>>::ROwned
) -> <T as BorrowOwned<'a>>::RBorrowed
[src]
&'a <T as BorrowOwned<'a>>::ROwned
) -> <T as BorrowOwned<'a>>::RBorrowed
pub fn r_to_owned(
<T as BorrowOwned<'a>>::RBorrowed
) -> <T as BorrowOwned<'a>>::ROwned
[src]
<T as BorrowOwned<'a>>::RBorrowed
) -> <T as BorrowOwned<'a>>::ROwned
pub fn deref_borrowed(&<T as BorrowOwned<'a>>::RBorrowed) -> &T
[src]
pub fn deref_owned(&<T as BorrowOwned<'a>>::ROwned) -> &T
[src]
pub fn from_cow_borrow(&'a T) -> <T as BorrowOwned<'a>>::RBorrowed
[src]
pub fn from_cow_owned(<T as ToOwned>::Owned) -> <T as BorrowOwned<'a>>::ROwned
[src]
pub fn into_cow_borrow(<T as BorrowOwned<'a>>::RBorrowed) -> &'a T
[src]
pub fn into_cow_owned(<T as BorrowOwned<'a>>::ROwned) -> <T as ToOwned>::Owned
[src]
impl<T> From<T> for T
[src]
impl<T> GetWithMetadata for T
[src]
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> SelfOps for T where
T: ?Sized,
[src]
T: ?Sized,
pub const T: PhantomData<fn() -> Self>
[src]
pub const T_D: PhantomData<Self>
[src]
pub fn assert_ty(self, _other: PhantomData<fn() -> Self>) -> Self
[src]
pub fn assert_ty_ref(&self, _other: PhantomData<fn() -> Self>) -> &Self
[src]
pub fn assert_ty_mut(&mut self, _other: PhantomData<fn() -> Self>) -> &mut Self
[src]
pub fn ty_(&self) -> PhantomData<fn() -> Self>
[src]
pub fn ty_d(&self) -> PhantomData<Self>
[src]
pub fn ty_inv(&self) -> PhantomData<fn(Self) -> Self>
[src]
pub fn ty_inv_ref(&self) -> PhantomData<Cell<&Self>>
[src]
pub fn eq_id(&self, other: &Self) -> bool
[src]
pub fn piped<F, U>(self, f: F) -> U where
F: FnOnce(Self) -> U,
[src]
F: FnOnce(Self) -> U,
pub fn piped_ref<'a, F, U>(&'a self, f: F) -> U where
F: FnOnce(&'a Self) -> U,
[src]
F: FnOnce(&'a Self) -> U,
pub fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U where
F: FnOnce(&'a mut Self) -> U,
[src]
F: FnOnce(&'a mut Self) -> U,
pub fn mutated<F>(self, f: F) -> Self where
F: FnOnce(&mut Self),
[src]
F: FnOnce(&mut Self),
pub fn observe<F>(self, f: F) -> Self where
F: FnOnce(&Self),
[src]
F: FnOnce(&Self),
pub fn into_<T>(self, PhantomData<fn() -> T>) -> T where
Self: Into<T>,
[src]
Self: Into<T>,
pub fn as_ref_<T>(&self) -> &T where
Self: AsRef<T>,
T: ?Sized,
[src]
Self: AsRef<T>,
T: ?Sized,
pub fn as_mut_<T>(&mut self) -> &mut T where
Self: AsMut<T>,
T: ?Sized,
[src]
Self: AsMut<T>,
T: ?Sized,
pub fn drop_(self)
[src]
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<This> TransmuteElement for This where
This: ?Sized,
[src]
This: ?Sized,
pub unsafe fn transmute_element<T>(self) -> Self::TransmutedPtr where
Self: CanTransmuteElement<T>,
Self::Target: Sized,
[src]
Self: CanTransmuteElement<T>,
Self::Target: Sized,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The error type returned when the conversion fails.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> TypeIdentity for T where
T: ?Sized,
[src]
T: ?Sized,
type Type = T
The same type as Self. Read more
pub fn into_type_val(self) -> Self::Type where
Self::Type: Sized,
[src]
Self::Type: Sized,
pub fn into_type_ref(&self) -> &Self::Type
[src]
pub fn into_type_mut(&mut self) -> &mut Self::Type
[src]
pub fn into_type_box(self: Box<Self, Global>) -> Box<Self::Type, Global>
[src]
pub fn into_type_arc(this: Arc<Self>) -> Arc<Self::Type>
[src]
pub fn into_type_rc(this: Rc<Self>) -> Rc<Self::Type>
[src]
pub fn from_type_val(this: Self::Type) -> Self where
Self::Type: Sized,
[src]
Self::Type: Sized,