Macros operate on streams of tokens, not literal text. When you try and stick the b in front of the string literal it produces a different stream of tokens than a byte string does. Specifically an ident token b followed by a normal string literal, as opposed to the single token of a byte string literal.
You just need to create a byte string literal[1] directly before passing it to quote
use proc_macro::TokenStream;
use proc_macro2::Literal;
use quote::quote;
#[proc_macro]
pub fn bytes(_tokens: TokenStream) -> TokenStream {
let bytes = "hello, world!".as_bytes();
let s = Literal::byte_string(bytes);
let len = bytes.len();
quote! {
pub static DATA: [u8; #len] = *#s;
}
.into()
}
Note that quote allows you to use proc_macro2 types, but not proc_macro types directly. So you'll need to add a dependency on proc_macro2 if you don't already have one âŠī¸