Crate boar[][src]

Expand description

This crate provides flexible clone on write smart pointers with Borrowed, Owned, Arc, Rc and Static variants. They are like std’s Cow but with additonal Arc, Rc and Static variants. Unlike std’s Cow, they can also be used with types that don’t implement Clone or ToOwned.

The boar smart pointers allow you to:

  • share data efficiently
  • decide ownership at runtime
  • avoid cloning the data until it becomes necessary

Examples

use std::sync::Arc;
use boar::BoasStr;

// No need to allocate memory for a &'static str
let title = BoasStr::Static("Dune");

// Also no need to allocate memory for a &str, that's valid for the required scope
let title = read_file("title.txt")?;
let title = BoasStr::Borrowed(&title);

// The author's name shouldn't be too long.
// So we can just store it in a normal String.
let author = BoasStr::Owned(read_file("dune/author.txt")?);

// A whole book is really long.
// And we want to share this string with multiple threads.
// So it would be efficient to store in an Arc,
// since cloning the Arc does not clone the String data inside of it.
let book_text = BoasStr::Arc(Arc::new(read_file("dune/book.txt")?));

// We can store a &str, String and Arc<String> inside the same Vec
// because we are using the BoasStr enum.
let book_infos: Vec<BoasStr> = Vec::from([title, author, book_text]);

fn read_file(path: &str) -> std::io::Result<String> {
    // ...
}

Enums

Borrowed, Owned or Arc smart pointer.

Borrowed, Owned, Arc or Rc smart pointer.

Borrowed, Owned, Arc, Rc or Static smart pointer.

Borrowed, Owned, Arc or Static smart pointer.

Type Definitions

Borrowed, Owned or Arc heap allocating smart pointer.

Borrowed, Owned or Arc smart pointer for types implementing the ToOwned (T.O.) trait.

Borrowed, Owned or Arc Vec smart pointer.

Borrowed, Owned, Arc or Rc heap allocating smart pointer.

Borrowed, Owned, Arc or Rc smart pointer for types implementing the ToOwned (T.O.) trait.

Borrowed, Owned, Arc or Rc Vec smart pointer.

Borrowed, Owned, Arc, Rc or Static heap allocating smart pointer.

Borrowed, Owned, Arc, Rc or Static str smart pointer.

Borrowed, Owned, Arc, Rc or Static smart pointer for types implementing the ToOwned (T.O.) trait.

Borrowed, Owned, Arc, Rc or Static Vec smart pointer.

Borrowed, Owned, Arc or Static heap allocating smart pointer.

Borrowed, Owned, Arc or Static str smart pointer.

Borrowed, Owned, Arc or Static smart pointer for types implementing the ToOwned (T.O.) trait.

Borrowed, Owned, Arc or Static Vec smart pointer.