10 releases (4 breaking)

0.5.1 Feb 24, 2024
0.5.0 Feb 24, 2024
0.4.1 Dec 27, 2023
0.3.0 Oct 2, 2023
0.1.1 Aug 19, 2023

#139 in Algorithms

Download history 10301/week @ 2024-07-21 12559/week @ 2024-07-28 9883/week @ 2024-08-04 9830/week @ 2024-08-11 10130/week @ 2024-08-18 11782/week @ 2024-08-25 12770/week @ 2024-09-01 17853/week @ 2024-09-08 13476/week @ 2024-09-15 8150/week @ 2024-09-22 10635/week @ 2024-09-29 8174/week @ 2024-10-06 7629/week @ 2024-10-13 5171/week @ 2024-10-20 5751/week @ 2024-10-27 6129/week @ 2024-11-03

25,061 downloads per month
Used in 2 crates

Apache-2.0

77KB
1.5K SLoC

byteyarn

byteyarn - Space-efficient byte strings πŸ§ΆπŸˆβ€β¬›

A Yarn is a highly optimized string type that provides a number of useful properties over String:

  • Always two pointers wide, so it is always passed into and out of functions in registers.
  • Small string optimization (SSO) up to 15 bytes on 64-bit architectures.
  • Can be either an owned buffer or a borrowed buffer (like Cow<str>).
  • Can be upcast to 'static lifetime if it was constructed from a known-static string.
  • Option<Yarn> has the same size and ABI as Yarn.

The main caveat is that Yarns cannot be easily appended to, since they do not track an internal capacity, and the slice returned by Yarn::as_slice() does not have the same pointer stability properties as String (these are rarely needed, though).


Yarns are useful for situations in which a copy-on-write string is necessary and most of the strings are relatively small. Although Yarn itself is not Copy, there is a separate YarnRef type that is. These types have equivalent representations, and can be cheaply cast between each other.

The easiest way to create a yarn is with the yarn!() macro, which is similar to format!().

// Create a new yarn via `fmt`ing.
let yarn = yarn!("Answer: {}", 42);

// Convert that yarn into a reference.
let ry: YarnRef<str> = yarn.as_ref();

// Try up-casting the yarn into an "immortal yarn" without copying.
let copy: YarnRef<'static, str> = ry.immortalize().unwrap();

assert_eq!(yarn, copy);

Yarns are intended for storing text, either as UTF-8 or as probably-UTF-8 bytes; Yarn<str> and Yarn<u8> serve these purposes, and can be inter-converted with each other. The Yarn::utf8_chunks() function can be used to iterate over definitely-valid-UTF-8 chunks within a string.

Both kinds of yarns can be Debuged and Displayed, and will print out as strings would. In particular, invalid UTF-8 is converted into either \xNN escapes or replacement characters (for Debug and Display respectively).

let invalid = ByteYarn::from_byte(0xff);
assert_eq!(format!("{invalid:?}"), r#""\xFF""#);
assert_eq!(format!("{invalid}"), "οΏ½");

Dependencies

~0.6–1MB
~12K SLoC