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 Sep 12, 2019
1 parent f89d728 commit de8105a
Showing 1 changed file with 122 additions and 0 deletions.
122 changes: 122 additions & 0 deletions collection/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,19 @@ impl<T> IntoIterator for Collection<T> {
}
}

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 @@ -1034,6 +1047,23 @@ impl<T> IntoIterator for CollectionWithId<T> {
}
}

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 All @@ -1058,3 +1088,95 @@ where
.and_then(|v| CollectionWithId::new(v).map_err(D::Error::custom))
}
}

#[cfg(test)]
mod tests {
use super::*;

struct ObjectWithId(String);
impl Id<ObjectWithId> 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());
}
}
}
}

0 comments on commit de8105a

Please sign in to comment.