Crates.io | fplist |
lib.rs | fplist |
version | 0.2.1 |
source | src |
created_at | 2019-04-28 20:06:01.307923 |
updated_at | 2021-07-27 04:48:25.467663 |
description | An immutable, persistent, singly-linked list |
homepage | |
repository | https://github.com/acdenisSK/fplist |
max_upload_size | |
id | 130772 |
size | 23,041 |
A persistent, immutable, and singly-linked list in Rust.
Persistency is a property of data structures when modifying them. Persistency stipulates that old versions of the structures are always preserved when adding or removing elements. This crate provides such a structure in the form of a linked list.
To be efficient as possible when maintaining persistency, the linked list uses
reference counting. This permits sharing memory between lists whose
elements are the same. By default, the list uses the Rc
type for this
purpose. However, one major downside of Rc
is that lists cannot be sent
across threads. For this, you might want to enable the multithreaded
feature,
which will signal the list to use the multithreaded sibling Arc
.
One innate requirement of persistency is that structures must be immutable. That
is, they cannot permit mutation of their elements. The list type only provides
an API for immutable and owned access to its elements. You may circumvent this
with the RefCell
or Mutex
types, which grant interior mutability.
The list type is inspired by cons lists from Lisp languages, which is also how the list type is constructed with and represented.
The list is able to be serialized or deserialized by the serde framework.
Support, however, is disabled by default (to avoid needless dependencies). Enable
support with the serde_impls
feature.
use fplist::{PersistentList, cons};
let list = cons(1, cons(2, cons(3, PersistentList::new())));
assert_eq!(list.first(), Some(&1));
let list = list.rest();
assert_eq!(list.first(), Some(&2));
let list = list.rest();
assert_eq!(list.first(), Some(&3));
let list = list.rest();
assert_eq!(list.first(), None);
This project is under the jurisdiction of the MIT License.