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§
- Boa
- Borrowed, Owned or Arc smart pointer.
- Boar
- Borrowed, Owned, Arc or Rc smart pointer.
- Boars
- Borrowed, Owned, Arc, Rc or Static smart pointer.
- Boas
- Borrowed, Owned, Arc or Static smart pointer.
Type Aliases§
- BoaBox
- Borrowed, Owned or Arc heap allocating smart pointer.
- BoaTo
- Borrowed, Owned or Arc smart pointer for types implementing the
ToOwned
(T.O.) trait. - BoaVec
- Borrowed, Owned or Arc
Vec
smart pointer. - BoarBox
- Borrowed, Owned, Arc or Rc heap allocating smart pointer.
- BoarTo
- Borrowed, Owned, Arc or Rc smart pointer for types implementing the
ToOwned
(T.O.) trait. - BoarVec
- Borrowed, Owned, Arc or Rc
Vec
smart pointer. - Boars
Box - Borrowed, Owned, Arc, Rc or Static heap allocating smart pointer.
- Boars
Str - Borrowed, Owned, Arc, Rc or Static
str
smart pointer. - BoarsTo
- Borrowed, Owned, Arc, Rc or Static smart pointer for types implementing the
ToOwned
(T.O.) trait. - Boars
Vec - Borrowed, Owned, Arc, Rc or Static
Vec
smart pointer. - BoasBox
- Borrowed, Owned, Arc or Static heap allocating smart pointer.
- BoasStr
- Borrowed, Owned, Arc or Static
str
smart pointer. - BoasTo
- Borrowed, Owned, Arc or Static smart pointer for types implementing the
ToOwned
(T.O.) trait. - BoasVec
- Borrowed, Owned, Arc or Static
Vec
smart pointer.