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,
impl<E> OneOf<E>where
E: TypeSet,
Sourcepub 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>,
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.
Sourcepub fn broaden<Other, Index>(self) -> OneOf<Other>
pub fn broaden<Other, Index>(self) -> OneOf<Other>
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.
Sourcepub fn subset<TargetList, Index>(
self,
) -> Result<OneOf<TargetList>, OneOf<<<E::Variants as SupersetOf<TargetList::Variants, Index>>::Remainder as TupleForm>::Tuple>>
pub fn subset<TargetList, Index>( self, ) -> Result<OneOf<TargetList>, OneOf<<<E::Variants as SupersetOf<TargetList::Variants, Index>>::Remainder as TupleForm>::Tuple>>
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.
Sourcepub fn take<Target>(self) -> Targetwhere
Target: 'static,
E: TypeSet<Variants = Cons<Target, End>>,
pub fn take<Target>(self) -> Targetwhere
Target: 'static,
E: TypeSet<Variants = Cons<Target, End>>,
For a OneOf
with a single variant, return
the contained value.