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

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

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::{
    StableAbi,
    nonexhaustive_enum::{NonExhaustiveFor,NonExhaustive},
    std_types::RString,
    sabi_trait,
};

#[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::{
    StableAbi,
    nonexhaustive_enum::{NonExhaustiveFor,NonExhaustive},
    std_types::RString,
    sabi_trait,
};

#[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

impl<E, S, I> NonExhaustive<E, S, I>[src]

pub fn new(value: E) -> Self where
    E: GetVTable<S, I, DefaultStorage = S, DefaultInterface = I>, 
[src]

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.

pub fn with_interface(value: E) -> Self where
    E: GetVTable<S, I, DefaultStorage = S>, 
[src]

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.

pub fn with_storage(value: E) -> Self where
    E: GetVTable<S, I, DefaultInterface = I>, 
[src]

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.

pub fn with_storage_and_interface(value: E) -> Self where
    E: GetVTable<S, I>, 
[src]

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.

pub fn check_alignment() -> bool[src]

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

pub fn check_size() -> bool[src]

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

pub fn assert_fits_within_storage()[src]

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

impl<E, S, I> NonExhaustive<E, S, I> where
    E: GetEnumInfo
[src]

pub fn as_enum(&self) -> Result<&E, UnwrapEnumError<&Self>>[src]

Unwraps 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::{Foo,new_a,new_b,new_c},
};

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

pub fn as_enum_mut(&mut self) -> Result<&mut E, UnwrapEnumError<&mut Self>> where
    E: GetVTable<S, I>, 
[src]

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::{Foo,new_a,new_b,new_c},
};

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

pub fn into_enum(self) -> Result<E, UnwrapEnumError<Self>>[src]

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::{Foo,new_a,new_b,new_c},
};

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

pub fn is_valid_discriminant(&self) -> bool[src]

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.

pub fn get_discriminant(&self) -> E::Discriminant[src]

Gets the value of the discriminant of the enum.

impl<E, S, I> NonExhaustive<E, S, I>[src]

pub unsafe fn transmute_enum<F>(self) -> NonExhaustive<F, S, I>[src]

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.

pub unsafe fn transmute_enum_ref<F>(&self) -> &NonExhaustive<F, S, I>[src]

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.

pub unsafe fn transmute_enum_mut<F>(&mut self) -> &mut NonExhaustive<F, S, I>[src]

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.

pub unsafe fn transmute_enum_ptr<P, F>(this: P) -> P::TransmutedPtr where
    P: Deref<Target = Self>,
    P: CanTransmuteElement<NonExhaustive<F, S, I>>, 
[src]

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.

impl<E, S, I> NonExhaustive<E, S, I>[src]

pub fn serialize_into_proxy(&self) -> Result<I::Proxy, RBoxError> where
    I: InterfaceBound<Serialize = Implemented<Serialize>>,
    I: SerializeEnum<NonExhaustive<E, S, I>>, 
[src]

It serializes a NonExhaustive<_> into a proxy.

pub fn deserialize_from_proxy<'borr>(proxy: I::Proxy) -> Result<Self, RBoxError> where
    I: InterfaceBound<Deserialize = Implemented<Deserialize>>,
    I: DeserializeEnum<'borr, NonExhaustive<E, S, I>>,
    I::Proxy: 'borr,
    E: GetEnumInfo
[src]

Deserializes a NonExhaustive<_> from a proxy.

Trait Implementations

impl<E, S, I> Clone for NonExhaustive<E, S, I> where
    I: InterfaceBound<Clone = Implemented<Clone>>, 
[src]

impl<E, S, I> Debug for NonExhaustive<E, S, I> where
    I: InterfaceBound<Debug = Implemented<Debug>>, 
[src]

impl<'de, E, S, I> Deserialize<'de> for NonExhaustive<E, S, I> where
    E: 'de + GetVTable<S, I>,
    S: 'de,
    I: 'de + InterfaceBound<Deserialize = Implemented<Deserialize>>,
    I: DeserializeEnum<'de, NonExhaustive<E, S, I>>,
    <I as DeserializeEnum<'de, NonExhaustive<E, S, I>>>::Proxy: Deserialize<'de>, 
[src]

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

impl<E, S, I> Display for NonExhaustive<E, S, I> where
    I: InterfaceBound<Display = Implemented<Display>>, 
[src]

impl<E, S, I> Drop for NonExhaustive<E, S, I>[src]

impl<E, S, I> Eq for NonExhaustive<E, S, I> where
    Self: PartialEq,
    I: InterfaceBound<Eq = Implemented<Eq>>, 
[src]

impl<E, S, I> Error for NonExhaustive<E, S, I> where
    I: InterfaceBound<Debug = Implemented<Debug>, Display = Implemented<Display>, Error = Implemented<Error>>, 
[src]

impl<E, S, I> GetStaticEquivalent_ for NonExhaustive<E, S, I> where
    E: __GetStaticEquivalent_,
    S: __GetStaticEquivalent_,
    I: __GetStaticEquivalent_,
    NonExhaustiveVtable_Ref<E, S, I>: StableAbi,
    E: GetNonExhaustive<S>,
    I: InterfaceBound
[src]

impl<E, S, I> Hash for NonExhaustive<E, S, I> where
    I: InterfaceBound<Hash = Implemented<Hash>>, 
[src]

impl<E, S, I> NonExhaustiveSharedOps for NonExhaustive<E, S, I> where
    E: GetEnumInfo
[src]

type Discriminant = E::Discriminant

The type of the discriminant of the wrapped enum.

impl<'a, E, S, I> NonExhaustiveSharedOps for &'a NonExhaustive<E, S, I> where
    E: GetEnumInfo
[src]

type Discriminant = E::Discriminant

The type of the discriminant of the wrapped enum.

impl<'a, E, S, I> NonExhaustiveSharedOps for &'a mut NonExhaustive<E, S, I> where
    E: GetEnumInfo
[src]

type Discriminant = E::Discriminant

The type of the discriminant of the wrapped enum.

impl<E, S, I> Ord for NonExhaustive<E, S, I> where
    I: InterfaceBound<Ord = Implemented<Ord>>,
    Self: PartialOrd + Eq
[src]

impl<E, S, I> PartialEq<E> for NonExhaustive<E, S, I> where
    E: GetEnumInfo + PartialEq,
    I: InterfaceBound<PartialEq = Implemented<PartialEq>>, 
[src]

impl<E, S, I1, I2> PartialEq<NonExhaustive<E, S, I2>> for NonExhaustive<E, S, I1> where
    I1: InterfaceBound<PartialEq = Implemented<PartialEq>>, 
[src]

impl<E, S, I> PartialOrd<E> for NonExhaustive<E, S, I> where
    E: GetEnumInfo + PartialOrd,
    I: InterfaceBound<PartialOrd = Implemented<PartialOrd>>,
    Self: PartialEq<E>, 
[src]

impl<E, S, I1, I2> PartialOrd<NonExhaustive<E, S, I2>> for NonExhaustive<E, S, I1> where
    I1: InterfaceBound<PartialOrd = Implemented<PartialOrd>>,
    Self: PartialEq<NonExhaustive<E, S, I2>>, 
[src]

impl<E, S, I> Serialize for NonExhaustive<E, S, I> where
    I: InterfaceBound<Serialize = Implemented<Serialize>>,
    I: SerializeEnum<NonExhaustive<E, S, I>>,
    I::Proxy: Serialize
[src]

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

impl<E, S, I> StableAbi for NonExhaustive<E, S, I> where
    E: __GetStaticEquivalent_,
    S: __GetStaticEquivalent_,
    I: __GetStaticEquivalent_,
    NonExhaustiveVtable_Ref<E, S, I>: StableAbi,
    E: GetNonExhaustive<S>,
    I: InterfaceBound,
    <E as GetNonExhaustive<S>>::NonExhaustive: __StableAbi
[src]

type IsNonZeroType = False

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

Auto Trait Implementations

impl<E, S, I> RefUnwindSafe for NonExhaustive<E, S, I> where
    E: RefUnwindSafe,
    I: RefUnwindSafe,
    S: RefUnwindSafe

impl<E, S, I> Send for NonExhaustive<E, S, I> where
    E: Sync,
    I: Sync,
    S: Send + Sync

impl<E, S, I> Sync for NonExhaustive<E, S, I> where
    E: Sync,
    I: Sync,
    S: Sync

impl<E, S, I> Unpin for NonExhaustive<E, S, I> where
    S: Unpin

impl<E, S, I> UnwindSafe for NonExhaustive<E, S, I> where
    E: RefUnwindSafe,
    I: RefUnwindSafe,
    S: RefUnwindSafe + UnwindSafe

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> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[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]

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<T> ToString for T where
    T: Display + ?Sized
[src]

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

impl<This> ValidTag_Bounds for This where
    This: Debug + Clone + PartialEq<This>, 
[src]