File: into_primitive.rs

package info (click to toggle)
rust-num-enum 0.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: makefile: 2
file content (50 lines) | stat: -rw-r--r-- 946 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![cfg_attr(feature = "nightly", feature(arbitrary_enum_discriminant))]

use ::num_enum::IntoPrimitive;

// Guard against https://github.com/illicitonion/num_enum/issues/27
mod alloc {}
mod core {}
mod num_enum {}
mod std {}

#[derive(IntoPrimitive)]
#[repr(u8)]
enum Enum {
    Zero,
    One,
    Two,
}

#[test]
fn simple() {
    let zero: u8 = Enum::Zero.into();
    assert_eq!(zero, 0u8);

    let one: u8 = Enum::One.into();
    assert_eq!(one, 1u8);

    let two: u8 = Enum::Two.into();
    assert_eq!(two, 2u8);
}

#[test]
#[cfg(feature = "nightly")]
fn catch_all() {
    #[derive(Debug, Eq, PartialEq, IntoPrimitive)]
    #[repr(u8)]
    enum Enum {
        Zero = 0,
        #[num_enum(catch_all)]
        NonZero(u8),
    }

    let zero: u8 = Enum::Zero.into();
    assert_eq!(zero, 0u8);

    let one: u8 = Enum::NonZero(1u8).into();
    assert_eq!(one, 1u8);

    let two: u8 = Enum::NonZero(2u8).into();
    assert_eq!(two, 2u8);
}