Trait enumscribe::ScribeStaticStr
source · pub trait ScribeStaticStr {
// Required method
fn scribe(&self) -> &'static str;
}
Expand description
Trait for converting an enum to a static string slice.
Like all of the traits provided by enumscribe, this should not be implemented manually; use
#[derive(ScribeStaticStr)]
provided by the
enumscribe_derive crate instead.
When deriving this trait, you may specify the string that a particular variant should be
converted to by annotating it with #[enumscribe(str = "foo")]
. If this is omitted, the name
of the variant will be used instead.
This trait can only be used if none of the enum’s variants use ignore
or other
. If you have
variants that use ignore
, use TryScribeStaticStr instead. If you have variants that use
other
, use ScribeCowStr. If you have variants that use both, use TryScribeCowStr.
use enumscribe::ScribeStaticStr;
#[derive(ScribeStaticStr, PartialEq, Eq, Debug)]
enum Airport {
#[enumscribe(str = "LHR")]
Heathrow,
#[enumscribe(str = "LGW")]
Gatwick,
UnnamedAirport,
}
assert_eq!(Airport::Heathrow.scribe(), "LHR");
assert_eq!(Airport::Gatwick.scribe(), "LGW");
assert_eq!(Airport::UnnamedAirport.scribe(), "UnnamedAirport");