Skip to content

Add Debug implementations for libcollection structs #39002

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 4 commits into from
Feb 7, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
45 changes: 45 additions & 0 deletions src/libcollections/binary_heap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ pub struct PeekMut<'a, T: 'a + Ord> {
sift: bool,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
Copy link
Member

Choose a reason for hiding this comment

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

These should all be "1.16.0".

impl<'a, T: Ord + fmt::Debug> fmt::Debug for PeekMut<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("PeekMut")
Copy link
Member

Choose a reason for hiding this comment

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

To match the others, should this not be "BinaryHeap::PeekMut"? Although it might be better to use part of the actual path like "binary_heap::PeekMut" for all of these instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer to refer to the struct used to create the iterator rather than the path of the iterator.

.field(&self.heap.data[0])
.finish()
}
}

#[stable(feature = "binary_heap_peek_mut", since = "1.12.0")]
impl<'a, T: Ord> Drop for PeekMut<'a, T> {
fn drop(&mut self) {
Expand Down Expand Up @@ -968,6 +977,15 @@ pub struct Iter<'a, T: 'a> {
iter: slice::Iter<'a, T>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Iter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BinaryHeap::Iter")
.field(&self.iter.as_slice())
.finish()
}
}

// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
#[stable(feature = "rust1", since = "1.0.0")]
impl<'a, T> Clone for Iter<'a, T> {
Expand Down Expand Up @@ -1016,6 +1034,15 @@ pub struct IntoIter<T> {
iter: vec::IntoIter<T>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BinaryHeap::IntoIter")
.field(&self.iter.as_slice())
.finish()
}
}

#[stable(feature = "rust1", since = "1.0.0")]
impl<T> Iterator for IntoIter<T> {
type Item = T;
Expand Down Expand Up @@ -1055,6 +1082,15 @@ pub struct Drain<'a, T: 'a> {
iter: vec::Drain<'a, T>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Drain<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BinaryHeap::Drain")
.field(&self.iter)
.finish()
}
}

#[stable(feature = "drain", since = "1.6.0")]
impl<'a, T: 'a> Iterator for Drain<'a, T> {
type Item = T;
Expand Down Expand Up @@ -1200,6 +1236,15 @@ where T: Clone + Ord {
place: vec::PlaceBack<'a, T>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
Copy link
Member

Choose a reason for hiding this comment

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

BinaryHeapPlace is unstable so this impl should get the same stability attribute as BinaryHeapPlace.

impl<'a, T: Clone + Ord + fmt::Debug> fmt::Debug for BinaryHeapPlace<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BinaryHeapPlace")
.field(&self)
.finish()
}
}

#[unstable(feature = "collection_placement",
reason = "placement protocol is subject to change",
issue = "30172")]
Expand Down
64 changes: 64 additions & 0 deletions src/libcollections/btree/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,27 @@ pub struct Iter<'a, K: 'a, V: 'a> {
length: usize,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for Iter<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

/// A mutable iterator over a BTreeMap's entries.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IterMut<'a, K: 'a, V: 'a> {
range: RangeMut<'a, K, V>,
length: usize,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for IterMut<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad(&format!("BTreeMap::IterMut({:?})", self.range))
}
}

/// An owning iterator over a BTreeMap's entries.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct IntoIter<K, V> {
Expand All @@ -285,30 +299,69 @@ pub struct IntoIter<K, V> {
length: usize,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let range = Range {
front: self.front.reborrow(),
back: self.back.reborrow(),
};
f.debug_list().entries(range).finish()
}
}

/// An iterator over a BTreeMap's keys.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Keys<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for Keys<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.inner.clone()).finish()
}
}

/// An iterator over a BTreeMap's values.
#[stable(feature = "rust1", since = "1.0.0")]
pub struct Values<'a, K: 'a, V: 'a> {
inner: Iter<'a, K, V>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for Values<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.inner.clone()).finish()
}
}

/// A mutable iterator over a BTreeMap's values.
#[stable(feature = "map_values_mut", since = "1.10.0")]
pub struct ValuesMut<'a, K: 'a, V: 'a> {
inner: IterMut<'a, K, V>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for ValuesMut<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad(&format!("BTreeMap::ValuesMut({:?})", self.inner))
}
}

/// An iterator over a sub-range of BTreeMap's entries.
pub struct Range<'a, K: 'a, V: 'a> {
front: Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Edge>,
back: Handle<NodeRef<marker::Immut<'a>, K, V, marker::Leaf>, marker::Edge>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for Range<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list().entries(self.clone()).finish()
}
}

/// A mutable iterator over a sub-range of BTreeMap's entries.
pub struct RangeMut<'a, K: 'a, V: 'a> {
front: Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge>,
Expand All @@ -318,6 +371,17 @@ pub struct RangeMut<'a, K: 'a, V: 'a> {
_marker: PhantomData<&'a mut (K, V)>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, K: 'a + fmt::Debug, V: 'a + fmt::Debug> fmt::Debug for RangeMut<'a, K, V> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let range = Range {
front: self.front.reborrow(),
back: self.back.reborrow(),
};
f.debug_list().entries(range).finish()
}
}

/// A view into a single entry in a map, which may either be vacant or occupied.
/// This enum is constructed from the [`entry`] method on [`BTreeMap`].
///
Expand Down
59 changes: 59 additions & 0 deletions src/libcollections/btree/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ pub struct Iter<'a, T: 'a> {
iter: Keys<'a, T, ()>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Iter<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BTreeSet::Iter")
.field(&self.iter.clone())
.finish()
}
}

/// An owning iterator over a `BTreeSet`'s items.
///
/// This structure is created by the `into_iter` method on [`BTreeSet`]
Expand All @@ -96,6 +105,13 @@ pub struct IntoIter<T> {
iter: ::btree_map::IntoIter<T, ()>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad(&format!("BTreeSet::IntoIter({:?})", self.iter))
Copy link
Member

Choose a reason for hiding this comment

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

There's still intermediate strings here (and in other places)

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I'll do a pass for the missing ones. Sorry about this.

}
}

/// An iterator over a sub-range of `BTreeSet`'s items.
///
/// This structure is created by the [`range`] method on [`BTreeSet`].
Expand All @@ -106,6 +122,13 @@ pub struct Range<'a, T: 'a> {
iter: ::btree_map::Range<'a, T, ()>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Range<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad(&format!("BTreeSet::Range({:?})", self.iter))
}
}

/// A lazy iterator producing elements in the set difference (in-order).
///
/// This structure is created by the [`difference`] method on [`BTreeSet`].
Expand All @@ -118,6 +141,15 @@ pub struct Difference<'a, T: 'a> {
b: Peekable<Iter<'a, T>>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Difference<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BTreeSet::Difference")
.field(&self.clone())
.finish()
}
}

/// A lazy iterator producing elements in the set symmetric difference (in-order).
///
/// This structure is created by the [`symmetric_difference`] method on
Expand All @@ -131,6 +163,15 @@ pub struct SymmetricDifference<'a, T: 'a> {
b: Peekable<Iter<'a, T>>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for SymmetricDifference<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BTreeSet::SymmetricDifference")
.field(&self.clone())
.finish()
}
}

/// A lazy iterator producing elements in the set intersection (in-order).
///
/// This structure is created by the [`intersection`] method on [`BTreeSet`].
Expand All @@ -143,6 +184,15 @@ pub struct Intersection<'a, T: 'a> {
b: Peekable<Iter<'a, T>>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Intersection<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BTreeSet::Intersection")
.field(&self.clone())
.finish()
}
}

/// A lazy iterator producing elements in the set union (in-order).
///
/// This structure is created by the [`union`] method on [`BTreeSet`].
Expand All @@ -155,6 +205,15 @@ pub struct Union<'a, T: 'a> {
b: Peekable<Iter<'a, T>>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
impl<'a, T: 'a + fmt::Debug> fmt::Debug for Union<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("BTreeSet::Union")
.field(&self.clone())
.finish()
}
}

impl<T: Ord> BTreeSet<T> {
/// Makes a new `BTreeSet` with a reasonable choice of B.
///
Expand Down
9 changes: 9 additions & 0 deletions src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ pub struct Iter<E> {
marker: marker::PhantomData<E>,
}

#[stable(feature = "collection_debug", since = "1.15.0")]
Copy link
Member

Choose a reason for hiding this comment

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

EnumSet is unstable so this stability attribute should probably be removed.

impl<E: fmt::Debug> fmt::Debug for Iter<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_tuple("EnumSet::Iter")
.field(&self.clone())
.finish()
}
}

// FIXME(#19839) Remove in favor of `#[derive(Clone)]`
impl<E> Clone for Iter<E> {
fn clone(&self) -> Iter<E> {
Expand Down
1 change: 1 addition & 0 deletions src/libcollections/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#![cfg_attr(test, allow(deprecated))] // rand
#![deny(warnings)]
#![deny(missing_debug_implementations)]

#![feature(alloc)]
#![feature(allow_internal_unstable)]
Expand Down
Loading