Crates.io | thisenum-impl |
lib.rs | thisenum-impl |
version | |
source | src |
created_at | 2024-08-15 19:25:10.233145+00 |
updated_at | 2025-02-19 00:53:13.939292+00 |
description | General implementation of `thisenum`. |
homepage | |
repository | https://github.com/arpadav/thisenum |
max_upload_size | |
id | 1339207 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
The simplest way to assign constant literals to enum arms in Rust! What fun!
Please also see enum-assoc, which is a more robust alternative.
use thisenum::Const;
#[derive(Const, Debug)]
#[armtype(&[u8])]
/// https://exiftool.org/TagNames/EXIF.html
enum ExifTag {
// ...
#[value = b"\x01\x00"]
ImageWidth,
#[value = b"\x01\x01"]
ImageHeight,
#[value = b"\x01\x02"]
BitsPerSample,
#[value = b"\x01\x03"]
Compression,
#[value = b"\x01\x06"]
PhotometricInterpretation,
// ...
}
assert_eq!(ExifTag::ImageWidth.value(), b"\x01\x00");
#[cfg(feature = "eq")]
assert_eq!(ExifTag::ImageWidth, b"\x01\x00");
If each arm is a different type, this is still possible using ConstEach
:
use thisenum::ConstEach;
#[derive(ConstEach, Debug)]
enum CustomEnum {
#[armtype(&[u8])]
#[value = b"\x01\x00"]
A,
// `armtype` is not required, type is inferred
#[value = "foo"]
B,
#[armtype(f32)]
#[value = 3.14]
C,
}
assert_eq!(CustomEnum::A.value::<&[u8]>().unwrap(), b"\x01\x00");
assert!(CustomEnum::B.value::<&str>().is_some());
assert_eq!(CustomEnum::B.value::<&str>().unwrap(), &"foo");
assert_eq!(CustomEnum::B.value::<&str>(), Some("foo").as_ref());
assert_eq!(CustomEnum::C.value::<f32>().unwrap(), &3.14);
// or on failure
assert!(CustomEnum::C.value::<i32>().is_none());
thisenum
is released under the MIT License http://opensource.org/licenses/MIT.