[][src]Struct abi_stable::prefix_type::PrefixRef

#[repr(transparent)]pub struct PrefixRef<P> { /* fields omitted */ }

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]

impl<P> Copy for PrefixRef<P>[src]

impl<P> Debug for PrefixRef<P>[src]

impl<P> GetStaticEquivalent_ for PrefixRef<P> where
    P: GetStaticEquivalent_
[src]

impl<P> ImmutableRef for PrefixRef<P>[src]

type Target = WithMetadata_<P, P>

The referent of the pointer, what it points to.

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

impl<'a, P: 'a> Send for PrefixRef<P> where
    &'a WithMetadata_<P, P>: Send
[src]

impl<P> StableAbi for PrefixRef<P> where
    P: PrefixStableAbi
[src]

type IsNonZeroType = True

Whether this type has a single invalid bit-pattern. Read more

impl<'a, P: 'a> Sync for PrefixRef<P> where
    &'a WithMetadata_<P, P>: Sync
[src]

Auto Trait Implementations

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

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<'a, T> BorrowOwned<'a> for T where
    T: 'a + Clone
[src]

type ROwned = T

The owned type, stored in RCow::Owned

type RBorrowed = &'a T

The borrowed type, stored in RCow::Borrowed

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]

impl<T> SelfOps for T where
    T: ?Sized
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<This> TransmuteElement for This where
    This: ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The error type returned when the conversion fails.

impl<T> TypeIdentity for T where
    T: ?Sized
[src]

type Type = T

The same type as Self. Read more