#reference-counting #arc #thread-safe #immutability #owned #cow

atomicow

A Cow-like data structure where owned data is stored inside an Arc

1 stable release

1.0.0 Aug 29, 2024

#453 in Data structures

Download history 66/week @ 2024-08-23 2567/week @ 2024-08-30 2757/week @ 2024-09-06 2920/week @ 2024-09-13 4432/week @ 2024-09-20 5896/week @ 2024-09-27 6038/week @ 2024-10-04 3953/week @ 2024-10-11 3017/week @ 2024-10-18 3313/week @ 2024-10-25 3691/week @ 2024-11-01 4408/week @ 2024-11-08 3123/week @ 2024-11-15 3500/week @ 2024-11-22 10447/week @ 2024-11-29 7723/week @ 2024-12-06

25,226 downloads per month
Used in 210 crates (via bevy_asset)

MIT/Apache

10KB
166 lines

atomicow

A Cow-like data structure where owned data is stored inside an Arc. Here's what it looks like:

pub enum CowArc<'a, T: ?Sized + 'static> {
    Borrowed(&'a T),
    Static(&'static T),
    Owned(Arc<T>),
}

As implied by the Cow name, this type allows for cheap immutable reference, but can be converted into an owned form via cloning when mutation or ownership is required.

This data structure is particularly useful for str or other values with a static lifetime, as might be used in structures such as asset paths. A 'static str stored in a CowArc can be cloned without allocations or bookkeeping, while owned values are shared by reference-counting in a thread-safe fashion.

Comparison to the cow_arc crate

The similar cow_arc crate already exists. How does atomicow differ?

Put simply: cow_arc's data structure is just a wrapper over an Arc. While this is exactly what you need in some use cases, the enum structure used in atomicow is both more transparent and more flexible.

Contributing

This crate is maintained by the Bevy organization, and is intended to be tiny, stable, zero-dependency, and broadly useful. Issues and pull requests are genuinely welcome!

No runtime deps