#discriminant #enums #derive #macro

derive-discriminant

枚举区分符的 derive 宏

2 个版本

0.1.1 2023年4月15日
0.1.0 2023年4月12日

#749 in 过程宏

Download history 2/week @ 2024-03-07 2/week @ 2024-03-14 20/week @ 2024-03-28 19/week @ 2024-04-04

每月下载量 54

MIT 许可证

8KB
123

derive-discriminant

Crates.io

#[derive(Discriminant)]
#[derive(Debug, PartialEq, Eq)]
enum Abc {
    A,
    B { x: usize, y: usize },
}

fn main() {
    // try_from implementations
    let a = A::try_from(Abc::A).unwrap();
    assert_eq!(a, A);
    let not_a = A::try_from(Abc::B { x: 1, y: 2 });
    assert!(not_a.is_err());

    let b = B::try_from(Abc::B { x: 1, y: 2 }).unwrap();
    assert_eq!(b, B { x: 1, y: 2 });
    let not_b = B::try_from(Abc::A);
    assert!(not_b.is_err());

    // from implementations
    let a = Abc::from(A);
    assert_eq!(a, Abc::A);
    let b = Abc::from(B { x: 1, y: 2 });
    assert_eq!(b, Abc::B { x: 1, y: 2 });
    
    // casting
    let a = Abc::A;
    let a: Box<dyn Debug> = a.cast();
    assert_eq!(format!("{a:?}"), "A");

    let b = Abc::B { x: 1, y: 2 };
    let b: Box<dyn Debug> = b.cast();
    assert_eq!(format!("{b:?}"), "B { x: 1, y: 2 }");
    
    // is_{variant} methods
    assert!(Abc::A.is_a());
    assert!(!Abc::A.is_b());

    assert!(!Abc::B { x: 1, y: 2 }.is_a());
    assert!(Abc::B { x: 1, y: 2 }.is_b());
}

依赖项

~310–770KB
~18K SLoC