abi_stable::prefix_type

Type Alias WithMetadata

Source
pub type WithMetadata<T, P = <T as PrefixTypeTrait>::PrefixFields> = WithMetadata_<T, P>;
Expand description

Alias for WithMetadata_ that defaults to passing <T as PrefixTypeTrait>::PrefixFields as the second type parameter.

WithMetadata_ can’t have that defaulted type parameter, because T: PrefixTypeTrait is an overly restrictive bound in some cases.

Aliased Type§

struct WithMetadata<T, P = <T as PrefixTypeTrait>::PrefixFields> {
    pub value: AlignToUsize<T>,
    /* private fields */
}

Fields§

§value: AlignToUsize<T>

The wrapped value.

Implementations

Source§

impl<T, P> WithMetadata_<T, P>

Source

pub const fn new(value: T) -> Self
where T: PrefixTypeTrait<PrefixFields = P>,

Constructs this with WithMetadata::new(value)

Source

pub const fn field_accessibility(&self) -> FieldAccessibility

A bit array that describes the accessibility of each field in T.

Source

pub const fn type_layout(&self) -> &'static PTStructLayout

The basic layout of the prefix type, for error messages.

Source

pub const unsafe fn raw_as_prefix(this: *const Self) -> PrefixRef<P>

Constructs a PrefixRef from this.

§Safety

You must enture that this WithMetadata lives for the entire program’s lifetime.

Source

pub const unsafe fn as_prefix(&self) -> PrefixRef<P>

Constructs a PrefixRef from self.

§Safety

You must ensure that self lives for the entire program’s lifetime.

§Alternative

For a safe equivalent of this, you can use StaticRef::as_prefix.

Source

pub const fn static_as_prefix(&'static self) -> PrefixRef<P>

Constructs a PrefixRef from self.

§Example
use abi_stable::{
    for_examples::{Module, Module_Ref},
    prefix_type::{PrefixRef, PrefixTypeTrait, WithMetadata},
    std_types::{RSome, RStr},
};

const WITH_META: &WithMetadata<Module> = &WithMetadata::new(
    Module {
        first: RSome(13),
        second: RStr::from_str("foo"),
        third: 100,
    },
);

const MOD: Module_Ref = Module_Ref(WITH_META.static_as_prefix());

assert_eq!(MOD.first(), RSome(13));
assert_eq!(MOD.second().as_str(), "foo");