You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm often running into a case where I'd want to derive discriminant on arbitrary enums. It's even more useful nowadays now that arbitrary_enum_discriminants are stabilized in Rust, so you can customize them and not only use the default order.
For example, deriving errors with associated error codes could then look like:
use num_enum::PrimitiveDiscriminant;use displaydoc::Display;use thiserror::Error;#[derive(Display,Debug,Error,PrimitiveDiscriminant)]#[repr(u16)]pubenumDataStoreError{/// data store disconnectedDisconnect(#[source] io::Error) = 0x201,/// the data for key `{0}` is not availableRedaction(String) = 0x205,/// invalid header (expected {expected:?}, found {found:?})InvalidHeader{expected:String,found:String,} = 0x300,/// unknown data store errorUnknown = 0,}fnprint_err(err:&DataStoreError){eprintln!("Error #{:X}: {}", err.discriminant(), err);}
This crate seems like the perfect fit for it since it has all the necessary machinery for dealing with discriminant on enums and would only need one extra trait+derive for the non-owned version of IntoPrimitive.
The text was updated successfully, but these errors were encountered:
I'm often running into a case where I'd want to derive discriminant on arbitrary enums. It's even more useful nowadays now that arbitrary_enum_discriminants are stabilized in Rust, so you can customize them and not only use the default order.
For example, deriving errors with associated error codes could then look like:
This crate seems like the perfect fit for it since it has all the necessary machinery for dealing with discriminant on enums and would only need one extra trait+derive for the non-owned version of
IntoPrimitive
.The text was updated successfully, but these errors were encountered: