Convert string literal into bytestring literal

i'm writing a custom string type, and i have a declarative macro that can be used to construct it from bytestring literals, like my_str!(b"string data"). i want this macro to be able to also accept plain string literals, such as my_str!("string data"), but i'm not sure what the best way to do that is.

using a simple procedural macro that converts any string literal into a bytestring literal should work, and it would be quite handy if someone already has written a crate that does this, but i'm not sure..

the other option is to make the underlying constructor function generic over AsRef<[u8]>, allowing it to accept str arguments, unfortunatly that doesn't work, since i need the argument to be converted into an array with known length, not a DST.

You can use AsRef<[u8]> and then TryInto<[u8; N]>.

calling trait methods in constant expressions is not possible in stable rust

Well then you can't even use AsRef. You'll probably need to make this a procedural macro and check what kind of literal it is.

i was hoping someone had already written this, but it seems not.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.