Struct typed_index_collection::Collection
source · pub struct Collection<T> { /* private fields */ }
Expand description
The Collection
object looks like a Map<Idx<T>, T>
, with opaque
keys. Then, you can easily store indices and don’t mess up
between different types of indices.
Implementations§
source§impl<T> Collection<T>
impl<T> Collection<T>
sourcepub fn new(v: Vec<T>) -> Self
pub fn new(v: Vec<T>) -> Self
Creates the Collection
from a Vec
.
Examples
use typed_index_collection::Collection;
let _: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of elements in the collection, also referred to as its ‘length’.
Examples
use typed_index_collection::Collection;
let c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]);
assert_eq!(6, c.len());
sourcepub fn iter(&self) -> Iter<'_, T>
pub fn iter(&self) -> Iter<'_, T>
Iterates over the (Idx<T>, &T)
of the Collection
.
Examples
use typed_index_collection::{Collection, Idx};
let c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]);
let (k, v): (Idx<i32>, &i32) = c.iter().nth(4).unwrap();
assert_eq!(&5, v);
assert_eq!(&5, &c[k]);
sourcepub fn values(&self) -> Iter<'_, T>
pub fn values(&self) -> Iter<'_, T>
Iterates over the &T
of the Collection
.
Examples
use typed_index_collection::Collection;
let c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]);
let values: Vec<&i32> = c.values().collect();
assert_eq!(vec![&1, &1, &2, &3, &5, &8], values);
sourcepub fn values_mut(&mut self) -> IterMut<'_, T>
pub fn values_mut(&mut self) -> IterMut<'_, T>
Iterates over the &mut T
of the Collection
.
Examples
use typed_index_collection::Collection;
let mut c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]);
for elem in c.values_mut() {
*elem *= 2;
}
assert_eq!(Collection::new(vec![2, 2, 4, 6, 10, 16]), c);
sourcepub fn iter_from<I>(&self, indexes: I) -> impl Iterator<Item = &T>where
I: IntoIterator,
I::Item: Borrow<Idx<T>>,
pub fn iter_from<I>(&self, indexes: I) -> impl Iterator<Item = &T>where I: IntoIterator, I::Item: Borrow<Idx<T>>,
Iterates on the objects corresponding to the given indices.
Examples
use typed_index_collection::{Collection, Idx};
use std::collections::BTreeSet;
let c = Collection::new(vec!["bike", "bus", "walking", "car", "metro", "train"]);
let transit_indices: BTreeSet<Idx<&str>> = get_transit_indices(&c);
let transit_refs: Vec<&&str> = c.iter_from(&transit_indices).collect();
assert_eq!(vec![&"bus", &"metro", &"train"], transit_refs);
sourcepub fn push(&mut self, item: T) -> Idx<T>
pub fn push(&mut self, item: T) -> Idx<T>
Push an element in the Collection
without control.
Examples
use typed_index_collection::{Collection, Id};
#[derive(PartialEq, Debug)]
struct Obj(&'static str);
let mut c = Collection::default();
let foo_idx = c.push(Obj("foo"));
let bar_idx = c.push(Obj("bar"));
assert_eq!(&Obj("foo"), &c[foo_idx]);
assert_ne!(&Obj("bar"), &c[foo_idx]);
sourcepub fn merge(&mut self, other: Self)
pub fn merge(&mut self, other: Self)
Merge a Collection
parameter into the current one.
Examples
use typed_index_collection::Collection;
#[derive(PartialEq, Debug)]
struct Obj(&'static str);
let mut c1 = Collection::from(Obj("foo"));
let c2 = Collection::from(Obj("bar"));
c1.merge(c2);
assert_eq!(2, c1.len());
sourcepub fn take(&mut self) -> Vec<T>
pub fn take(&mut self) -> Vec<T>
Takes the corresponding vector without clones or allocation,
leaving self
empty.
Examples
use typed_index_collection::Collection;
#[derive(PartialEq, Debug)]
struct Obj(&'static str);
let mut c = Collection::new(vec![Obj("foo"), Obj("bar")]);
let v = c.take();
assert_eq!(vec![Obj("foo"), Obj("bar")], v);
assert_eq!(0, c.len());
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Examples
use typed_index_collection::Collection;
#[derive(PartialEq, Debug)]
struct Obj;
let mut c: Collection<Obj> = Collection::default();
assert!(c.is_empty());
sourcepub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)
Retains the elements matching predicate parameter from the current CollectionWithId
object
Examples
use typed_index_collection::Collection;
use std::collections::HashSet;
#[derive(PartialEq, Debug)]
struct Obj(&'static str);
let mut c = Collection::new(vec![Obj("foo"), Obj("bar"), Obj("qux")]);
let mut ids_to_keep: HashSet<String> = HashSet::new();
ids_to_keep.insert("foo".to_string());
ids_to_keep.insert("qux".to_string());
c.retain(|item| ids_to_keep.contains(item.0));
assert_eq!(2, c.len());
assert_eq!(vec!["foo", "qux"], c.values().map(|obj| obj.0).collect::<Vec<&str>>());
Trait Implementations§
source§impl<T: Clone> Clone for Collection<T>
impl<T: Clone> Clone for Collection<T>
source§fn clone(&self) -> Collection<T>
fn clone(&self) -> Collection<T>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<T: Debug> Debug for Collection<T>
impl<T: Debug> Debug for Collection<T>
source§impl<T> Default for Collection<T>
impl<T> Default for Collection<T>
source§impl<'de, T> Deserialize<'de> for Collection<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for Collection<T>where T: Deserialize<'de>,
source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where D: Deserializer<'de>,
source§impl<T> From<T> for Collection<T>
impl<T> From<T> for Collection<T>
Creates a Collection
from one element.
Examples
use typed_index_collection::Collection;
let collection: Collection<i32> = Collection::from(42);
assert_eq!(1, collection.len());
let integer = collection.into_iter().next().unwrap();
assert_eq!(42, integer);
source§impl<T> Index<Idx<T>> for Collection<T>
impl<T> Index<Idx<T>> for Collection<T>
source§impl<T> IndexMut<Idx<T>> for Collection<T>
impl<T> IndexMut<Idx<T>> for Collection<T>
source§fn index_mut(&mut self, idx: Idx<T>) -> &mut T
fn index_mut(&mut self, idx: Idx<T>) -> &mut T
Access a mutable reference on an entry of the Collection
from its
Idx
.
use typed_index_collection::Collection;
let mut c = Collection::new(vec![-2, -1, 0, 1, 2]);
let negatives_idxs = c
.iter()
.filter(|(_, &v)| v < 0)
.map(|(idx, _)| idx)
.collect::<Vec<_>>();
for idx in negatives_idxs {
*c.index_mut(idx) = 0;
}
assert_eq!(vec![0, 0, 0, 1, 2], c.take());
source§impl<'a, T> IntoIterator for &'a Collection<T>
impl<'a, T> IntoIterator for &'a Collection<T>
source§impl<T> IntoIterator for Collection<T>
impl<T> IntoIterator for Collection<T>
source§impl<T: PartialEq> PartialEq<Collection<T>> for Collection<T>
impl<T: PartialEq> PartialEq<Collection<T>> for Collection<T>
source§fn eq(&self, other: &Collection<T>) -> bool
fn eq(&self, other: &Collection<T>) -> bool
self
and other
values to be equal, and is used
by ==
.