10 releases (3 stable)

1.2.0 Feb 20, 2024
1.1.0 Oct 31, 2023
1.0.0 Jul 21, 2023
0.5.0 Mar 15, 2023
0.2.1 Mar 4, 2019

#32 in Concurrency

Download history 13463/week @ 2023-12-23 23217/week @ 2023-12-30 30819/week @ 2024-01-06 33146/week @ 2024-01-13 32557/week @ 2024-01-20 32968/week @ 2024-01-27 29190/week @ 2024-02-03 34336/week @ 2024-02-10 34015/week @ 2024-02-17 37944/week @ 2024-02-24 36801/week @ 2024-03-02 39283/week @ 2024-03-09 37048/week @ 2024-03-16 38173/week @ 2024-03-23 37467/week @ 2024-03-30 30006/week @ 2024-04-06

150,520 downloads per month
Used in 44 crates (4 directly)

MPL-2.0 license

61KB
1K SLoC

Build Status Code Coverage Dependency status crates.io Downloads Github stars Documentation License

archery

archery is a rust library that offers a way to abstraction over Rc and Arc smart pointers. This allows you to create data structures where the pointer type is parameterizable, so you can avoid the overhead of Arc when you don’t need to share data across threads.

In languages that supports higher-kinded polymorphism this would be simple to achieve without any library, but rust does not support that yet. To mimic higher-kinded polymorphism archery implements the approach suggested by Joshua Liebow-Feeser in “Rust has higher kinded types already… sort of”. While other approaches exist, they seem to always offer poor ergonomics for the user.

Setup

To use archery add the following to your Cargo.toml:

[dependencies]
archery = "<version>"

Using archery

archery defines a SharedPointer that receives the kind of pointer as a type parameter. This gives you a convenient and ergonomic way to abstract the pointer type away.

Example

Declare a data structure with the pointer kind as a type parameter bounded by SharedPointerKind:

use archery::*;

struct KeyValuePair<K, V, P: SharedPointerKind> {
    pub key: SharedPointer<K, P>,
    pub value: SharedPointer<V, P>,
}

impl<K, V, P: SharedPointerKind> KeyValuePair<K, V, P> {
    fn new(key: K, value: V) -> KeyValuePair<K, V, P> {
        KeyValuePair {
            key: SharedPointer::new(key),
            value: SharedPointer::new(value),
        }
    }
}

To use it just plug-in the kind of pointer you want:

let pair: KeyValuePair<_, _, RcK> =
    KeyValuePair::new("António Variações", 1944);

assert_eq!(*pair.value, 1944);

triomphe::Arc

You can also use triomphe::Arc as the backing implementation of a SharedPointer. This is generally faster than std::sync::Arc. Read triomphe’s crate documentation to learn more about it.

To use it you need to enable the triomphe feature in archery. Use ArcTK as the pointer kind in SharedPointer.

Serialization

We support serialization through serde. To use it enable the serde feature. To do so change the archery dependency in your Cargo.toml to

[dependencies]
archery = { version = "<version>", features = ["serde"] }

Limitations

Currently it is not possible to have unsized types inside a SharedPointer. As a workaround you can put the unsized type inside a Box.

Alternative approaches

An alternative to the approach taken by archery is to use traits with associated types to encode type-level functions. This has been suggested multiple times, but offers ugly ergonomics (see here and here).

Dependencies

~245KB