pub trait ScribeCowStr {
    // Required method
    fn scribe(&self) -> Cow<'static, str>;
}
Expand description

Trait for converting an enum to a clone-on-write string.

Like all of the traits provided by enumscribe, this should not be implemented manually; use #[derive(ScribeCowStr)] 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.

A maximum of one variant can be annotated with #[enumscribe(other)]. This variant must have exactly one field, which must implement Into<String>. Converting this variant to a string will result in whatever the value of its field is.

This trait can only be used if none of the enum’s variants use ignore. If you have variants that use ignore, use TryScribeCowStr instead.

use std::borrow::Cow;

use enumscribe::ScribeCowStr;

#[derive(ScribeCowStr, PartialEq, Eq, Debug)]
enum Airport {
    #[enumscribe(str = "LHR")]
    Heathrow,
    #[enumscribe(str = "LGW")]
    Gatwick,
    #[enumscribe(other)]
    Other(String),
}

assert_eq!(Airport::Heathrow.scribe(),
           Cow::Borrowed("LHR"));
assert_eq!(Airport::Gatwick.scribe(),
           Cow::Borrowed("LGW"));
assert_eq!(Airport::Other("STN".to_owned()).scribe(),
           Cow::Owned::<'static, str>("STN".to_owned()));

Required Methods§

source

fn scribe(&self) -> Cow<'static, str>

Converts this enum to a Cow<'static, str>.

When called on a variant marked with #[enumscribe(other)], the variant’s field will be returned as a Cow::Owned. For other variants, a Cow::Borrowed is returned, containing a static string slice determined by the #[enumscribe(str = "...")] attribute, or the name of the variant if the attribute is omitted.

Implementors§