[][src]Trait newtype_enum::Enum

pub trait Enum: Sized {
    fn from_variant(v: impl Variant<Self>) -> Self { ... }
fn set_variant(&mut self, v: impl Variant<Self>) -> Self { ... }
fn into_variant<V: Variant<Self>>(self) -> Option<V> { ... }
fn variant<V: Variant<Self>>(&self) -> Option<&V> { ... }
fn variant_mut<V: Variant<Self>>(&mut self) -> Option<&mut V> { ... }
fn is_variant<V: Variant<Self>>(&self) -> bool { ... }
fn into_variant_unwrap<V: Variant<Self>>(self) -> V { ... }
unsafe fn into_variant_unchecked<V: Variant<Self>>(self) -> V { ... }
unsafe fn variant_unchecked<V: Variant<Self>>(&self) -> &V { ... }
unsafe fn variant_unchecked_mut<V: Variant<Self>>(&mut self) -> &mut V { ... } }

Mark a type as an enum.

Use the newtype_enum macro to implement this trait for your enum types.

#[newtype_enum(variants = "pub example")]
#[derive(Debug)]
pub enum Test {
    Ping,
    Number(usize),
    Str(&'static str),
    #[derive(Clone)]
    Hello {
        name: &'static str,
    },
}

use newtype_enum::Enum;

let test = Test::from_variant(example::Hello { name: "Tester" });
println!("{:?}", test);

let variant: example::Hello = test.into_variant().unwrap();
let cloned = variant.clone();
assert_eq!(variant, cloned);

Provided methods

fn from_variant(v: impl Variant<Self>) -> Self

Construct an enum from one of its newtype variants.

let test = Test::from_variant(123);
assert_eq!(test, Test::Number(123));

fn set_variant(&mut self, v: impl Variant<Self>) -> Self

Set the enum to one of its newtype variants.

This returns the old value of the enum.

let mut test = Test::from_variant(123);

let old = test.set_variant("Hello World");
assert_eq!(old, Test::Number(123));
assert_eq!(test, Test::Str("Hello World"));

fn into_variant<V: Variant<Self>>(self) -> Option<V>

Convert the enum into one of its newtype variants.

let create_test = || Test::from_variant(123);

assert_eq!(create_test().into_variant(), Some(123));

let variant: Option<&str> = create_test().into_variant();
assert_eq!(variant, None);

assert_eq!(create_test().into_variant::<&str>(), None);

fn variant<V: Variant<Self>>(&self) -> Option<&V>

Get a reference to one of its newtype variants.

let test = Test::from_variant(123);
assert_eq!(test.variant(), Some(&123));

let variant: Option<&&str> = test.variant();
assert_eq!(variant, None);

assert_eq!(test.variant::<&str>(), None);

fn variant_mut<V: Variant<Self>>(&mut self) -> Option<&mut V>

Get a mutable reference to one of its newtype variants.

let mut test = Test::from_variant(123);
assert_eq!(test.variant_mut(), Some(&mut 123));
assert_eq!(test.variant_mut(), None::<&mut &str>);

if let Some(mut variant) = test.variant_mut() {
    *variant = 42;
}
assert_eq!(test.into_variant(), Some(42));

fn is_variant<V: Variant<Self>>(&self) -> bool

Check if the enum currently holds the newtype variant V.

If this method returns true, it is safe to call one of the variant_unchecked methods.

let mut test = Test::from_variant(123);
assert_eq!(test.is_variant::<usize>(), true);
assert_eq!(test.is_variant::<&str>(), false);

fn into_variant_unwrap<V: Variant<Self>>(self) -> V

Convert the enum into one of its newtype variants and unwrap the value.

This method is equivalent to self.into_variant().unwrap() but written without an intermediate Option<V> value. Therefore the compiler can sometimes optimize the code better.

let mut test = Test::from_variant(123);
let variant: usize = test.into_variant_unwrap();
assert_eq!(variant, 123);
let mut test = Test::from_variant("Hello World");
let variant: usize = test.into_variant_unwrap(); // fails

unsafe fn into_variant_unchecked<V: Variant<Self>>(self) -> V

Convert the enum into one of its newtype variants without checking if the variant matches.

let test = Test::from_variant(123);

if test.is_variant::<usize>() {
    // ...

    // SAFETY: We already checked if the enum has the correct variant
    // and we did not change it in between.
    let number: usize = unsafe { test.into_variant_unchecked() };
    assert_eq!(number, 123);
} else {
    panic!("expected a usize variant");
}

unsafe fn variant_unchecked<V: Variant<Self>>(&self) -> &V

Get a reference to one of its newtype variants without checking if the variant matches.

let test = Test::from_variant(123);

if test.is_variant::<usize>() {
    // ...

    // SAFETY: We already checked if the enum has the correct variant
    // and we did not change it in between.
    let number: &usize = unsafe { test.variant_unchecked() };
    assert_eq!(number, &123);
} else {
    panic!("expected a usize variant");
}

unsafe fn variant_unchecked_mut<V: Variant<Self>>(&mut self) -> &mut V

Get a mutable reference to one of its newtype variants without checking if the variant matches.

let mut test = Test::from_variant(123);

if test.is_variant::<usize>() {
    // ...

    // SAFETY: We already checked if the enum has the correct variant
    // and we did not change it in between.
    let number: &mut usize = unsafe { test.variant_unchecked_mut() };
    *number = 42;
} else {
    panic!("expected a usize variant");
}

assert_eq!(test.into_variant(), Some(42));
Loading content...

Implementors

Loading content...