Skip to main content

IsVariant

Trait IsVariant 

Source
pub unsafe trait IsVariant<V>: AssertTStrParam<V> {
    // Required method
    fn is_variant_(&self, variant: V) -> bool;
}
Expand description

Queries whether an enum is some variant (the V type parameter)

Example bounds: IsVariant<TS!(Foo)>,IsVariant<TS!(Bar)>.

§Safety

An implementation of IsVariant<TS!(Foo)> must only return true if the enum is the Foo variant (Foo is just an example,it applies to all variants).

Undefined behavior will happen if this trait return true, while the accessor for a field of that variant returns None.

§Example

use structural::enums::IsVariant;
use structural::{TS,Structural,fp};

assertions(Enum::Foo, Enum::Bar(0), Enum::Boom{x:0,y:false});

assertions(Enum2::Foo, Enum2::Bar, Enum2::Boom);

fn assertions<T>(foo:T, bar:T, boom:T)
where
    T: IsVariant<TS!(Foo)> + IsVariant<TS!(Bar)> + IsVariant<TS!(Boom)>
{
    assert_eq!( foo.is_variant_(fp!(Foo)), true );
    assert_eq!( foo.is_variant_(fp!(Bar)), false );
    assert_eq!( foo.is_variant_(fp!(Boom)), false );
    
    assert_eq!( bar.is_variant_(fp!(Foo)), false );
    assert_eq!( bar.is_variant_(fp!(Bar)), true );
    assert_eq!( bar.is_variant_(fp!(Boom)), false );
    
    assert_eq!( boom.is_variant_(fp!(Foo)), false );
    assert_eq!( boom.is_variant_(fp!(Bar)), false );
    assert_eq!( boom.is_variant_(fp!(Boom)), true );
}

#[derive(Structural)]
enum Enum{
    Foo,
    Bar(u8),
    Boom{x:u32,y:bool},
}

#[derive(Structural)]
enum Enum2{
    Foo,
    Bar,
    Boom,
}

Required Methods§

Source

fn is_variant_(&self, variant: V) -> bool

Checks whether this enum is the variant that V stands for.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<'a, T, __V> IsVariant<TStr<__V>> for &'a T
where T: IsVariant<TStr<__V>> + ?Sized,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<'a, T, __V> IsVariant<TStr<__V>> for &'a mut T
where T: IsVariant<TStr<__V>> + ?Sized + 'a,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<P, __V> IsVariant<TStr<__V>> for Pin<P>
where P::Target: IsVariant<TStr<__V>> + Sized, P: Deref,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<T, __V> IsVariant<TStr<__V>> for Arc<T>
where T: IsVariant<TStr<__V>> + ?Sized,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<T, __V> IsVariant<TStr<__V>> for Box<T>
where T: IsVariant<TStr<__V>> + ?Sized,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<T, __V> IsVariant<TStr<__V>> for ManuallyDrop<T>
where T: IsVariant<TStr<__V>>,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Source§

impl<T, __V> IsVariant<TStr<__V>> for Rc<T>
where T: IsVariant<TStr<__V>> + ?Sized,

Source§

fn is_variant_(&self, name: TStr<__V>) -> bool

Implementors§

Source§

impl<T, __V> IsVariant<TStr<__V>> for FieldCloner<T>
where T: IsVariant<TStr<__V>>,

Source§

impl<T, __V> IsVariant<TStr<__V>> for StrucWrapper<T>
where T: IsVariant<TStr<__V>>,