Comment by parkmycar

1 year ago

Author of compact_str here, you hit the nail on the head, great explanation!

> ... and another (217) signifies that it's a weird static allocation (I do not know why you'd do this)

In addition to String Rust also has str[1], which is an entirely different type. It's common to represent string literals known at compile time as `&'static str`, but they can't be used in all of the same places that a String can. For example, you can't put a &'static str into a Vec<String> unless you first heap allocate and create a String. We added the additional variant of 217 so users of CompactString could abstract over both string literals and strings created at runtime to solve cases like the example.

[1]: https://doc.rust-lang.org/std/primitive.str.html

Thanks! And the explanation of 217 makes sense too.

Since I have you here, wouldn't it be better to name that type "LastByte" or something? It's not a (Rust) char, and it's not necessarily UTF-8 whereas it is definitely the last byte.

  • Ha, naming is hard! You’re totally right, it used to be just the values of the last byte of a UTF-8 char (and before that it was NonMaxU8) but now represents more. I’ll update it once I’m back at my computer, thanks!

    • Ah, while you're in there, IIRC you mention NonMaxU8 in the code still and probably that's worth updating at the same time.