diff options
| author | Corey Farwell <coreyf@rwell.org> | 2016-08-29 07:17:27 -0400 |
|---|---|---|
| committer | Corey Farwell <coreyf@rwell.org> | 2016-08-30 12:40:44 -0400 |
| commit | f48d3859bc90e6e8a2e5455845b13d55bb8720ab (patch) | |
| tree | 8eb33a46eb2501393c088f8850f6c667eddf568a /src/libstd | |
| parent | acd3f796d26e9295db1eba1ef16e0d4cc3b96dd5 (diff) | |
| download | rust-f48d3859bc90e6e8a2e5455845b13d55bb8720ab.tar.gz rust-f48d3859bc90e6e8a2e5455845b13d55bb8720ab.zip | |
Implement `Debug` for `std::path::Components`.
Diffstat (limited to 'src/libstd')
| -rw-r--r-- | src/libstd/path.rs | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 67219b6fd1b..791b7b2288d 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -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] @@ -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); + } } |
