https://github.com/coolreader18/e_num
Serialize enums into numbers.
https://github.com/coolreader18/e_num
enum rust serialization
Last synced: 5 months ago
JSON representation
Serialize enums into numbers.
- Host: GitHub
- URL: https://github.com/coolreader18/e_num
- Owner: coolreader18
- License: mit
- Created: 2018-11-21T23:36:10.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2018-11-24T07:09:08.000Z (about 7 years ago)
- Last Synced: 2025-08-27T15:16:13.620Z (5 months ago)
- Topics: enum, rust, serialization
- Language: Rust
- Homepage:
- Size: 15.6 KB
- Stars: 2
- Watchers: 0
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# e_num
> Serialize enums into numbers.
## Warning
This library works with variant fields (e.g. `Variant1(u64)`) by bitshifting the
number representation of the contained value over enough so that the tagging can
fit on the right of the number. If you're dealing with very large numbers in the
fields or have a ton of variants, data on the left side of the value will likely
be lost.
## Usage
```rust
#[macro_use]
extern crate e_num;
use e_num::ENum;
#[derive(ENum)]
enum A {
B,
C(u64),
}
fn main() {
let b: usize = A::B.to_num();
println!("b as a number: {:#b}", b);
let b = A::from_num(b);
assert!(match b {
A::B => true,
_ => false,
});
let c = A::C(85).to_num();
println!("c as a number: {:#b}", c);
let c = A::from_num(c);
assert!(match c {
A::C(inner) => {
assert_eq!(inner, 85);
true
}
_ => false,
});
}
```
## License
This project is licensed under the MIT license. See the [LICENSE](LICENSE) file
for more details.