Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boxbeam/constant_pool
A rust macro for making constant pools much like Java enums
https://github.com/boxbeam/constant_pool
Last synced: 2 days ago
JSON representation
A rust macro for making constant pools much like Java enums
- Host: GitHub
- URL: https://github.com/boxbeam/constant_pool
- Owner: boxbeam
- License: mit
- Created: 2022-01-28T22:25:45.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2022-01-30T18:34:16.000Z (almost 3 years ago)
- Last Synced: 2024-11-05T23:09:33.169Z (about 2 months ago)
- Language: Rust
- Size: 5.86 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# constant_pool
A rust macro for making constant pools much like Java enums# Usage
To define a constant pool, you must first define a struct or tuple struct type.If you are defining a struct type, it must have a field of type `&str` called `name`. If it is a tuple struct, the first value must be of type `&str`.
Examples:
```rs
struct Color(&'static str, u8, u8, u8);
``````rs
struct Team {
name: &'static str,
number: u8
}
```Once your struct is defined, you can define the constant pool using the `constant_pool!` macro:
```rs
constant_pool! {
pub Colors: Color {
RED(255, 0, 0),
BLUE(0, 0, 255),
GREEN(0, 255, 0),
YELLOW(0, 255, 255)
}
}
``````rs
constant_pool! {
pub Teams: Team {
WHITE{number: 1},
BLACK{number: 2},
ORANGE{number: 3}
}
}
```The name of the constant pool is defined, followed by the struct type it will use. Once defined, it creates a namespace with a module for the constant pool, populating it with all the constants you defined. For the above examples, you could access `Color::RED` and `Team::WHITE` respectively.
Additionally, the constant pool generates a slice containing all of the constant values, under `Namespace::values`. For our examples, this will be `Color::values` and `Team::values`. It will also generate a function to get any of the values by name, returning an `Option<&T>`:
```rs
let color: &Color = Color::by_name("RED").unwrap();
let team = Team::by_name("ORANGE").unwrap();
```