You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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::*;structKeyValuePair<K,V,P:SharedPointerKind>{pubkey:SharedPointer<K,P>,pubvalue:SharedPointer<V,P>,}impl<K,V,P:SharedPointerKind>KeyValuePair<K,V,P>{fnnew(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);
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
multipletimes,
but offers ugly ergonomics (see
here
and here).
About
Abstract over the atomicity of reference-counting pointers in rust