Skip to content

Commit

Permalink
add trait implementations to iterators mirroring std::collections
Browse files Browse the repository at this point in the history
  • Loading branch information
bhgomes authored and JohnTitor committed Aug 6, 2021
1 parent d71851e commit e47e4dc
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 0 deletions.
62 changes: 62 additions & 0 deletions phf/src/map.rs
@@ -1,5 +1,6 @@
//! An immutable map constructed at compile time.
use core::fmt;
use core::iter::FusedIterator;
use core::iter::IntoIterator;
use core::ops::Index;
use core::slice;
Expand Down Expand Up @@ -148,6 +149,25 @@ pub struct Entries<'a, K, V> {
iter: slice::Iter<'a, (K, V)>,
}

impl<'a, K, V> Clone for Entries<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Entries<'a, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Entries<'a, K, V> {
type Item = (&'a K, &'a V);

Expand All @@ -168,11 +188,31 @@ impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {

impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}

impl<'a, K, V> FusedIterator for Entries<'a, K, V> {}

/// An iterator over the keys in a `Map`.
pub struct Keys<'a, K, V> {
iter: Entries<'a, K, V>,
}

impl<'a, K, V> Clone for Keys<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Keys<'a, K, V>
where
K: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;

Expand All @@ -193,11 +233,31 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {

impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}

impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}

/// An iterator over the values in a `Map`.
pub struct Values<'a, K, V> {
iter: Entries<'a, K, V>,
}

impl<'a, K, V> Clone for Values<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Values<'a, K, V>
where
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;

Expand All @@ -217,3 +277,5 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
}

impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}

impl<'a, K, V> FusedIterator for Values<'a, K, V> {}
62 changes: 62 additions & 0 deletions phf/src/ordered_map.rs
@@ -1,5 +1,6 @@
//! An order-preserving immutable map constructed at compile time.
use core::fmt;
use core::iter::FusedIterator;
use core::iter::IntoIterator;
use core::ops::Index;
use core::slice;
Expand Down Expand Up @@ -179,6 +180,25 @@ pub struct Entries<'a, K, V> {
iter: slice::Iter<'a, (K, V)>,
}

impl<'a, K, V> Clone for Entries<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Entries<'a, K, V>
where
K: fmt::Debug,
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Entries<'a, K, V> {
type Item = (&'a K, &'a V);

Expand All @@ -199,11 +219,31 @@ impl<'a, K, V> DoubleEndedIterator for Entries<'a, K, V> {

impl<'a, K, V> ExactSizeIterator for Entries<'a, K, V> {}

impl<'a, K, V> FusedIterator for Entries<'a, K, V> {}

/// An iterator over the keys in a `OrderedMap`.
pub struct Keys<'a, K, V> {
iter: Entries<'a, K, V>,
}

impl<'a, K, V> Clone for Keys<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Keys<'a, K, V>
where
K: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Keys<'a, K, V> {
type Item = &'a K;

Expand All @@ -224,11 +264,31 @@ impl<'a, K, V> DoubleEndedIterator for Keys<'a, K, V> {

impl<'a, K, V> ExactSizeIterator for Keys<'a, K, V> {}

impl<'a, K, V> FusedIterator for Keys<'a, K, V> {}

/// An iterator over the values in a `OrderedMap`.
pub struct Values<'a, K, V> {
iter: Entries<'a, K, V>,
}

impl<'a, K, V> Clone for Values<'a, K, V> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, K, V> fmt::Debug for Values<'a, K, V>
where
V: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, K, V> Iterator for Values<'a, K, V> {
type Item = &'a V;

Expand All @@ -248,3 +308,5 @@ impl<'a, K, V> DoubleEndedIterator for Values<'a, K, V> {
}

impl<'a, K, V> ExactSizeIterator for Values<'a, K, V> {}

impl<'a, K, V> FusedIterator for Values<'a, K, V> {}
21 changes: 21 additions & 0 deletions phf/src/ordered_set.rs
@@ -1,6 +1,7 @@
//! An order-preserving immutable set constructed at compile time.
use crate::{ordered_map, OrderedMap, PhfHash};
use core::fmt;
use core::iter::FusedIterator;
use core::iter::IntoIterator;
use phf_shared::PhfBorrow;

Expand Down Expand Up @@ -125,6 +126,24 @@ pub struct Iter<'a, T> {
iter: ordered_map::Keys<'a, T, ()>,
}

impl<'a, T> Clone for Iter<'a, T> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, T> fmt::Debug for Iter<'a, T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand All @@ -147,3 +166,5 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {}

impl<'a, T> FusedIterator for Iter<'a, T> {}
21 changes: 21 additions & 0 deletions phf/src/set.rs
@@ -1,5 +1,6 @@
//! An immutable set constructed at compile time.
use core::fmt;
use core::iter::FusedIterator;
use core::iter::IntoIterator;

use phf_shared::{PhfBorrow, PhfHash};
Expand Down Expand Up @@ -105,6 +106,24 @@ pub struct Iter<'a, T: 'static> {
iter: map::Keys<'a, T, ()>,
}

impl<'a, T> Clone for Iter<'a, T> {
#[inline]
fn clone(&self) -> Self {
Self {
iter: self.iter.clone(),
}
}
}

impl<'a, T> fmt::Debug for Iter<'a, T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

impl<'a, T> Iterator for Iter<'a, T> {
type Item = &'a T;

Expand All @@ -124,3 +143,5 @@ impl<'a, T> DoubleEndedIterator for Iter<'a, T> {
}

impl<'a, T> ExactSizeIterator for Iter<'a, T> {}

impl<'a, T> FusedIterator for Iter<'a, T> {}

0 comments on commit e47e4dc

Please sign in to comment.