Skip to content

Commit

Permalink
Implement IntoIterator
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Feb 3, 2015
1 parent 139b7b5 commit 2f63ded
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 2 deletions.
9 changes: 9 additions & 0 deletions phf/src/map.rs
Expand Up @@ -4,6 +4,7 @@ use std::borrow::BorrowFrom;
use std::ops::Index;
use std::slice;
use std::fmt;
use std::iter::IntoIterator;
use PhfHash;
use phf_shared;

Expand Down Expand Up @@ -128,6 +129,14 @@ impl<K, V> Map<K, V> {
}
}

impl<'a, K, V> IntoIterator for &'a Map<K, V> {
type Iter = Entries<'a, K, V>;

fn into_iter(self) -> Entries<'a, K, V> {
self.entries()
}
}

/// An iterator over the key/value pairs in a `Map`.
pub struct Entries<'a, K:'a, V:'a> {
iter: slice::Iter<'a, (K, V)>,
Expand Down
10 changes: 9 additions & 1 deletion phf/src/ordered_map.rs
@@ -1,7 +1,7 @@
//! An order-preserving immutable map constructed at compile time.
use std::prelude::v1::*;
use std::borrow::BorrowFrom;
use std::iter::RandomAccessIterator;
use std::iter::{IntoIterator, RandomAccessIterator};
use std::ops::Index;
use std::fmt;
use std::slice;
Expand Down Expand Up @@ -148,6 +148,14 @@ impl<K, V> OrderedMap<K, V> {
}
}

impl<'a, K, V> IntoIterator for &'a OrderedMap<K, V> {
type Iter = Entries<'a, K, V>;

fn into_iter(self) -> Entries<'a, K, V> {
self.entries()
}
}

/// An iterator over the entries in a `OrderedMap`.
pub struct Entries<'a, K:'a, V:'a> {
iter: slice::Iter<'a, (K, V)>,
Expand Down
10 changes: 9 additions & 1 deletion phf/src/ordered_set.rs
@@ -1,7 +1,7 @@
//! An order-preserving immutable set constructed at compile time.
use std::prelude::v1::*;
use std::borrow::BorrowFrom;
use std::iter::RandomAccessIterator;
use std::iter::{IntoIterator, RandomAccessIterator};
use std::fmt;
use ordered_map;
use {PhfHash, OrderedMap};
Expand Down Expand Up @@ -111,6 +111,14 @@ impl<T> OrderedSet<T> where T: Eq + PhfHash {
}
}

impl<'a, T> IntoIterator for &'a OrderedSet<T> {
type Iter = Iter<'a, T>;

fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}

/// An iterator over the values in a `OrderedSet`.
pub struct Iter<'a, T:'a> {
iter: ordered_map::Keys<'a, T, ()>,
Expand Down
9 changes: 9 additions & 0 deletions phf/src/set.rs
@@ -1,6 +1,7 @@
//! An immutable set constructed at compile time.
use std::prelude::v1::*;
use std::borrow::BorrowFrom;
use std::iter::IntoIterator;
use std::fmt;

use PhfHash;
Expand Down Expand Up @@ -99,6 +100,14 @@ impl<T> Set<T> where T: Eq + PhfHash {
}
}

impl<'a, T> IntoIterator for &'a Set<T> {
type Iter = Iter<'a, T>;

fn into_iter(self) -> Iter<'a, T> {
self.iter()
}
}

/// An iterator over the values in a `Set`.
pub struct Iter<'a, T:'static> {
iter: map::Keys<'a, T, ()>,
Expand Down
46 changes: 46 additions & 0 deletions phf/tests/test.rs
Expand Up @@ -220,6 +220,18 @@ mod map {
fn test_bool_keys() {
test_key_type!(bool, false => 0, true => 1);
}

#[test]
fn test_into_iterator() {
static MAP: phf::Map<&'static str, isize> = phf_map!(
"foo" => 10,
);

for (k, v) in &MAP {
assert_eq!(&"foo", k);
assert_eq!(&10, v)
}
}
}

mod set {
Expand Down Expand Up @@ -268,6 +280,17 @@ mod set {
};
assert!(SET.contains(&*"hello".to_string()));
}

#[test]
fn test_into_iterator() {
static SET: phf::Set<&'static str> = phf_set! {
"hello",
};

for e in &SET {
assert_eq!(&"hello", e);
}
}
}

mod ordered_map {
Expand Down Expand Up @@ -371,6 +394,18 @@ mod ordered_map {
);
assert_eq!(Some(&0), MAP.get(&*"a".to_string()));
}

#[test]
fn test_into_iterator() {
static MAP: phf::OrderedMap<&'static str, isize> = phf_ordered_map!(
"foo" => 10,
);

for (k, v) in &MAP {
assert_eq!(&"foo", k);
assert_eq!(&10, v)
}
}
}

mod ordered_set {
Expand Down Expand Up @@ -437,4 +472,15 @@ mod ordered_set {
};
assert!(SET.contains(&*"hello".to_string()));
}

#[test]
fn test_into_iterator() {
static SET: phf::OrderedSet<&'static str> = phf_ordered_set!(
"foo",
);

for e in &SET {
assert_eq!(&"foo", e);
}
}
}

0 comments on commit 2f63ded

Please sign in to comment.