diff --git a/src/lib.rs b/src/lib.rs index aa419da3..489e5486 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,6 +162,9 @@ impl Bucket { fn key(self) -> K { self.key } + fn value(self) -> V { + self.value + } fn key_value(self) -> (K, V) { (self.key, self.value) } diff --git a/src/map.rs b/src/map.rs index a482a399..aa8d06a0 100644 --- a/src/map.rs +++ b/src/map.rs @@ -230,6 +230,13 @@ impl IndexMap { } } + /// Return an owning iterator over the keys of the map, in their order + pub fn into_keys(self) -> IntoKeys { + IntoKeys { + iter: self.into_entries().into_iter(), + } + } + /// Return an iterator over the values of the map, in their order pub fn values(&self) -> Values<'_, K, V> { Values { @@ -245,6 +252,13 @@ impl IndexMap { } } + /// Return an owning iterator over the values of the map, in their order + pub fn into_values(self) -> IntoValues { + IntoValues { + iter: self.into_entries().into_iter(), + } + } + /// Remove all key-value pairs in the map, while preserving its capacity. /// /// Computes in **O(n)** time. @@ -825,6 +839,42 @@ impl fmt::Debug for Keys<'_, K, V> { } } +/// An owning iterator over the keys of a `IndexMap`. +/// +/// This `struct` is created by the [`into_keys`] method on [`IndexMap`]. +/// See its documentation for more. +/// +/// [`IndexMap`]: struct.IndexMap.html +/// [`into_keys`]: struct.IndexMap.html#method.into_keys +pub struct IntoKeys { + iter: vec::IntoIter>, +} + +impl Iterator for IntoKeys { + type Item = K; + + iterator_methods!(Bucket::key); +} + +impl DoubleEndedIterator for IntoKeys { + fn next_back(&mut self) -> Option { + self.iter.next_back().map(Bucket::key) + } +} + +impl ExactSizeIterator for IntoKeys { + fn len(&self) -> usize { + self.iter.len() + } +} + +impl fmt::Debug for IntoKeys { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let iter = self.iter.as_slice().iter().map(Bucket::key_ref); + f.debug_list().entries(iter).finish() + } +} + /// An iterator over the values of a `IndexMap`. /// /// This `struct` is created by the [`values`] method on [`IndexMap`]. See its @@ -898,6 +948,42 @@ impl ExactSizeIterator for ValuesMut<'_, K, V> { } } +/// An owning iterator over the values of a `IndexMap`. +/// +/// This `struct` is created by the [`into_values`] method on [`IndexMap`]. +/// See its documentation for more. +/// +/// [`IndexMap`]: struct.IndexMap.html +/// [`into_values`]: struct.IndexMap.html#method.into_values +pub struct IntoValues { + iter: vec::IntoIter>, +} + +impl Iterator for IntoValues { + type Item = V; + + iterator_methods!(Bucket::value); +} + +impl DoubleEndedIterator for IntoValues { + fn next_back(&mut self) -> Option { + self.iter.next_back().map(Bucket::value) + } +} + +impl ExactSizeIterator for IntoValues { + fn len(&self) -> usize { + self.iter.len() + } +} + +impl fmt::Debug for IntoValues { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let iter = self.iter.as_slice().iter().map(Bucket::value_ref); + f.debug_list().entries(iter).finish() + } +} + /// An iterator over the entries of a `IndexMap`. /// /// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its @@ -1683,6 +1769,17 @@ mod tests { assert!(keys.contains(&3)); } + #[test] + fn into_keys() { + let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; + let map: IndexMap<_, _> = vec.into_iter().collect(); + let keys: Vec = map.into_keys().collect(); + assert_eq!(keys.len(), 3); + assert!(keys.contains(&1)); + assert!(keys.contains(&2)); + assert!(keys.contains(&3)); + } + #[test] fn values() { let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; @@ -1707,4 +1804,15 @@ mod tests { assert!(values.contains(&4)); assert!(values.contains(&6)); } + + #[test] + fn into_values() { + let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')]; + let map: IndexMap<_, _> = vec.into_iter().collect(); + let values: Vec = map.into_values().collect(); + assert_eq!(values.len(), 3); + assert!(values.contains(&'a')); + assert!(values.contains(&'b')); + assert!(values.contains(&'c')); + } }