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

#[repr(transparent)]
pub struct PrefixRef<P> { /* fields omitted */ }
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(PrefixTypeTrait::METADATA, 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(PrefixTypeTrait::METADATA, MOD_VAL)
    });
}

const PREFIX_B: PrefixRef<Module_Prefix> = WithAssoc::MOD_WM.as_prefix();

/////////////////////////////////////////

Implementations

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},
    rstr,
    std_types::*,
};

const MOD_WM: &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));

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(PrefixTypeTrait::METADATA, 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));

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(
        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));

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`

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(
        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;

Converts this PrefixRef into a raw pointer.

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.

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

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

The referent of the pointer, what it points to.

A marker type that can be used as a proof that the T type parameter of ImmutableRefTarget<T, U> implements ImmutableRef<Target = U>. Read more

Converts this pointer to a NonNull.

Constructs this pointer from a NonNull. Read more

Converts this pointer to a raw pointer.

Constructs this pointer from a raw pointer. Read more

A struct that contains all the fields of some other struct up to the field annotated with #[sabi(last_prefix_field)] inclusive. Read more

A type used to prove that the This type parameter in PointsToPrefixFields<This, PF> implements PrefixRefTrait<PrefixFields = PF>. Read more

Converts a PrefixRef to Self

Converts Self to a PrefixRef

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

The layout of the type provided by implementors.

const-equivalents of the associated types.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

The owned type, stored in RCow::Owned

The borrowed type, stored in RCow::Borrowed

Performs the conversion.

This is always WithMetadata_<Self, Self>

Performs the conversion.

Gets a reference to a field, determined by offset. Read more

Gets a muatble reference to a field, determined by offset. Read more

Gets a const pointer to a field, the field is determined by offset. Read more

Gets a mutable pointer to a field, determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Replaces a field (determined by offset) with value, returning the previous value of the field. Read more

Swaps a field (determined by offset) with the same field in right. Read more

Gets a copy of a field (determined by offset). The field is determined by offset. Read more

Compares the address of self with the address of other. Read more

Emulates the pipeline operator, allowing method syntax in more places. Read more

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self. Read more

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self. Read more

Mutates self using a closure taking self by mutable reference, passing it along the method chain. Read more

Observes the value of self, passing it along unmodified. Useful in long method chains. Read more

Performs a conversion with Into. using the turbofish .into_::<_>() syntax. Read more

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax. Read more

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax. Read more

Drops self using method notation. Alternative to std::mem::drop. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

Transmutes the element type of this pointer.. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

This is always Self.

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type. Read more

This is supported on crate feature alloc only.

Converts an Rc back to the original type. Read more

Converts a value back to the original type.

Converts a reference back to the original type.

Converts a mutable reference back to the original type.

This is supported on crate feature alloc only.

Converts a box back to the original type.

This is supported on crate feature alloc only.

Converts an Arc back to the original type.

This is supported on crate feature alloc only.

Converts an Rc back to the original type.