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>

source

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]);
source

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());
source

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]);
source

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);
source

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);
source

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);
source

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]);
source

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());
source

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());
source

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());
source

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>

source§

fn clone(&self) -> Collection<T>

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl<T: Debug> Debug for Collection<T>

source§

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

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

impl<T> Default for Collection<T>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

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>,

Deserialize this value from the given Serde deserializer. Read more
source§

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§

fn from(object: T) -> Self

Converts to this type from the input type.
source§

impl<T> Index<Idx<T>> for Collection<T>

§

type Output = T

The returned type after indexing.
source§

fn index(&self, index: Idx<T>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
source§

impl<T> IndexMut<Idx<T>> for Collection<T>

source§

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>

§

type Item = (Idx<T>, &'a T)

The type of the elements being iterated over.
§

type IntoIter = Map<Enumerate<Iter<'a, T>>, fn(_: (usize, &T)) -> (Idx<T>, &T)>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Iter<'a, T>

Creates an iterator from a value. Read more
source§

impl<T> IntoIterator for Collection<T>

§

type Item = T

The type of the elements being iterated over.
§

type IntoIter = IntoIter<T, Global>

Which kind of iterator are we turning this into?
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl<T: PartialEq> PartialEq<Collection<T>> for Collection<T>

source§

fn eq(&self, other: &Collection<T>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T> Serialize for Collection<T>where T: Serialize,

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> RefUnwindSafe for Collection<T>where T: RefUnwindSafe,

§

impl<T> Send for Collection<T>where T: Send,

§

impl<T> Sync for Collection<T>where T: Sync,

§

impl<T> Unpin for Collection<T>where T: Unpin,

§

impl<T> UnwindSafe for Collection<T>where T: UnwindSafe,

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<!> for T

const: unstable · source§

fn from(t: !) -> T

Converts to this type from the input type.
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

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

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

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 Twhere U: From<T>,

const: unstable · 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> ToOwned for Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

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

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,