Struct OneOf

Source
pub struct OneOf<E: TypeSet> { /* private fields */ }
Expand description

Similar to anonymous unions / enums in languages that support type narrowing. OneOf is an open sum type. It differs from an enum in that you do not need to define any actual new type in order to hold some specific combination of variants, but rather you simply describe the OneOf as holding one value out of several specific possibilities, defined by using a tuple of those possible variants as the generic parameter for the OneOf.

For example, a OneOf<(String, u32)> contains either a String or a u32. The value over a simple Result or other traditional enum starts to become apparent in larger codebases where error handling needs to occur in different places for different errors. OneOf allows you to quickly specify a function’s return value as involving a precise subset of errors that the caller can clearly reason about.

Implementations§

Source§

impl<E> OneOf<E>
where E: TypeSet,

Source

pub fn new<T, Index>(t: T) -> OneOf<E>
where T: Any, E::Variants: Contains<T, Index>,

Create a new OneOf.

Source

pub fn narrow<Target, Index>( self, ) -> Result<Target, OneOf<<<E::Variants as Narrow<Target, Index>>::Remainder as TupleForm>::Tuple>>
where Target: 'static, E::Variants: Narrow<Target, Index>,

Attempt to downcast the OneOf into a specific type, and if that fails, return a OneOf which does not contain that type as one of its possible variants.

Source

pub fn broaden<Other, Index>(self) -> OneOf<Other>
where Other: TypeSet, Other::Variants: SupersetOf<E::Variants, Index>,

Turns the OneOf into a OneOf with a set of variants which is a superset of the current one. This may also be the same set of variants, but in a different order.

Source

pub fn subset<TargetList, Index>( self, ) -> Result<OneOf<TargetList>, OneOf<<<E::Variants as SupersetOf<TargetList::Variants, Index>>::Remainder as TupleForm>::Tuple>>
where TargetList: TypeSet, E::Variants: IsFold + SupersetOf<TargetList::Variants, Index>,

Attempt to split a subset of variants out of the OneOf, returning the remainder of possible variants if the value does not have one of the TargetList types.

Source

pub fn take<Target>(self) -> Target
where Target: 'static, E: TypeSet<Variants = Cons<Target, End>>,

For a OneOf with a single variant, return the contained value.

Source

pub fn to_enum(self) -> E::Enum
where E::Enum: From<Self>,

Convert the OneOf to an owned enum for use in pattern matching etc…

Source

pub fn as_enum<'a>(&'a self) -> E::EnumRef<'a>
where E::EnumRef<'a>: From<&'a Self>,

Borrow the enum as an enum for use in pattern matching etc…

Trait Implementations§

Source§

impl<E> Clone for OneOf<E>
where E: TypeSet, E::Variants: Clone + CloneFold,

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E> Debug for OneOf<E>
where E: TypeSet, E::Variants: Debug + DebugFold,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T> Deref for OneOf<(T,)>
where T: 'static,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<E> Display for OneOf<E>
where E: TypeSet, E::Variants: Display + DisplayFold,

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<E> Error for OneOf<E>
where E: TypeSet, E::Variants: Error + DebugFold + DisplayFold + ErrorFold,

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl<'a, A> From<&'a OneOf<(A,)>> for E1<&'a A>
where A: 'static,

Source§

fn from(one_of: &'a OneOf<(A,)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B> From<&'a OneOf<(A, B)>> for E2<&'a A, &'a B>
where A: 'static, B: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C> From<&'a OneOf<(A, B, C)>> for E3<&'a A, &'a B, &'a C>
where A: 'static, B: 'static, C: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D> From<&'a OneOf<(A, B, C, D)>> for E4<&'a A, &'a B, &'a C, &'a D>
where A: 'static, B: 'static, C: 'static, D: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D, E> From<&'a OneOf<(A, B, C, D, E)>> for E5<&'a A, &'a B, &'a C, &'a D, &'a E>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D, E)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D, E, F> From<&'a OneOf<(A, B, C, D, E, F)>> for E6<&'a A, &'a B, &'a C, &'a D, &'a E, &'a F>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D, E, F)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D, E, F, G> From<&'a OneOf<(A, B, C, D, E, F, G)>> for E7<&'a A, &'a B, &'a C, &'a D, &'a E, &'a F, &'a G>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D, E, F, G)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D, E, F, G, H> From<&'a OneOf<(A, B, C, D, E, F, G, H)>> for E8<&'a A, &'a B, &'a C, &'a D, &'a E, &'a F, &'a G, &'a H>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D, E, F, G, H)>) -> Self

Converts to this type from the input type.
Source§

impl<'a, A, B, C, D, E, F, G, H, I> From<&'a OneOf<(A, B, C, D, E, F, G, H, I)>> for E9<&'a A, &'a B, &'a C, &'a D, &'a E, &'a F, &'a G, &'a H, &'a I>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static,

Source§

fn from(one_of: &'a OneOf<(A, B, C, D, E, F, G, H, I)>) -> Self

Converts to this type from the input type.
Source§

impl<A> From<OneOf<(A,)>> for E1<A>
where A: 'static,

Source§

fn from(one_of: OneOf<(A,)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B> From<OneOf<(A, B)>> for E2<A, B>
where A: 'static, B: 'static,

Source§

fn from(one_of: OneOf<(A, B)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C> From<OneOf<(A, B, C)>> for E3<A, B, C>
where A: 'static, B: 'static, C: 'static,

Source§

fn from(one_of: OneOf<(A, B, C)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D> From<OneOf<(A, B, C, D)>> for E4<A, B, C, D>
where A: 'static, B: 'static, C: 'static, D: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D, E> From<OneOf<(A, B, C, D, E)>> for E5<A, B, C, D, E>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D, E)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D, E, F> From<OneOf<(A, B, C, D, E, F)>> for E6<A, B, C, D, E, F>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D, E, F)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D, E, F, G> From<OneOf<(A, B, C, D, E, F, G)>> for E7<A, B, C, D, E, F, G>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D, E, F, G)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D, E, F, G, H> From<OneOf<(A, B, C, D, E, F, G, H)>> for E8<A, B, C, D, E, F, G, H>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D, E, F, G, H)>) -> Self

Converts to this type from the input type.
Source§

impl<A, B, C, D, E, F, G, H, I> From<OneOf<(A, B, C, D, E, F, G, H, I)>> for E9<A, B, C, D, E, F, G, H, I>
where A: 'static, B: 'static, C: 'static, D: 'static, E: 'static, F: 'static, G: 'static, H: 'static, I: 'static,

Source§

fn from(one_of: OneOf<(A, B, C, D, E, F, G, H, I)>) -> Self

Converts to this type from the input type.
Source§

impl<T> From<T> for OneOf<(T,)>
where T: 'static,

Source§

fn from(t: T) -> OneOf<(T,)>

Converts to this type from the input type.
Source§

impl<T> Send for OneOf<T>
where T: TypeSet + Send,

Source§

impl<T> Sync for OneOf<T>
where T: TypeSet + Sync,

Auto Trait Implementations§

§

impl<E> Freeze for OneOf<E>

§

impl<E> !RefUnwindSafe for OneOf<E>

§

impl<E> Unpin for OneOf<E>
where E: Unpin,

§

impl<E> !UnwindSafe for OneOf<E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.