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::{
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
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.
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>,
pub fn as_enum_mut(&mut self) -> Result<&mut E, UnwrapEnumError<&mut Self>> where
E: GetVTable<S, I>,
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);
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);
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.
pub unsafe fn transmute_enum_ptr<P, F>(this: P) -> P::TransmutedPtr where
P: Deref<Target = Self>,
P: CanTransmuteElement<NonExhaustive<F, S, I>>,
pub unsafe fn transmute_enum_ptr<P, F>(this: P) -> P::TransmutedPtr where
P: Deref<Target = Self>,
P: CanTransmuteElement<NonExhaustive<F, S, I>>,
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
.
pub fn serialize_into_proxy(&self) -> Result<I::Proxy, RBoxError> where
I: InterfaceBound<Serialize = Implemented<Serialize>>,
I: SerializeEnum<NonExhaustive<E, S, I>>,
pub fn serialize_into_proxy(&self) -> Result<I::Proxy, RBoxError> where
I: InterfaceBound<Serialize = Implemented<Serialize>>,
I: SerializeEnum<NonExhaustive<E, S, I>>,
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,
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,
Deserializes a NonExhaustive<_>
from a proxy.
Trait Implementations
impl<E, S, I> Clone for NonExhaustive<E, S, I> where
I: InterfaceBound<Clone = Implemented<Clone>>,
impl<E, S, I> Clone for NonExhaustive<E, S, I> where
I: InterfaceBound<Clone = Implemented<Clone>>,
impl<E, S, I> Debug for NonExhaustive<E, S, I> where
I: InterfaceBound<Debug = Implemented<Debug>>,
impl<E, S, I> Debug for NonExhaustive<E, S, I> where
I: InterfaceBound<Debug = Implemented<Debug>>,
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>,
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>,
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
impl<E, S, I> Display for NonExhaustive<E, S, I> where
I: InterfaceBound<Display = Implemented<Display>>,
impl<E, S, I> Display for NonExhaustive<E, S, I> where
I: InterfaceBound<Display = Implemented<Display>>,
impl<E, S, I> Error for NonExhaustive<E, S, I> where
I: InterfaceBound<Debug = Implemented<Debug>, Display = Implemented<Display>, Error = Implemented<Error>>,
impl<E, S, I> Error for NonExhaustive<E, S, I> where
I: InterfaceBound<Debug = Implemented<Debug>, Display = Implemented<Display>, Error = Implemented<Error>>,
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,
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,
type StaticEquivalent = _static_NonExhaustive<__GetStaticEquivalent<E>, __GetStaticEquivalent<S>, __GetStaticEquivalent<I>>
impl<E, S, I> Hash for NonExhaustive<E, S, I> where
I: InterfaceBound<Hash = Implemented<Hash>>,
impl<E, S, I> Hash for NonExhaustive<E, S, I> where
I: InterfaceBound<Hash = Implemented<Hash>>,
type Discriminant = E::Discriminant
type Discriminant = E::Discriminant
The type of the discriminant of the wrapped enum.
Gets the discriminant of the wrapped enum.
Gets miscelaneous information about the wrapped enum
type Discriminant = E::Discriminant
type Discriminant = E::Discriminant
The type of the discriminant of the wrapped enum.
Gets the discriminant of the wrapped enum.
Gets miscelaneous information about the wrapped enum
type Discriminant = E::Discriminant
type Discriminant = E::Discriminant
The type of the discriminant of the wrapped enum.
Gets the discriminant of the wrapped enum.
Gets miscelaneous information about the wrapped enum
impl<E, S, I> Ord for NonExhaustive<E, S, I> where
I: InterfaceBound<Ord = Implemented<Ord>>,
Self: PartialOrd + Eq,
impl<E, S, I> Ord for NonExhaustive<E, S, I> where
I: InterfaceBound<Ord = Implemented<Ord>>,
Self: PartialOrd + Eq,
impl<E, S, I> PartialEq<E> for NonExhaustive<E, S, I> where
E: GetEnumInfo + PartialEq,
I: InterfaceBound<PartialEq = Implemented<PartialEq>>,
impl<E, S, I> PartialEq<E> for NonExhaustive<E, S, I> where
E: GetEnumInfo + PartialEq,
I: InterfaceBound<PartialEq = Implemented<PartialEq>>,
impl<E, S, I1, I2> PartialEq<NonExhaustive<E, S, I2>> for NonExhaustive<E, S, I1> where
I1: InterfaceBound<PartialEq = Implemented<PartialEq>>,
impl<E, S, I1, I2> PartialEq<NonExhaustive<E, S, I2>> for NonExhaustive<E, S, I1> where
I1: InterfaceBound<PartialEq = Implemented<PartialEq>>,
impl<E, S, I> PartialOrd<E> for NonExhaustive<E, S, I> where
E: GetEnumInfo + PartialOrd,
I: InterfaceBound<PartialOrd = Implemented<PartialOrd>>,
Self: PartialEq<E>,
impl<E, S, I> PartialOrd<E> for NonExhaustive<E, S, I> where
E: GetEnumInfo + PartialOrd,
I: InterfaceBound<PartialOrd = Implemented<PartialOrd>>,
Self: PartialEq<E>,
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
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>>,
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>>,
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
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,
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,
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,
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,
type IsNonZeroType = False
type IsNonZeroType = False
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.
impl<E, S, I> Eq for NonExhaustive<E, S, I> where
Self: PartialEq,
I: InterfaceBound<Eq = Implemented<Eq>>,
Auto Trait Implementations
impl<E, S, I> RefUnwindSafe for NonExhaustive<E, S, I> where
E: RefUnwindSafe,
I: RefUnwindSafe,
S: RefUnwindSafe,
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
Mutably borrows from an owned value. Read more
type ROwned = T
type ROwned = T
The owned type, stored in RCow::Owned
type ForSelf = WithMetadata_<T, T>
type ForSelf = WithMetadata_<T, T>
This is always WithMetadata_<Self, Self>
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
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
unsafe fn transmute_element<T>(
self
) -> <Self as CanTransmuteElement<T>>::TransmutedPtr where
Self: CanTransmuteElement<T>,
Transmutes the element type of this pointer.. Read more
type Type = T
type Type = T
This is always Self
.
Converts a value back to the original type.
Converts a mutable reference back to the original type.
Converts a box back to the original type.
Converts an Arc back to the original type. Read more
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.
Converts a box back to the original type.
Converts an Arc back to the original type.
Converts an Rc back to the original type.