Skip to content

Commit

Permalink
Implement Debug for std::path::Iter.
Browse files Browse the repository at this point in the history
  • Loading branch information
frewsxcv committed Aug 30, 2016
1 parent f48d385 commit e118d4c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,25 @@ impl<'a> AsRef<OsStr> for Components<'a> {
}
}

#[stable(feature = "path_iter_debug", since = "1.13.0")]
impl<'a> fmt::Debug for Iter<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
struct DebugHelper<'a>(&'a Path);

impl<'a> fmt::Debug for DebugHelper<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_list()
.entries(self.0.iter())
.finish()
}
}

f.debug_tuple("Iter")
.field(&DebugHelper(self.as_path()))
.finish()
}
}

impl<'a> Iter<'a> {
/// Extracts a slice corresponding to the portion of the path remaining for iteration.
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down Expand Up @@ -3523,4 +3542,25 @@ mod tests {
let actual = format!("{:?}", components);
assert_eq!(expected, actual);
}

#[test]
fn test_iter_debug() {
let path = Path::new("/tmp");

let mut iter = path.iter();

let expected = "Iter([\"/\", \"tmp\"])";
let actual = format!("{:?}", iter);
assert_eq!(expected, actual);

let _ = iter.next().unwrap();
let expected = "Iter([\"tmp\"])";
let actual = format!("{:?}", iter);
assert_eq!(expected, actual);

let _ = iter.next().unwrap();
let expected = "Iter([])";
let actual = format!("{:?}", iter);
assert_eq!(expected, actual);
}
}

0 comments on commit e118d4c

Please sign in to comment.