Crate beef

source ·
Expand description

Faster, more compact implementation of Cow.

Changelog - Cargo - Repository

use beef::Cow;

let borrowed: Cow<str> = Cow::borrowed("Hello");
let owned: Cow<str> = Cow::owned(String::from("World"));

assert_eq!(
    format!("{} {}!", borrowed, owned),
    "Hello World!",
);

There are two versions of Cow exposed by this crate:

  • beef::Cow is 3 words wide: pointer, length, and capacity. It stores the ownership tag in capacity.
  • beef::lean::Cow is 2 words wide, storing length, capacity, and the ownership tag all in one word.

Both versions are leaner than the std::borrow::Cow:

use std::mem::size_of;

const WORD: usize = size_of::<usize>();

assert_eq!(size_of::<std::borrow::Cow<str>>(), 4 * WORD);
assert_eq!(size_of::<beef::Cow<str>>(), 3 * WORD);

// Lean variant is two words on 64-bit architecture
#[cfg(target_pointer_width = "64")]
assert_eq!(size_of::<beef::lean::Cow<str>>(), 2 * WORD);

Modules§

  • This module contains the actual, albeit generic, implementaiton of the Cow, and the traits that are available to it.
  • Namespace containing the 2-word Cow implementation.

Type Aliases§

  • Compact three word Cow that puts the ownership tag in capacity. This is a type alias, for documentation see beef::generic::Cow.