use core::fmt; use byte_enum::*; #[derive(ByteEnum, Copy, Clone, Debug, PartialEq, Eq)] #[repr(u8)] pub enum TestEnumBasic { VariantA, VariantB, VariantC, VariantD, } #[derive(ByteEnum, Copy, Clone, Debug, PartialEq, Eq)] #[repr(u8)] pub enum TestEnumComprehensive { Variant000, Variant001, Variant002, Variant003, Variant004, Variant005, Variant006, Variant007, Variant008, Variant009, Variant010, Variant011, Variant012, Variant013, Variant014, Variant015, Variant016, Variant017, Variant018, Variant019, Variant020, Variant021, Variant022, Variant023, Variant024, Variant025, Variant026, Variant027, Variant028, Variant029, Variant030, Variant031, Variant032, Variant033, Variant034, Variant035, Variant036, Variant037, Variant038, Variant039, Variant040, Variant041, Variant042, Variant043, Variant044, Variant045, Variant046, Variant047, Variant048, Variant049, Variant050, Variant051, Variant052, Variant053, Variant054, Variant055, Variant056, Variant057, Variant058, Variant059, Variant060, Variant061, Variant062, Variant063, Variant064, Variant065, Variant066, Variant067, Variant068, Variant069, Variant070, Variant071, Variant072, Variant073, Variant074, Variant075, Variant076, Variant077, Variant078, Variant079, Variant080, Variant081, Variant082, Variant083, Variant084, Variant085, Variant086, Variant087, Variant088, Variant089, Variant090, Variant091, Variant092, Variant093, Variant094, Variant095, Variant096, Variant097, Variant098, Variant099, Variant100, Variant101, Variant102, Variant103, Variant104, Variant105, Variant106, Variant107, Variant108, Variant109, Variant110, Variant111, Variant112, Variant113, Variant114, Variant115, Variant116, Variant117, Variant118, Variant119, Variant120, Variant121, Variant122, Variant123, Variant124, Variant125, Variant126, Variant127, Variant128, Variant129, Variant130, Variant131, Variant132, Variant133, Variant134, Variant135, Variant136, Variant137, Variant138, Variant139, Variant140, Variant141, Variant142, Variant143, Variant144, Variant145, Variant146, Variant147, Variant148, Variant149, Variant150, Variant151, Variant152, Variant153, Variant154, Variant155, Variant156, Variant157, Variant158, Variant159, Variant160, Variant161, Variant162, Variant163, Variant164, Variant165, Variant166, Variant167, Variant168, Variant169, Variant170, Variant171, Variant172, Variant173, Variant174, Variant175, Variant176, Variant177, Variant178, Variant179, Variant180, Variant181, Variant182, Variant183, Variant184, Variant185, Variant186, Variant187, Variant188, Variant189, Variant190, Variant191, Variant192, Variant193, Variant194, Variant195, Variant196, Variant197, Variant198, Variant199, Variant200, Variant201, Variant202, Variant203, Variant204, Variant205, Variant206, Variant207, Variant208, Variant209, Variant210, Variant211, Variant212, Variant213, Variant214, Variant215, Variant216, Variant217, Variant218, Variant219, Variant220, Variant221, Variant222, Variant223, Variant224, Variant225, Variant226, Variant227, Variant228, Variant229, Variant230, Variant231, Variant232, Variant233, Variant234, Variant235, Variant236, Variant237, Variant238, Variant239, Variant240, Variant241, Variant242, Variant243, Variant244, Variant245, Variant246, Variant247, Variant248, Variant249, Variant250, Variant251, Variant252, Variant253, Variant254, Variant255, //Variant256, // <-- Uncomment to test compiler error! } impl fmt::Display for TestEnumComprehensive { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) } } fn get_variant_index(variant: TestEnumComprehensive) -> u32 { variant.to_string()[7..].parse::().unwrap() } #[test] fn test_basic() { let x = TestEnumBasic::VariantC; let y: u8 = x.into(); assert_eq!(y, 2); let z: TestEnumBasic = y.try_into().unwrap(); assert_eq!(x, z); let c = TestEnumBasic::try_from(2_u8); assert_eq!(c, Ok(TestEnumBasic::VariantC)); } #[test] fn test_bounds() { let x = 5; let v: Result = x.try_into(); assert!(v.is_err()); } #[test] fn test_comprehensive() { for i in 0_u8..=255_u8 { let e: TestEnumComprehensive = i.try_into().unwrap(); assert_eq!(i as u32, get_variant_index(e)); let v: u8 = e.into(); assert_eq!(i, v); } }