#macro #enum

caret

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

8 releases

Uses new Rust 2021

0.2.1 Jun 24, 2022
0.2.0 May 6, 2022
0.1.1 Apr 1, 2022
0.1.0 Mar 1, 2022
0.0.0 Jun 24, 2021

#152 in Rust patterns

Download history 927/week @ 2022-04-22 977/week @ 2022-04-29 955/week @ 2022-05-06 646/week @ 2022-05-13 609/week @ 2022-05-20 777/week @ 2022-05-27 415/week @ 2022-06-03 566/week @ 2022-06-10 1009/week @ 2022-06-17 1133/week @ 2022-06-24 620/week @ 2022-07-01 722/week @ 2022-07-08 1125/week @ 2022-07-15 826/week @ 2022-07-22 1367/week @ 2022-07-29 433/week @ 2022-08-05

3,823 downloads per month
Used in 19 crates (4 directly)

MIT/Apache

9KB
93 lines

caret

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