Skip to content

Implement Default for iterators #261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/map/iter.rs
Original file line number Diff line number Diff line change
@@ -89,6 +89,12 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Iter<'_, K, V> {
}
}

impl<K, V> Default for Iter<'_, K, V> {
fn default() -> Self {
Self { iter: [].iter() }
}
}

/// A mutable iterator over the entries of a `IndexMap`.
///
/// This `struct` is created by the [`iter_mut`] method on [`IndexMap`]. See its
@@ -145,6 +151,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IterMut<'_, K, V> {
}
}

impl<K, V> Default for IterMut<'_, K, V> {
fn default() -> Self {
Self {
iter: [].iter_mut(),
}
}
}

/// An owning iterator over the entries of a `IndexMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
@@ -199,6 +213,14 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
}
}

impl<K, V> Default for IntoIter<K, V> {
fn default() -> Self {
Self {
iter: Vec::new().into_iter(),
}
}
}

/// A draining iterator over the entries of a `IndexMap`.
///
/// This `struct` is created by the [`drain`] method on [`IndexMap`]. See its
@@ -298,6 +320,12 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
}
}

impl<K, V> Default for Keys<'_, K, V> {
fn default() -> Self {
Self { iter: [].iter() }
}
}

/// An owning iterator over the keys of a `IndexMap`.
///
/// This `struct` is created by the [`into_keys`] method on [`IndexMap`].
@@ -342,6 +370,14 @@ impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
}
}

impl<K, V> Default for IntoKeys<K, V> {
fn default() -> Self {
Self {
iter: Vec::new().into_iter(),
}
}
}

/// An iterator over the values of a `IndexMap`.
///
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
@@ -394,6 +430,12 @@ impl<K, V: fmt::Debug> fmt::Debug for Values<'_, K, V> {
}
}

impl<K, V> Default for Values<'_, K, V> {
fn default() -> Self {
Self { iter: [].iter() }
}
}

/// A mutable iterator over the values of a `IndexMap`.
///
/// This `struct` is created by the [`values_mut`] method on [`IndexMap`]. See its
@@ -438,6 +480,14 @@ impl<K, V: fmt::Debug> fmt::Debug for ValuesMut<'_, K, V> {
}
}

impl<K, V> Default for ValuesMut<'_, K, V> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, oddly std left this out, but I think it's fine.

fn default() -> Self {
Self {
iter: [].iter_mut(),
}
}
}

/// An owning iterator over the values of a `IndexMap`.
///
/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
@@ -481,3 +531,11 @@ impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
f.debug_list().entries(iter).finish()
}
}

impl<K, V> Default for IntoValues<K, V> {
fn default() -> Self {
Self {
iter: Vec::new().into_iter(),
}
}
}
20 changes: 20 additions & 0 deletions src/map/tests.rs
Original file line number Diff line number Diff line change
@@ -427,3 +427,23 @@ fn from_array() {

assert_eq!(map, expected)
}

#[test]
fn iter_default() {
struct K;
struct V;
fn assert_default<T>()
where
T: Default + Iterator,
{
assert!(T::default().next().is_none());
}
assert_default::<Iter<'static, K, V>>();
assert_default::<IterMut<'static, K, V>>();
assert_default::<IntoIter<K, V>>();
assert_default::<Keys<'static, K, V>>();
assert_default::<IntoKeys<K, V>>();
assert_default::<Values<'static, K, V>>();
assert_default::<ValuesMut<'static, K, V>>();
assert_default::<IntoValues<K, V>>();
}
14 changes: 14 additions & 0 deletions src/set/iter.rs
Original file line number Diff line number Diff line change
@@ -80,6 +80,12 @@ impl<T: fmt::Debug> fmt::Debug for Iter<'_, T> {
}
}

impl<T> Default for Iter<'_, T> {
fn default() -> Self {
Self { iter: [].iter() }
}
}

/// An owning iterator over the items of a `IndexSet`.
///
/// This `struct` is created by the [`into_iter`] method on [`IndexSet`]
@@ -129,6 +135,14 @@ impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
}
}

impl<T> Default for IntoIter<T> {
fn default() -> Self {
Self {
iter: Vec::new().into_iter(),
}
}
}

/// A draining iterator over the items of a `IndexSet`.
///
/// This `struct` is created by the [`drain`] method on [`IndexSet`].
13 changes: 13 additions & 0 deletions src/set/tests.rs
Original file line number Diff line number Diff line change
@@ -530,3 +530,16 @@ fn from_array() {

assert_eq!(set1, set2);
}

#[test]
fn iter_default() {
struct Item;
fn assert_default<T>()
where
T: Default + Iterator,
{
assert!(T::default().next().is_none());
}
assert_default::<Iter<'static, Item>>();
assert_default::<IntoIter<Item>>();
}