Struct abi_stable::nonexhaustive_enum::NonExhaustive[][src]

#[repr(C)]
pub struct NonExhaustive<E, S, I> { /* fields omitted */ }
Expand description

A generic type for all ffi-safe non-exhaustive enums.

This type allows adding variants to enums it wraps in ABI compatible versions of a library.

Generic parameters

E

This is the enum that this was constructed from, and can be unwrapped back into if it’s one of the valid variants in this context.

S

The storage type,used to store the enum opaquely.

This has to be at least the size and alignment of the wrapped enum.

This is necessary because:

  • The compiler assumes that an enum cannot be a variant outside the ones it sees.

  • To give some flexibility to grow the enum in semver compatible versions of a library.

I

The interface of the enum(it implements InterfaceType), determining which traits are required when constructing NonExhaustive<> and which are available afterwards.

Example

Say that we define an error type for a library.

Version 1.0.

use abi_stable::{
    nonexhaustive_enum::{NonExhaustive, NonExhaustiveFor},
    sabi_trait,
    std_types::RString,
    StableAbi,
};

#[repr(u8)]
#[derive(StableAbi, Debug, Clone, PartialEq)]
#[sabi(kind(WithNonExhaustive(
    size = "[usize;8]",
    traits(Debug, Clone, PartialEq),
)))]
pub enum Error {
    #[doc(hidden)]
    __NonExhaustive,
    CouldNotFindItem {
        name: RString,
    },
    OutOfStock {
        id: usize,
        name: RString,
    },
}

fn returns_could_not_find_item(name: RString) -> NonExhaustiveFor<Error> {
    let e = Error::CouldNotFindItem { name };
    NonExhaustive::new(e)
}

fn returns_out_of_stock(id: usize, name: RString) -> NonExhaustiveFor<Error> {
    let e = Error::OutOfStock { id, name };
    NonExhaustive::new(e)
}

Then in 1.1 we add another error variant,returned only by new library functions.

use abi_stable::{
    nonexhaustive_enum::{NonExhaustive, NonExhaustiveFor},
    sabi_trait,
    std_types::RString,
    StableAbi,
};

#[repr(u8)]
#[derive(StableAbi, Debug, Clone, PartialEq)]
#[sabi(kind(WithNonExhaustive(
    size = "[usize;8]",
    traits(Debug, Clone, PartialEq),
)))]
pub enum Error {
    #[doc(hidden)]
    __NonExhaustive,
    CouldNotFindItem {
        name: RString,
    },
    OutOfStock {
        id: usize,
        name: RString,
    },
    InvalidItemId {
        id: usize,
    },
}

fn returns_invalid_item_id() -> NonExhaustiveFor<Error> {
    NonExhaustive::new(Error::InvalidItemId { id: 100 })
}

If a library user attempted to unwrap Error::InvalidItemId (using NonExhaustive::as_enum/as_enum_mut/into_enum) with the 1.0 version of Error they would get an Err(..) back.

Implementations

Constructs a NonExhaustive<> from value using its default interface and storage.

Panic

This panics if the storage has an alignment or size smaller than that of E.

Constructs a NonExhaustive<> from value using its default storage and a custom interface.

Panic

This panics if the storage has an alignment or size smaller than that of E.

Constructs a NonExhaustive<> from value using its default interface and a custom storage.

Panic

This panics if the storage has an alignment or size smaller than that of E.

Constructs a NonExhaustive<> from value using both a custom interface and storage.

Panic

This panics if the storage has an alignment or size smaller than that of E.

Checks that the alignment of E is correct,returning true if it is.

Checks that the size of E is correct,returning true if it is.

Asserts that E fits within S,with the correct alignment and size.

wraps a reference to this NonExhaustive<> into a reference to the original enum.

Errors

This returns an error if the wrapped enum is of a variant that is not valid in this context.

Example

This shows how some NonExhaustive<enum> can be unwrapped, and others cannot.
That enum comes from a newer version of the library than this knows.

use abi_stable::nonexhaustive_enum::doc_enums::example_2::{
    new_a, new_b, new_c, Foo,
};

assert_eq!(new_a().as_enum().ok(), Some(&Foo::A));
assert_eq!(new_b(10).as_enum().ok(), Some(&Foo::B(10)));
assert_eq!(new_b(77).as_enum().ok(), Some(&Foo::B(77)));
assert_eq!(new_c().as_enum().ok(), None);

Unwraps a mutable reference to this NonExhaustive<> into a mutable reference to the original enum.

Errors

This returns an error if the wrapped enum is of a variant that is not valid in this context.

Example

This shows how some NonExhaustive<enum> can be unwrapped, and others cannot.
That enum comes from a newer version of the library than this knows.

use abi_stable::nonexhaustive_enum::doc_enums::example_1::{
    new_a, new_b, new_c, Foo,
};

assert_eq!(new_a().as_enum_mut().ok(), Some(&mut Foo::A));
assert_eq!(new_b(10).as_enum_mut().ok(), None);
assert_eq!(new_b(77).as_enum_mut().ok(), None);
assert_eq!(new_c().as_enum_mut().ok(), None);

Unwraps this NonExhaustive<> into the original enum.

Errors

This returns an error if the wrapped enum is of a variant that is not valid in this context.

Example

This shows how some NonExhaustive<enum> can be unwrapped, and others cannot.
That enum comes from a newer version of the library than this knows.

use abi_stable::nonexhaustive_enum::doc_enums::example_2::{
    new_a, new_b, new_c, Foo,
};

assert_eq!(new_a().into_enum().ok(), Some(Foo::A));
assert_eq!(new_b(10).into_enum().ok(), Some(Foo::B(10)));
assert_eq!(new_b(77).into_enum().ok(), Some(Foo::B(77)));
assert_eq!(new_c().into_enum().ok(), None);

Returns whether the discriminant of this enum is valid in this context.

The only way for it to be invalid is if the dynamic library is a newer version than this knows.

Gets the value of the discriminant of the enum.

Transmute this NonExhaustive<E,S,I> into NonExhaustive<F,S,I>, changing the type of the enum it wraps.

Safety

This has the same safety requirements that std::mem::transmute has.

Panics

This panics if the storage has an alignment or size smaller than that of F.

Transmute this &NonExhaustive<E,S,I> into &NonExhaustive<F,S,I>, changing the type of the enum it wraps.

Safety

This has the same safety requirements that std::mem::transmute has.

Panics

This panics if the storage has an alignment or size smaller than that of F.

Transmute this &mut NonExhaustive<E,S,I> into &mut NonExhaustive<F,S,I>, changing the type of the enum it wraps.

Safety

This has the same safety requirements that std::mem::transmute has.

Panics

This panics if the storage has an alignment or size smaller than that of F.

Transmute this pointer to a NonExhaustive<E,S,I> into a pointer (of the same kind) to a NonExhaustive<F,S,I>, changing the type of the enum it wraps.

Safety

This has the same safety requirements that abi_stable::pointer_traits::TransmuteElement::transmute_element has.

Panics

This panics if the storage has an alignment or size smaller than that of F.

It serializes a NonExhaustive<_> into a proxy.

Deserializes a NonExhaustive<_> from a proxy.

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

First it Deserializes a string,then it deserializes into a NonExhaustive<_>,by using <I as DeserializeEnum>::deserialize_enum.

Deserialize this value from the given Serde deserializer. Read more

Formats the value using the given formatter. Read more

Executes the destructor for this type. Read more

The lower-level source of this error, if any. Read more

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

Returns a stack backtrace, if available, of where this error occurred. Read more

👎 Deprecated since 1.42.0:

use the Display impl or to_string()

👎 Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The type of the discriminant of the wrapped enum.

Gets the discriminant of the wrapped enum.

Gets miscelaneous information about the wrapped enum

The type of the discriminant of the wrapped enum.

Gets the discriminant of the wrapped enum.

Gets miscelaneous information about the wrapped enum

The type of the discriminant of the wrapped enum.

Gets the discriminant of the wrapped enum.

Gets miscelaneous information about the wrapped enum

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

First it serializes a NonExhaustive<_> into a proxy,then it serializes that proxy.

Serialize this value into the given Serde serializer. Read more

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

Converts the given value to a String. 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.