Skip to main content

Collected

Struct Collected 

Source
pub struct Collected<K, V> { /* private fields */ }
Expand description

What the collecting shortcuts return (D-268): the Collection of what was collected (through Deref) and why the collector ended. An empty result is not an error.

discord.js counterpart: the Collection of awaitMessages / awaitReactions.

Implementations§

Source§

impl<K, V> Collected<K, V>

Source

pub const fn reason(&self) -> EndReason

Why the collector ended.

discord.js counterpart: the reason of a collector’s end event.

Source

pub fn into_collection(self) -> Collection<K, V>

Returns the collection.

discord.js counterpart: the Collection awaitMessages and awaitReactions resolve to.

Methods from Deref<Target = Collection<K, V>>§

Source

pub fn len(&self) -> usize

The number of entries.

discord.js counterpart: size.

Source

pub fn is_empty(&self) -> bool

Whether there are no entries.

discord.js counterpart: collection.size === 0.

Source

pub fn capacity(&self) -> usize

How many entries fit before the collection reallocates.

discord.js counterpart: none (JavaScript maps have no capacity).

Source

pub fn clear(&mut self)

Removes every entry, keeping the memory for reuse.

discord.js counterpart: clear.

Source

pub fn hasher(&self) -> &S

The hasher keys are hashed with.

discord.js counterpart: none (JavaScript maps have no hasher).

Source

pub fn iter(&self) -> Iter<'_, K, V> ⓘ

The entries in order, as (&key, &value).

discord.js counterpart: entries.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V> ⓘ

The entries in order, with mutable values.

discord.js counterpart: none (JavaScript values are mutated in place).

Source

pub fn keys(&self) -> Keys<'_, K, V> ⓘ

The keys in order.

discord.js counterpart: keys.

Source

pub fn values(&self) -> Values<'_, K, V> ⓘ

The values in order.

discord.js counterpart: values.

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V> ⓘ

The values in order, mutable.

discord.js counterpart: none (JavaScript values are mutated in place).

Source

pub fn as_index_map(&self) -> &IndexMap<K, V, S>

The inner IndexMap, for operations this type does not offer (D-217).

discord.js counterpart: none (the Map a Collection extends).

Source

pub fn as_index_map_mut(&mut self) -> &mut IndexMap<K, V, S>

The inner IndexMap, mutable (D-217).

discord.js counterpart: none (the Map a Collection extends).

Source

pub fn first(&self) -> Option<&V>

The first value inserted, or None if the collection is empty. The first n are .values().take(n).

discord.js counterpart: first().

Source

pub fn first_key(&self) -> Option<&K>

The first key inserted.

discord.js counterpart: firstKey().

Source

pub fn last(&self) -> Option<&V>

The last value inserted. The last n are .values().rev().take(n).

discord.js counterpart: last().

Source

pub fn last_key(&self) -> Option<&K>

The last key inserted.

discord.js counterpart: lastKey().

Source

pub fn at(&self, index: isize) -> Option<&V>

The value at position index in insertion order; a negative index counts from the end (-1 is the last).

discord.js counterpart: at.

§Examples
use djs_collection::Collection;

let letters: Collection<u8, char> = [(1, 'a'), (2, 'b'), (3, 'c')].into();
assert_eq!(letters.at(0), Some(&'a'));
assert_eq!(letters.at(-1), Some(&'c'));
assert_eq!(letters.at(3), None);
assert_eq!(letters.key_at(-3), Some(&1));
Source

pub fn key_at(&self, index: isize) -> Option<&K>

The key at position index; a negative index counts from the end.

discord.js counterpart: keyAt.

Source

pub fn random(&self) -> Option<&V>

A random value, or None if the collection is empty.

The randomness comes from std’s RandomState (D-210): fine for games and giveaways, not for anything that must resist prediction.

discord.js counterpart: random().

Source

pub fn random_key(&self) -> Option<&K>

A random key.

discord.js counterpart: randomKey().

Source

pub fn random_n(&self, count: usize) -> Vec<&V>

Up to count different random values, in random order — all of them, shuffled, if count is at least the length.

discord.js counterpart: random(amount).

§Examples
use djs_collection::Collection;

let entrants: Collection<u64, &str> = [(1, "ada"), (2, "grace"), (3, "linus")].into();
let winners = entrants.random_n(2);
assert_eq!(winners.len(), 2);
assert_ne!(winners[0], winners[1]);
Source

pub fn find(&self, predicate: impl FnMut(&V, &K) -> bool) -> Option<&V>

The first value for which predicate(value, key) is true.

discord.js counterpart: find.

Source

pub fn find_key(&self, predicate: impl FnMut(&V, &K) -> bool) -> Option<&K>

The key of the first value for which predicate(value, key) is true.

discord.js counterpart: findKey.

Source

pub fn find_last(&self, predicate: impl FnMut(&V, &K) -> bool) -> Option<&V>

The last value for which predicate(value, key) is true.

discord.js counterpart: findLast.

Source

pub fn find_last_key(&self, predicate: impl FnMut(&V, &K) -> bool) -> Option<&K>

The key of the last value for which predicate(value, key) is true.

discord.js counterpart: findLastKey.

Source

pub fn map<T>(&self, f: impl FnMut(&V, &K) -> T) -> Vec<T>

f(value, key) of every entry, in order.

discord.js counterpart: map.

§Examples
use djs_collection::Collection;

let users: Collection<u64, &str> = [(1, "ada"), (2, "grace")].into();
assert_eq!(users.map(|name, id| format!("{id}:{name}")), ["1:ada", "2:grace"]);
Source

pub fn reverse(&mut self)

Reverses the order of the entries.

discord.js counterpart: reverse.

Source

pub fn sort_by(&mut self, compare: impl FnMut(&V, &V, &K, &K) -> Ordering)

Sorts the entries with compare(value_a, value_b, key_a, key_b), keeping the order of equal ones.

discord.js counterpart: sort(compareFunction).

Source

pub fn sort(&mut self)
where V: Ord,

Sorts the entries by value, keeping the order of equal ones.

discord.js counterpart: sort() (which compares values as strings).

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

The value of key.

discord.js counterpart: get.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

The value of key, mutable.

discord.js counterpart: none (JavaScript values are mutated in place).

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

The stored key equal to key, with its value.

discord.js counterpart: none (Map#get returns the value only).

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Whether key has a value.

discord.js counterpart: has.

Source

pub fn index_of<Q>(&self, key: &Q) -> Option<usize>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

The position of key in insertion order.

discord.js counterpart: none (the inverse of Collection#keyAt).

Source

pub fn insert(&mut self, key: K, value: V) -> Option<V>

Sets the value of key, returning the previous one. A new key goes last; an existing key keeps its position.

discord.js counterpart: set.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes key, returning its value; the entries after it move up, so the order is kept (O(n), D-215). swap_remove is O(1).

To remove many entries, use one sweep or retain pass, which is linear however many it removes: removing 1 000 entries near the front of a million one by one moves the rest 1 000 times (seconds instead of milliseconds).

discord.js counterpart: delete.

Source

pub fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes key, returning the stored key and its value, keeping the order.

discord.js counterpart: Map#delete.

Source

pub fn swap_remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: Hash + Eq + ?Sized,

Removes key in O(1) by moving the last entry into its place, so the order changes.

discord.js counterpart: none (Map#delete keeps the order).

Source

pub fn ensure(&mut self, key: K, default: impl FnOnce(&K) -> V) -> &mut V

The value of key, inserting default(&key) first if there is none.

discord.js counterpart: ensure.

§Examples
use djs_collection::Collection;

let mut warnings: Collection<u64, u32> = Collection::new();
*warnings.ensure(1, |_| 0) += 1;
*warnings.ensure(1, |_| 0) += 1;
assert_eq!(warnings.get(&1), Some(&2));
Source

pub fn has_all<'q, Q>(&self, keys: impl IntoIterator<Item = &'q Q>) -> bool
where K: Borrow<Q>, Q: Hash + Eq + 'q + ?Sized,

Whether every key of keys has a value.

discord.js counterpart: hasAll.

Source

pub fn has_any<'q, Q>(&self, keys: impl IntoIterator<Item = &'q Q>) -> bool
where K: Borrow<Q>, Q: Hash + Eq + 'q + ?Sized,

Whether any key of keys has a value.

discord.js counterpart: hasAny.

Source

pub fn retain(&mut self, keep: impl FnMut(&K, &mut V) -> bool)

Keeps only the entries for which keep(&key, &mut value) is true, in one pass that keeps the order.

discord.js counterpart: Collection#sweep, with the condition inverted.

Source

pub fn sweep(&mut self, remove: impl FnMut(&V, &K) -> bool) -> usize

Removes the entries for which remove(value, key) is true, in one pass that keeps the order, and returns how many were removed.

discord.js counterpart: sweep.

§Examples
use djs_collection::Collection;

let mut ages: Collection<&str, u32> = [("a", 3), ("b", 40), ("c", 7)].into();
assert_eq!(ages.sweep(|&age, _| age < 10), 2);
assert_eq!(ages.len(), 1);

Trait Implementations§

Source§

impl<K: Clone, V: Clone> Clone for Collected<K, V>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K: Debug, V: Debug> Debug for Collected<K, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V> Deref for Collected<K, V>

Source§

type Target = Collection<K, V>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Collection<K, V>

Dereferences the value.
Source§

impl<K, V> DerefMut for Collected<K, V>

Source§

fn deref_mut(&mut self) -> &mut Collection<K, V>

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<K, V> Freeze for Collected<K, V>

§

impl<K, V> RefUnwindSafe for Collected<K, V>

§

impl<K, V> Send for Collected<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for Collected<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for Collected<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnsafeUnpin for Collected<K, V>

§

impl<K, V> UnwindSafe for Collected<K, V>
where K: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> Classify for T

§

type Classified = T

§

fn classify(self) -> T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> Declassify for T

§

type Declassified = T

§

fn declassify(self) -> T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more