A crate for defining C-style string enums.
C APIs sometimes require string constants. One could define a bunch of &CStr constants using the
constr_cstr crate, but this becomes unergonomic with a large number of constants.
It also does not allow the type checking Rust's enums provide.
This crate provides two traits for converting between to and from &CStr: AsCStr and FromCStr. It also provides
derive macros for implementing these traits on enums. The implementations provided
by the derive macros perform no allocations, using only static [u8] buffers.
```rust use cstrenum::*; use std::ffi::CStr; use std::os::raw::cchar;
enum Constants { Apple, Bacon, Cat = 1337, // user discriminants supported }
asserteq!(Constants::Apple.ascstr().tobyteswith_nul(), b"Apple\0");
let returnedfromcapi = CStr::frombyteswithnul(b"Cat\0").unwrap(); asserteq!(Constants::fromcstr(returnedfromc_api), Ok(Constants::Cat));
let returnedfromcapi = CStr::frombyteswithnul(b"unknown\0").unwrap(); asserteq!( Constants::fromcstr(returnedfromc_api), Err("unexpected string while parsing for Constants variant") ); ```
Licensed under MIT.