Skip to content

Commit

Permalink
Rollup merge of rust-lang#36101 - frewsxcv:debug-path-components, r=a…
Browse files Browse the repository at this point in the history
…lexcrichton

Implement `Debug` for `std::path::{Components,Iter}`.

None
  • Loading branch information
Jonathan Turner authored Aug 31, 2016
2 parents 50deebc + 268b3f5 commit fc80e7d
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/libstd/path.rs
Original file line number Diff line number Diff line change
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 @@ -818,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 @@ -3483,4 +3521,47 @@ 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);
}

#[cfg(unix)]
#[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 fc80e7d

Please sign in to comment.