#声明性宏 #枚举 # #无 std

无 std numeric-enum-macro

类型安全的枚举到数字转换的声明性宏

2 个不稳定版本

0.2.0 2020 年 3 月 2 日
0.1.1 2020 年 2 月 25 日
0.1.0 2020 年 2 月 25 日

2864Rust 模式

Download history 161/week @ 2024-03-14 138/week @ 2024-03-21 199/week @ 2024-03-28 130/week @ 2024-04-04 190/week @ 2024-04-11 153/week @ 2024-04-18 105/week @ 2024-04-25 91/week @ 2024-05-02 134/week @ 2024-05-09 142/week @ 2024-05-16 225/week @ 2024-05-23 185/week @ 2024-05-30 127/week @ 2024-06-06 104/week @ 2024-06-13 106/week @ 2024-06-20 71/week @ 2024-06-27

每月 440 次下载

MIT/Apache

7KB
62

numeric-enum-macro

类型安全的枚举到数字转换的声明性宏。 无 std 支持!

use numeric_enum_macro::numeric_enum;

numeric_enum! {
    #[repr(i64)] // repr must go first.
    /// Some docs.
    ///
    /// Multiline docs works too.
    #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
    pub enum Lol {
        // All the constants must have explicit values assigned!
        Kek = 14,
        Wow = 87,
    }
}
// Conversion to raw number:
assert_eq!(14i64, Lol::Kek.into());
// Conversion from raw number:
assert_eq!(Ok(Lol::Wow), Lol::try_from(87));
// Unknown number:
assert_eq!(Err(88), Lol::try_from(88));

许可协议:MIT/Apache-2.0


lib.rs:

类型安全的枚举到数字转换的声明性宏。 无 std 支持!

use numeric_enum_macro::numeric_enum;

numeric_enum! {
    #[repr(i64)] // repr must go first.
    /// Some docs.
    ///
    /// Multiline docs works too.
    #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
    pub enum Lol {
        // All the constants must have explicit values assigned!
        Kek = 14,
        Wow = 87,
    }
}

const KEK: u32 = 0;
const WOW: u32 = 1;

numeric_enum! {
    #[repr(u32)] // repr must go first.
    /// Some docs.
    ///
    /// Multiline docs works too.
    #[derive(Debug, PartialEq, PartialOrd, Ord, Eq, Hash)] // all the attributes are forwarded!
    pub enum Lol2 {
        /// This is KEK
        Kek = KEK,
        /// And this is WOW
        Wow = WOW,
    }
}

// Conversion to raw number:
assert_eq!(14i64, Lol::Kek.into());
// Conversion from raw number:
assert_eq!(Ok(Lol::Wow), Lol::try_from(87));
// Unknown number:
assert_eq!(Err(88), Lol::try_from(88));

assert_eq!(Ok(Lol2::Wow), Lol2::try_from(WOW));

无运行时依赖