diff --git a/src/collection.rs b/src/collection.rs index bdb0a3ae9..1c38f7711 100644 --- a/src/collection.rs +++ b/src/collection.rs @@ -323,6 +323,19 @@ impl IntoIterator for Collection { } } +impl std::iter::FromIterator for Collection { + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + iter.into_iter() + .fold(Collection::default(), |mut accumulator, object| { + accumulator.push(object); + accumulator + }) + } +} + impl ops::Index> for Collection { type Output = T; fn index(&self, index: Idx) -> &Self::Output { @@ -883,6 +896,23 @@ impl IntoIterator for CollectionWithId { } } +impl std::iter::FromIterator for CollectionWithId +where + T: Id, +{ + #![allow(unused_must_use)] + fn from_iter(iter: I) -> Self + where + I: IntoIterator, + { + iter.into_iter() + .fold(CollectionWithId::default(), |mut accumulator, object| { + accumulator.push(object); + accumulator + }) + } +} + impl ::serde::Serialize for CollectionWithId where T: ::serde::Serialize + Id, @@ -907,3 +937,95 @@ where .and_then(|v| CollectionWithId::new(v).map_err(D::Error::custom)) } } + +#[cfg(test)] +mod tests { + use super::*; + + struct ObjectWithId(String); + impl Id for ObjectWithId { + fn id(&self) -> &str { + &self.0 + } + fn set_id(&mut self, _id: String) { + unimplemented!(); + } + } + + mod collection { + use super::*; + + mod from_iterator { + use super::*; + + #[test] + fn collect() { + let range = vec![42, 43]; + let collection: Collection<_> = range + .into_iter() + .map(|id| ObjectWithId(id.to_string())) + .collect(); + assert_eq!(collection.len(), 2); + + let mut values = collection.values(); + let object = values.next().unwrap(); + assert_eq!(object.0, 42.to_string()); + let object = values.next().unwrap(); + assert_eq!(object.0, 43.to_string()); + } + + #[test] + fn duplicate() { + 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(); + let object = values.next().unwrap(); + assert_eq!(object.0, 42.to_string()); + let object = values.next().unwrap(); + assert_eq!(object.0, 43.to_string()); + let object = values.next().unwrap(); + assert_eq!(object.0, 42.to_string()); + } + } + } + mod collection_with_id { + use super::*; + + mod from_iterator { + use super::*; + + #[test] + fn collect() { + let range = vec![42, 43]; + let collection: CollectionWithId<_> = range + .into_iter() + .map(|id| ObjectWithId(id.to_string())) + .collect(); + assert_eq!(collection.len(), 2); + let object = collection.get("42").unwrap(); + assert_eq!(object.0, 42.to_string()); + let object = collection.get("43").unwrap(); + assert_eq!(object.0, 43.to_string()); + } + + #[test] + fn duplicate() { + let range = vec![42, 43, 42]; + let collection: CollectionWithId<_> = range + .into_iter() + .map(|id| ObjectWithId(id.to_string())) + .collect(); + assert_eq!(collection.len(), 2); + let object = collection.get("42").unwrap(); + assert_eq!(object.0, 42.to_string()); + let object = collection.get("43").unwrap(); + assert_eq!(object.0, 43.to_string()); + } + } + } +}