Crates.io | safe-discriminant |
lib.rs | safe-discriminant |
version | 0.2.0 |
source | src |
created_at | 2024-08-16 07:45:03.12041 |
updated_at | 2024-08-19 20:59:18.611782 |
description | A minimalistic, `no_std` compatible trait and procedural macro for extracting discriminants from enums at zero cost. |
homepage | https://docs.rs/safe_discriminant |
repository | https://github.com/oddcoder/safe-discriminant |
max_upload_size | |
id | 1340049 |
size | 14,602 |
safe-discriminant
provides a minimalistic, no_std
compatible trait and
procedural macro for extracting discriminants from enums at zero cost. It
automatically generates unsafe { ... }
blocks, ensuring semantic safety so
you don’t have to worry about it.
This crate is available on crates.io and can be easily included in your project by:
[dependencies]
safe-discriminant = "0.2.0"
$ cargo add safe-discriminant
use safe_discriminant::Discriminant;
#[derive(Discriminant)]
#[repr(i64)]
pub enum Foo<T> {
A = 1,
B(T) = -1,
C { fst: T, snd: T } = -2,
}
fn main() {
let a: Foo<u8> = Foo::A;
let b = Foo::B(5);
let c = Foo::C { fst: 2, snd: 3 };
assert_eq!(a.discriminant(), 1);
assert_eq!(b.discriminant(), -1);
assert_eq!(c.discriminant(), -2);
}
EnumDiscriminants
,
which extracts the name of each variant from the enum and organizes them into
a separate enum.