archery/lib.rs
1#![no_std]
2// Note: If you change this remember to update `README.md`. To do so run `cargo rdme`.
3//! `archery` is a rust library that offers a way to abstraction over
4//! [`Rc`](::alloc::rc::Rc) and
5//! [`Arc`](::alloc::sync::Arc) smart pointers.
6//! This allows you to create data structures where the pointer type is parameterizable, so you can
7//! [avoid the overhead of `Arc`](::alloc::sync::Arc#thread-safety)
8//! when you don’t need to share data across threads.
9//!
10//! In languages that supports
11//! [higher-kinded polymorphism](https://en.wikipedia.org/wiki/Type_class#Higher-kinded_polymorphism)
12//! this would be simple to achieve without any library, but
13//! [rust does not support that yet](https://github.com/rust-lang/rfcs/issues/324).
14//! To mimic higher-kinded polymorphism `archery` implements the approach suggested by
15//! Joshua Liebow-Feeser in
16//! “[Rust has higher kinded types already… sort of](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/)”.
17//! While [other approaches](#alternative-approaches) exist, they seem to always offer poor
18//! ergonomics for the user.
19//!
20//! # Setup
21//!
22//! To use `archery` add the following to your `Cargo.toml`:
23//!
24//! ```toml
25//! [dependencies]
26//! archery = "<version>"
27//! ```
28//!
29//! # Using `archery`
30//!
31//! `archery` defines a [`SharedPointer`](crate::shared_pointer::SharedPointer)
32//! that receives the [kind of pointer](crate::shared_pointer::kind::SharedPointerKind)
33//! as a type parameter. This gives you a convenient and ergonomic way to abstract the pointer
34//! type away.
35//!
36//! ## Example
37//!
38//! Declare a data structure with the pointer kind as a type parameter bounded by
39//! [`SharedPointerKind`](crate::shared_pointer::kind::SharedPointerKind):
40//!
41//! ```rust
42//! use archery::*;
43//!
44//! struct KeyValuePair<K, V, P: SharedPointerKind> {
45//! pub key: SharedPointer<K, P>,
46//! pub value: SharedPointer<V, P>,
47//! }
48//!
49//! impl<K, V, P: SharedPointerKind> KeyValuePair<K, V, P> {
50//! fn new(key: K, value: V) -> KeyValuePair<K, V, P> {
51//! KeyValuePair {
52//! key: SharedPointer::new(key),
53//! value: SharedPointer::new(value),
54//! }
55//! }
56//! }
57//! ```
58//!
59//! To use it just plug-in the kind of pointer you want:
60//!
61//! ```rust
62//! # use archery::*;
63//! #
64//! # struct KeyValuePair<K, V, P: SharedPointerKind> {
65//! # pub key: SharedPointer<K, P>,
66//! # pub value: SharedPointer<V, P>,
67//! # }
68//! #
69//! # impl<K, V, P: SharedPointerKind> KeyValuePair<K, V, P> {
70//! # fn new(key: K, value: V) -> KeyValuePair<K, V, P> {
71//! # KeyValuePair {
72//! # key: SharedPointer::new(key),
73//! # value: SharedPointer::new(value),
74//! # }
75//! # }
76//! # }
77//! #
78//! let pair: KeyValuePair<_, _, RcK> =
79//! KeyValuePair::new("António Variações", 1944);
80//!
81//! assert_eq!(*pair.value, 1944);
82//! ```
83//!
84//! ## `triomphe::Arc`
85//!
86//! You can also use [`triomphe::Arc`](https://docs.rs/triomphe/latest/triomphe/struct.Arc.html)
87//! as the backing implementation of a [`SharedPointer`](crate::shared_pointer::SharedPointer).
88//! This is generally faster than [`std::sync::Arc`](::alloc::sync::Arc).
89//! Read [`triomphe`’s crate documentation](https://docs.rs/triomphe/latest/triomphe/) to learn more
90//! about it.
91//!
92//! To use it you need to enable the `triomphe` feature in `archery`. Use `ArcTK` as the pointer
93//! kind in [`SharedPointer`](crate::shared_pointer::SharedPointer).
94//!
95//! ## Serialization
96//!
97//! We support serialization through [serde](https://crates.io/crates/serde). To use it
98//! enable the `serde` feature. To do so change the archery dependency in your `Cargo.toml` to
99//!
100//! ```toml
101//! [dependencies]
102//! archery = { version = "<version>", features = ["serde"] }
103//! ```
104//! # Limitations
105//!
106//! Currently it is not possible to have unsized types inside a
107//! [`SharedPointer`](crate::shared_pointer::SharedPointer). As a workaround you can put the
108//! unsized type inside a [`Box`](::alloc::boxed::Box).
109//!
110//! # Alternative approaches
111//!
112//! An alternative to the approach taken by `archery` is to use traits with associated types to encode
113//! type-level functions. This has been suggested
114//! [multiple](https://github.com/orium/rpds/issues/7#issuecomment-362635901)
115//! [times](https://joshlf.com/post/2018/10/18/rust-higher-kinded-types-already/#comment-4160863400),
116//! but offers ugly ergonomics (see
117//! [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L153)
118//! and [here](https://github.com/Marwes/rpds/blob/e482d5abbaa6c876d7c624e497affe7299bbeece/src/sequence/vector/mod.rs#L249)).
119
120extern crate alloc;
121
122#[cfg(test)]
123#[macro_use]
124extern crate std;
125
126pub mod shared_pointer;
127
128pub use shared_pointer::SharedPointer;
129
130pub use shared_pointer::kind::SharedPointerKind;
131
132#[doc(no_inline)]
133pub use shared_pointer::kind::ArcK;
134#[cfg(feature = "triomphe")]
135#[doc(no_inline)]
136pub use shared_pointer::kind::ArcTK;
137#[doc(no_inline)]
138pub use shared_pointer::kind::RcK;