#smart-pointers #pointers #shared #cow #shared-ptr #string #rc

bos

Flexible Borrowed, Owned or Shared (B.O.S.) smart pointers. Like std's Cow but with Rc/Arc and without the ToOwned requirement

10 releases

0.3.1 Aug 22, 2021
0.3.0 Aug 22, 2021
0.2.2 Jul 24, 2021
0.1.1 Jul 18, 2021

#2399 in Rust patterns

MIT/Apache

48KB
1K SLoC

bos

Flexible Borrowed, Owned or Shared (B.O.S.) smart pointers. Like std's Cow but with Rc/Arc and without the ToOwned requirement.

Have a look at the documentation for more information.

Safety

This crate uses #![forbid(unsafe_code)]. We want to keep this crate 100% safe and its dependencies to a minimum. Currently this crate has no dependencies using unsafe code, unless you activate the serde feature, which brings in the serde crate, that has some code using unsafe.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


lib.rs:

This crate provides flexible Borrowed, Owned or Shared (B.O.S.) smart pointers. They are like std's Cow but with additonal Arc and Rc variants. Unlike std's Cow, they can also be used with types that don't implement Clone or ToOwned.

The bos smart pointers allow you to:

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

"A" stands for "atomic"

For example Abos implements Send and Sync but does not have a Rc variant.

[Bos] does not implement Send or Sync but does have a Rc variant.

AbosStr and BosStr types

Abos and [Bos] are often used with [str] or similar types. Feel free to have a look at our handy type aliases below.

features

Enable additional features or optional dependencies by adding the following to your Cargo.toml, replacing "x.y" with the version you want.

[dependencies]
bos = { version = "x.y", features = ["serde", "lifetime"] }

Examples

use std::sync::Arc;
use bos::AbosStr;

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

// The author's name shouldn't be too long.
// So we can just store it in a normal String.
let author = AbosStr::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 = AbosStr::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 AbosStr enum.
let book_infos: Vec<AbosStr> = vec![title, author, book_text];

fn read_file(path: &str) -> String {
// ...
}

Dependencies

~180KB