#smart-pointers #reference-counting #immutability #cloning #string #cow #values

oco_ref

A smart pointer for storing immutable values with relatively-cheap cloning. (Like a Cow meets an Rc!)

3 unstable releases

0.2.0 Apr 27, 2024
0.1.1 Apr 16, 2024
0.1.0 Apr 16, 2024

#450 in Rust patterns

Download history 5524/week @ 2024-07-22 6252/week @ 2024-07-29 4915/week @ 2024-08-05 6476/week @ 2024-08-12 6029/week @ 2024-08-19 6329/week @ 2024-08-26 6722/week @ 2024-09-02 7458/week @ 2024-09-09 5658/week @ 2024-09-16 8021/week @ 2024-09-23 8503/week @ 2024-09-30 9936/week @ 2024-10-07 8958/week @ 2024-10-14 10358/week @ 2024-10-21 8828/week @ 2024-10-28 10478/week @ 2024-11-04

39,202 downloads per month
Used in 154 crates (2 directly)

MIT license

22KB
494 lines

This module contains the Oco (Owned Clones Once) smart pointer, which is used to store immutable references to values. This is useful for storing, for example, strings.

Imagine this as an alternative to [Cow] with an additional, reference-counted branch.

use oco_ref::Oco;
use std::sync::Arc;

let static_str = "foo";
let arc_str: Arc<str> = "bar".into();
let owned_str: String = "baz".into();

fn uses_oco(value: impl Into<Oco<'static, str>>) {
    let mut value = value.into();

    // ensures that the value is either a reference, or reference-counted
    // O(n) at worst
    let clone1 = value.clone_inplace();

    // these subsequent clones are O(1)
    let clone2 = value.clone();
    let clone3 = value.clone();
}

uses_oco(static_str);
uses_oco(arc_str);
uses_oco(owned_str);

lib.rs:

This module contains the Oco (Owned Clones Once) smart pointer, which is used to store immutable references to values. This is useful for storing, for example, strings.

Dependencies

~0.3–1MB
~21K SLoC