Skip to content

Commit

Permalink
Add FromIterator implementations for Collection and CollectionWithId
Browse files Browse the repository at this point in the history
  • Loading branch information
woshilapin committed Nov 14, 2019
1 parent 2c785d2 commit 313c92c
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions collection/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,41 @@ impl<T> IntoIterator for Collection<T> {
}
}

/// Collect from an iterator into a `Collection`
///
/// ```
/// # use transit_model_collection::{Collection, Id};
/// struct ObjectWithId(String);
/// impl Id<ObjectWithId> for ObjectWithId {
/// fn id(&self) -> &str { &self.0 }
/// fn set_id(&mut self, _id: String) { unimplemented!() }
/// }
///
/// let range = vec![42, 43, 42];
/// let collection: Collection<_> = range
/// .into_iter()
/// .map(|id| ObjectWithId(id.to_string()))
/// .collect();
/// assert_eq!(collection.len(), 3);
///
/// let mut values = collection.values();
/// assert_eq!("42", values.next().unwrap().0);
/// assert_eq!("43", values.next().unwrap().0);
/// assert_eq!("42", values.next().unwrap().0);
/// ```
impl<T> std::iter::FromIterator<T> for Collection<T> {
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
iter.into_iter()
.fold(Collection::default(), |mut accumulator, object| {
accumulator.push(object);
accumulator
})
}
}

impl<T> ops::Index<Idx<T>> for Collection<T> {
type Output = T;
fn index(&self, index: Idx<T>) -> &Self::Output {
Expand Down Expand Up @@ -1053,6 +1088,42 @@ impl<T> IntoIterator for CollectionWithId<T> {
}
}

/// Collect from an iterator into a `CollectionWithId`
///
/// ```
/// # use transit_model_collection::{CollectionWithId, Id};
/// struct ObjectWithId(String);
/// impl Id<ObjectWithId> for ObjectWithId {
/// fn id(&self) -> &str { &self.0 }
/// fn set_id(&mut self, _id: String) { unimplemented!() }
/// }
///
/// let range = vec![42, 43, 42];
/// let collection: CollectionWithId<_> = range
/// .into_iter()
/// .map(|id| ObjectWithId(id.to_string()))
/// .collect();
/// assert_eq!(collection.len(), 2);
/// assert!(collection.contains_id("42"));
/// assert!(collection.contains_id("43"));
/// ```
impl<T> std::iter::FromIterator<T> for CollectionWithId<T>
where
T: Id<T>,
{
#![allow(unused_must_use)]
fn from_iter<I>(iter: I) -> Self
where
I: IntoIterator<Item = T>,
{
iter.into_iter()
.fold(CollectionWithId::default(), |mut accumulator, object| {
accumulator.push(object);
accumulator
})
}
}

impl<T> ::serde::Serialize for CollectionWithId<T>
where
T: ::serde::Serialize + Id<T>,
Expand Down

0 comments on commit 313c92c

Please sign in to comment.