A doubly-linked list for Rust backed by generational-arena.

Overview

generational_token_list

crates.io Rust MIT licensed

A doubly-linked list backed by generational-arena. Inspired by indexlist.

Instead of returning pointers or numerical indices to items this data structure returns opaque ItemTokens.

fn main() {
    let mut list = GenerationalTokenList::<i32>::new();
    let item1 = list.push_back(10);
    let item2 = list.push_back(20);
    let item3 = list.push_back(30);

    let data = list.into_iter().collect::<Vec<_>>();
    assert_eq!(data, vec![10, 20, 30]);
}

Tokens remain valid regardless of other items being inserted or removed. Removing an item invalidates its token. Clearing the list invalidates all tokens.

More details and examples are available in the documentation for the methods.

Useful features

There are a couple features that I think make this crate stand out compared to other similar crates. Some crates implement a few of these features but I haven't found one that implements all of them (or at the very least, both 1 and 2).

  1. Insertion of items relative to other items
fn main() {
    let mut list = GenerationalTokenList::<i32>::new();
    let item1 = list.push_back(10);
    list.push_back(20);
    list.insert_after(item1, 300);
    // list: [10, 300, 20]
}
  1. All push/insert methods have a variant that takes a FnOnce allowing creation of items that know their own token
fn main() {
    struct Meta {
        data: u8,
        my_token: ItemToken,
    }

    let mut list = GenerationalTokenList::new();
    let item1 = list.push_back_with(|token| Meta { data: 1, my_token: token });
    let item2 = list.push_back_with(|token| Meta { data: 2, my_token: token });

    let item1_data = list.head().unwrap();
    assert_eq!(item1, item1_data.my_token);
}
  1. Passthrough of get2_mut method from generational-arena.
  2. Implements Iter and IterMut1 traits.

1 requires enabling iter-mut feature

Cargo features

  • iter-mut: enables the iter_mut method. See "Safety" section for more details.

Safety

By default, this crate is forbid(unsafe_code).

If you need GenerationalTokenList::iter_mut then you must enable the iter-mut feature. Doing so makes the crate deny(unsafe_code), and the unsafe block inside iter_mut is excluded via allow(unsafe_code).

Similar crates

TODO

Pull requests are welcome :)

  • Implement Index and IndexMut traits
  • Implement Drain
  • Implement try_push_* and try_insert_* methods
  • Implement flavors of push_*_with and insert_*_with that allow fallible insertion of items? E.g.:
pub fn push_back_fallible(&mut self, create: impl FnOnce(ItemToken) -> Result<T>) -> Result<ItemToken> {
    //...
}
  • Add no-std support?'
  • Consider adding #[inline] to some methods?

Disclaimer

This is not an official Agilent product. No support is implied.

You might also like...
Generic array types in Rust

generic-array This crate implements generic array types for Rust. Requires minumum Rust version of 1.36.0, or 1.41.0 for From[T; N] implementations

A priority queue for Rust with efficient change function.

PriorityQueue This crate implements a Priority Queue with a function to change the priority of an object. Priority and items are stored in an IndexMap

K-dimensional tree in Rust for fast geospatial indexing and lookup

kdtree K-dimensional tree in Rust for fast geospatial indexing and nearest neighbors lookup Crate Documentation Usage Benchmark License Usage Add kdtr

Roaring bitmap implementation for Rust

RoaringBitmap This is not yet production ready. The API should be mostly complete now. This is a Rust port of the Roaring bitmap data structure, initi

Rust Persistent Data Structures

Rust Persistent Data Structures Rust Persistent Data Structures provides fully persistent data structures with structural sharing. Setup To use rpds a

Rust crate to extend io::Read & io::Write types with progress callbacks

progress-streams Rust crate to provide progress callbacks for types which implement io::Read or io::Write. Examples Reader extern crate progress_strea

Parameterized routing for generic resources in Rust

Usher Usher provides an easy way to construct parameterized routing trees in Rust. The nodes of these trees is naturally generic, allowing Usher to le

RiteLinked - LinkedHashMap & LinkedHashSet in Rust

RiteLinked -- HashMap-like containers that hold their key-value pairs in a user controllable order RiteLinked provides more up to date versions of Lin

A proof of concept implementation of cyclic data structures in stable, safe, Rust.

A proof of concept implementation of cyclic data structures in stable, safe, Rust. This demonstrates the combined power of the static-rc crate and the

Owner
null
Rust Vec Doubly Linked List

Rust Vec Doubly Linked List Just like doubly linked list(e.g. std::LinkedList), but supports that returning a index of the vec when push. And you can

~ 2 May 1, 2022
A Rust doubly-linked intrusive list with Miri tests

Intrusive stack-allocated doubly-linked list example Quickstart cargo test Or to run the tests under Miri: rustup toolchain install nightly # if y

Cliff L. Biffle 9 Dec 4, 2022
wait-free spsc linked-list queue with individually reusable nodes

A wait-free single-producer single-consumer linked-list queue with individually reusable nodes.

glowcoil 22 Dec 26, 2022
Cyclic Double-Linked List

Double-Linked List This crate provides a doubly-linked list with owned nodes, implemented as a cyclic list. Usage First, add dependency in your Cargo.

Frank King 10 Nov 26, 2021
Display linked packages for compiled rust binaries

cargo-linked Display the packages a rust binary is linked against. As cargo subcommand! Easy said: run cargo linked to find out which packages you mus

Jojii 1 Jun 7, 2021
A number of collections, such as linked-lists, binary-trees, or B-Trees are most easily implemented with aliasing pointers.

StaticRc is a safe reference-counted pointer, similar to Rc or Arc, though performing its reference-counting at compile-time rather than run-time, and

null 372 Dec 19, 2022
disk backed wal queue

Repository Template  Queue like disk backed WAL Pronouced Quál - from the german wordrd for agony - because it is. Operations The basic concept is si

The Tremor Project 8 Jun 4, 2022
Rust-algorithm-club - Learn algorithms and data structures with Rust

Rust Algorithm Club ?? ?? This repo is under construction. Most materials are written in Chinese. Check it out here if you are able to read Chinese. W

Weihang Lo 360 Dec 28, 2022
Ternary search tree collection in rust

tst Ternary search tree collection in rust with similar API to std::collections as it possible. Ternary search tree is a type of trie (sometimes calle

Alexey Pervushin 20 Dec 7, 2022
Array helpers for Rust's Vector and String types

array_tool Array helpers for Rust. Some of the most common methods you would use on Arrays made available on Vectors. Polymorphic implementations for

Daniel P. Clark 69 Dec 9, 2022