I have macros that generate FromStr
for enum
.
macro_rules! define_enum_with_str_values {
(
$(#[$outer:meta])*
enum $Name:ident {
default = $Default:ident,
$($Variant:ident => $StrVar:literal),*
$(,)*
}
) => { ... }
}
define_enum_with_str_values!(enum Foo {
default = A,
A => "A",
B => "B"
});
All works fine except in half cases I need convert from bytes instead of string,
and I do not want to waste CPU cycles for from_utf8
call.
So I want to write inside macro_rules
:
impl TryFrom<&[u8]> for $Name {
type Error = ();
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
match value {
$(
b$StrVar => Ok($Name :: $Variant) , <== HERE ERROR
)*
_ => Err(()),
}
}
}
and can not get this to comile. So how should I add b
prefix to string literal in macros ?