#enums #integer-value #macro #field-value

caret

Macros for declaring non-exhaustive C-style enumerations, with named members

21 releases

0.5.0 Sep 30, 2024
0.4.6 Jun 5, 2024
0.4.4 Sep 5, 2023
0.4.2 Jun 30, 2023
0.0.0 Jun 24, 2021

#542 in Rust patterns

Download history 1183/week @ 2024-08-17 1601/week @ 2024-08-24 2031/week @ 2024-08-31 1726/week @ 2024-09-07 1262/week @ 2024-09-14 1401/week @ 2024-09-21 1587/week @ 2024-09-28 1136/week @ 2024-10-05 1126/week @ 2024-10-12 1180/week @ 2024-10-19 1122/week @ 2024-10-26 2308/week @ 2024-11-02 1243/week @ 2024-11-09 2439/week @ 2024-11-16 1809/week @ 2024-11-23 2109/week @ 2024-11-30

7,807 downloads per month
Used in 52 crates (6 directly)

MIT/Apache

9KB
102 lines

caret

Integers with some named values.

Crikey! Another Rust Enum Tool?

Suppose you have an integer type with some named values. For example, you might be implementing a protocol where "command" can be any 8-bit value, but where only a small number of commands are recognized.

In that case, you can use the caret_int macro to define a wrapper around u8 so named values are displayed with their preferred format, but you can still represent all the other values of the field:

use caret::caret_int;
caret_int!{
    struct Command(u8) {
       Get = 0,
       Put = 1,
       Swap = 2,
    }
}

let c1: Command = 2.into();
let c2: Command = 100.into();

assert_eq!(c1.to_string().as_str(), "Swap");
assert_eq!(c2.to_string().as_str(), "100");

assert_eq!(c1, Command::Swap);

This crate is developed as part of Arti, a project to implement Tor in Rust. Many other crates in Arti depend on it, but it should be of general use.

License: MIT OR Apache-2.0

No runtime deps

Features