Skip to main content

Module variable

Module variable 

Source
Expand description

Canonical, order-preserving variable-length byte encodings for str, String, [u8] and Vec<u8>.

The encoding is the value’s own bytes, borrowed: a string encodes to its UTF-8 bytes, and a byte string to itself. No copy is made, so no second copy of the plaintext is left in memory.

Byte-wise lexicographic order is already the order these types define, with a value sorting before any longer value it is a prefix of ("ab" < "abc"). For str this is Unicode code-point order, because UTF-8 preserves it. Byte equality is value equality.

§No normalisation

A string is encoded exactly as given. Two strings that Unicode considers the same text but that use different code points (é as U+00E9, or as e followed by U+0301) encode differently, and sort by code point rather than by any language’s collation. Callers that need canonical equivalence, case folding or accent folding must normalise before encoding.

§Never pad, never concatenate

These encodings do not mark where they end. A consumer must keep each value’s length and compare by length when one value is a prefix of the other, as a variable-length scheme does. Two misuses break the guarantees:

  • Zero-padding to a fixed block size makes "a" and "a\0" encode identically.
  • Concatenating two values loses the boundary between them, so ("ab", "c") and ("a", "bc") encode identically.

Fixed-length encodings (see crate::FixedOrderableBytes) are safe to zero-extend; these are not. That is why these types implement crate::VariableOrderableBytes and not crate::FixedOrderableBytes: code that pads must bound on the latter, and then rejects these types at compile time.