[−][src]Struct transit_model::collection::CollectionWithId
A Collection
with identifier support.
Methods
impl<T: Id<T>> CollectionWithId<T>
[src]
pub fn new(v: Vec<T>) -> Result<Self>
[src]
Creates a CollectionWithId
from a Vec
. Fails if there is
duplicates in identifiers.
Examples
#[derive(PartialEq, Debug)] struct Obj(&'static str); impl Id<Obj> for Obj { fn id(&self) -> &str { self.0 } fn set_id(&mut self, id: String) { unimplemented!(); } } let c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; assert_eq!(c.len(), 2); assert_eq!(c.get("foo"), Some(&Obj("foo"))); assert!(CollectionWithId::new(vec![Obj("foo"), Obj("foo")]).is_err());
pub fn get_id_to_idx(&self) -> &HashMap<String, Idx<T>>
[src]
Get a reference to the String
to Idx<T>
internal mapping.
Examples
#[derive(PartialEq, Debug)] struct Obj(&'static str); impl Id<Obj> for Obj { fn id(&self) -> &str { self.0 } fn set_id(&mut self, id: String) { unimplemented!(); } } let c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; assert_eq!(c.len(), 2); assert_eq!(c.get_id_to_idx().len(), 2);
pub fn index_mut(&mut self, idx: Idx<T>) -> RefMut<T>
[src]
Access to a mutable reference of the corresponding object.
The drop
of the proxy object panic if the identifier is
modified to an indentifier already on the collection.
Examples
let mut c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let idx = c.get_idx("foo").unwrap(); c.index_mut(idx).0 = "baz"; assert!(c.get("foo").is_none()); assert_eq!(c.get("baz"), Some(&Obj("baz")));
let mut c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let idx = c.get_idx("foo").unwrap(); c.index_mut(idx).0 = "bar"; // panic
pub fn get_mut(&mut self, id: &str) -> Option<RefMut<T>>
[src]
Returns an option of a mutable reference of the corresponding object.
Examples
let mut c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; c.get_mut("foo").unwrap().0 = "baz"; assert!(c.get("foo").is_none()); assert_eq!(c.get("baz"), Some(&Obj("baz")));
pub fn push(&mut self, item: T) -> Result<Idx<T>>
[src]
Push an element in the CollectionWithId
. Fails if the
identifier of the new object is already in the collection.
Examples
let mut c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let baz_idx = c.push(Obj("baz"))?; assert_eq!(&c[baz_idx], &Obj("baz")); assert!(c.push(Obj("baz")).is_err()); let foobar_idx = c.push(Obj("foobar"))?; assert_eq!(&c[baz_idx], &Obj("baz")); assert_eq!(&c[foobar_idx], &Obj("foobar"));
pub fn retain<F: FnMut(&T) -> bool>(&mut self, f: F)
[src]
Retains the elements matching predicate parameter from the current CollectionWithId
object
Examples
let mut c = CollectionWithId::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.id())); assert_eq!(c.len(), 2); assert_eq!(c.get("foo"), Some(&Obj("foo"))); assert_eq!(c.get("qux"), Some(&Obj("qux")));
pub fn try_merge(&mut self, other: Self) -> Result<()>
[src]
Merge a CollectionWithId
parameter into the current one. Fails if any identifier into the
CollectionWithId
parameter is already in the collection.
Examples
let mut c1 = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let mut c2 = CollectionWithId::new(vec![Obj("foo"), Obj("qux")])?; let mut c3 = CollectionWithId::new(vec![Obj("corge"), Obj("grault")])?; assert!(c1.try_merge(c2).is_err()); c1.try_merge(c3); assert_eq!(c1.len(), 4);
pub fn merge(&mut self, other: Self)
[src]
Merge a CollectionWithId
parameter into the current one. If any identifier into the
CollectionWithId
parameter is already in the collection, CollectionWithId
is not added.
Examples
let mut c1 = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let mut c2 = CollectionWithId::new(vec![Obj("foo"), Obj("qux")])?; c1.merge(c2); assert_eq!(c1.len(), 3);
pub fn is_empty(&self) -> bool
[src]
Examples
let mut c: CollectionWithId<Obj> = CollectionWithId::new(vec![])?; assert!(c.is_empty());
impl<T: Id<T> + WithId> CollectionWithId<T>
[src]
pub fn get_or_create<'a>(&'a mut self, id: &str) -> RefMut<'a, T>
[src]
Get a mutable reference of the corresponding object or create it
Examples
let mut c = CollectionWithId::new(vec![Obj("1".into())])?; let obj = c.get_or_create("2"); assert_eq!(obj.0, "2");
impl<T: Id<T>> CollectionWithId<T>
[src]
pub fn get_or_create_with<'a, F>(&'a mut self, id: &str, f: F) -> RefMut<'a, T> where
F: FnMut() -> T,
[src]
F: FnMut() -> T,
Get a mutable reference of the corresponding object or create it and apply a function on it.
Examples
let mut c = CollectionWithId::new(vec![Obj("1".into(), "foo".into())])?; let obj = c.get_or_create_with("2", || Obj("bob".into(), "bar".into())); assert_eq!(obj.0, "2"); assert_eq!(obj.1, "bar");
impl<T> CollectionWithId<T>
[src]
pub fn get_idx(&self, id: &str) -> Option<Idx<T>>
[src]
Returns the index corresponding to the identifier.
Examples
let c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let idx = c.get_idx("foo").unwrap(); assert_eq!(&c[idx], &Obj("foo")); assert!(c.get_idx("baz").is_none());
pub fn get(&self, id: &str) -> Option<&T>
[src]
Returns a reference to the object corresponding to the identifier.
Examples
let c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; assert_eq!(c.get("foo"), Some(&Obj("foo"))); assert!(c.get("baz").is_none());
pub fn into_vec(self) -> Vec<T>
[src]
Converts self
into a vector without clones or allocation.
Examples
let c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let v = c.into_vec(); assert_eq!(v, &[Obj("foo"), Obj("bar")]);
pub fn take(&mut self) -> Vec<T>
[src]
Takes the corresponding vector without clones or allocation,
leaving self
empty.
Examples
let mut c = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let v = c.take(); assert_eq!(v, &[Obj("foo"), Obj("bar")]); assert_eq!(c.len(), 0);
Methods from Deref<Target = Collection<T>>
pub fn len(&self) -> usize
[src]
Returns the number of elements in the collection, also referred to as its 'length'.
Examples
let c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]); assert_eq!(c.len(), 6);
pub fn iter(&self) -> Iter<T>
[src]
Iterates over the (Idx<T>, &T)
of the Collection
.
Examples
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!(v, &5); assert_eq!(&c[k], &5);
pub fn values(&self) -> Iter<T>
[src]
Iterates over the &T
of the Collection
.
Examples
let c: Collection<i32> = Collection::new(vec![1, 1, 2, 3, 5, 8]); let values: Vec<&i32> = c.values().collect(); assert_eq!(values, &[&1, &1, &2, &3, &5, &8]);
pub fn iter_from<I>(&self, indexes: I) -> impl Iterator<Item = &T> where
I: IntoIterator,
I::Item: Borrow<Idx<T>>,
[src]
I: IntoIterator,
I::Item: Borrow<Idx<T>>,
Iterates on the objects corresponding to the given indices.
Examples
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!(transit_refs, &[&"bus", &"metro", &"train"]);
pub fn is_empty(&self) -> bool
[src]
Examples
let mut c: Collection<Obj> = Collection::new(vec![]); assert!(c.is_empty());
Trait Implementations
impl<T: PartialEq> PartialEq<CollectionWithId<T>> for CollectionWithId<T>
[src]
fn eq(&self, other: &CollectionWithId<T>) -> bool
[src]
#[must_use]
default fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<T: Clone> Clone for CollectionWithId<T>
[src]
fn clone(&self) -> CollectionWithId<T>
[src]
default fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<T> Default for CollectionWithId<T>
[src]
impl<'a, T> IntoIterator for &'a CollectionWithId<T>
[src]
type Item = (Idx<T>, &'a T)
The type of the elements being iterated over.
type IntoIter = Iter<'a, T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<T> IntoIterator for CollectionWithId<T>
[src]
type Item = T
The type of the elements being iterated over.
type IntoIter = IntoIter<T>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
impl<T: Id<T>> Extend<T> for CollectionWithId<T>
[src]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
[src]
Extend a CollectionWithId
with the content of an iterator of
CollectionWithId without duplicated ids.
Examples
let mut c1 = CollectionWithId::new(vec![Obj("foo"), Obj("bar")])?; let mut c2 = CollectionWithId::new(vec![Obj("foo"), Obj("qux")])?; c1.extend(c2); assert_eq!(c1.len(), 3);
impl<T: Debug> Debug for CollectionWithId<T>
[src]
impl<T> Deref for CollectionWithId<T>
[src]
type Target = Collection<T>
The resulting type after dereferencing.
fn deref(&self) -> &Collection<T>
[src]
impl<'de, T> Deserialize<'de> for CollectionWithId<T> where
T: Deserialize<'de> + Id<T>,
[src]
T: Deserialize<'de> + Id<T>,
fn deserialize<D>(deserializer: D) -> StdResult<Self, D::Error> where
D: Deserializer<'de>,
[src]
D: Deserializer<'de>,
impl<T> Serialize for CollectionWithId<T> where
T: Serialize + Id<T>,
[src]
T: Serialize + Id<T>,
Auto Trait Implementations
impl<T> Send for CollectionWithId<T> where
T: Send,
T: Send,
impl<T> Sync for CollectionWithId<T> where
T: Sync,
T: Sync,
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
impl<T> From for T
[src]
impl<I> IntoIterator for I where
I: Iterator,
[src]
I: Iterator,
type Item = <I as Iterator>::Item
The type of the elements being iterated over.
type IntoIter = I
Which kind of iterator are we turning this into?
fn into_iter(self) -> I
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> DeserializeOwned for T where
T: Deserialize<'de>,
[src]
T: Deserialize<'de>,