3 unstable releases

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

#504 in Rust patterns

Download history 8782/week @ 2024-09-26 8351/week @ 2024-10-03 9616/week @ 2024-10-10 10039/week @ 2024-10-17 9873/week @ 2024-10-24 10153/week @ 2024-10-31 8663/week @ 2024-11-07 9653/week @ 2024-11-14 8025/week @ 2024-11-21 9892/week @ 2024-11-28 10280/week @ 2024-12-05 10674/week @ 2024-12-12 6773/week @ 2024-12-19 6074/week @ 2024-12-26 9057/week @ 2025-01-02 10927/week @ 2025-01-09

34,732 downloads per month
Used in 162 crates (3 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