Skip to content

Commit

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

#[stable(feature = "path_components_debug", since = "1.13.0")]
impl<'a> fmt::Debug for Components<'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.components())
.finish()
}
}

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

impl<'a> Components<'a> {
// how long is the prefix, if any?
#[inline]
Expand Down Expand Up @@ -3483,4 +3502,25 @@ mod tests {
);
}
}

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

let mut components = path.components();

let expected = "Components([RootDir, Normal(\"tmp\")])";
let actual = format!("{:?}", components);
assert_eq!(expected, actual);

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

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

0 comments on commit f48d385

Please sign in to comment.