Skip to main content

Crate djs_collection

Crate djs_collection 

Source
Expand description

§djs-collection

A feature-rich map type modelled on discord.js Collection.

Status: alpha. Collection with discord.js’ helpers. The API may still change between alpha releases; see the changelog.

discord.js counterpart: @discordjs/collection

§The collection

Collection<K, V> is a hash map that remembers insertion order, like the JS Map discord.js’ Collection extends: iteration, first, last, at and random follow the order keys were first inserted in, and remove keeps it. It is built on indexmap and hashes with foldhash by default.

use djs_collection::Collection;

let mut members: Collection<u64, &str> = Collection::new();
members.insert(1, "ada");
members.insert(2, "grace");
members.insert(3, "linus");

assert_eq!(members.first(), Some(&"ada"));
assert_eq!(members.at(-1), Some(&"linus"));
assert_eq!(members.find_key(|name, _| name.starts_with('g')), Some(&2));

// Transformations consume the collection and clone nothing; clone it to keep it.
let (short, long) = members.clone().partition(|name, _| name.len() <= 3);
assert_eq!(short.len(), 1);
assert_eq!(long.keys().copied().collect::<Vec<_>>(), [2, 3]);

// In-place changes.
assert_eq!(members.sweep(|name, _| name.contains('i')), 1);
let winner = members.random();
assert!(winner.is_some());

§Methods

KindMethodsTakes
Mapget, get_mut, insert, remove (keeps order), swap_remove, contains_key, len, iter, keys, values, …
Positionfirst, first_key, last, last_key, at, key_at (negative from the end), index_of&self
Searchfind, find_key, find_last, find_last_key, has_all, has_any, map → Vec&self
Randomrandom, random_key, random_n&self
In placeensure, sweep, retain, sort, sort_by, reverse&mut self
New collectionfilter, partition, map_values, flat_map, to_sorted, to_sorted_by, to_reversed, concat, union, intersection, difference, symmetric_difference, merge, Collection::combine_entriesself

discord.js’ each, some, every, reduce and tap are the iterators’ job (.values().any(…), .iter().fold(…)). == compares keys and values in any order, like discord.js’ equals. as_index_map and into_index_map reach the inner IndexMap for anything else.

§Part of djs-rs

djs-collection is one crate of djs-rs, a high-performance Rust library for building Discord bots modelled on discord.js v14. It can be used on its own or through the main djs-rs crate, as djs_rs::collection.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Modules§

iter
The iterators of a Collection, in insertion order (D-217).

Structs§

Collection
A map that remembers the order its keys were first inserted in, with the helpers of discord.js’ Collection.

Type Aliases§

DefaultHashBuilder
The default hasher of a Collection: foldhash’s fast hasher with a random seed (D-216), as in djs-cache.