Skip to content

Commit 1048ea2

Browse files
author
Jonathan Turner
authoredAug 31, 2016
Rollup merge of rust-lang#36101 - frewsxcv:debug-path-components, r=alexcrichton
Implement `Debug` for `std::path::{Components,Iter}`. None
2 parents 18b5ae3 + 268b3f5 commit 1048ea2

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
 

‎src/libstd/path.rs

+81
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,25 @@ pub struct Iter<'a> {
639639
inner: Components<'a>,
640640
}
641641

642+
#[stable(feature = "path_components_debug", since = "1.13.0")]
643+
impl<'a> fmt::Debug for Components<'a> {
644+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
645+
struct DebugHelper<'a>(&'a Path);
646+
647+
impl<'a> fmt::Debug for DebugHelper<'a> {
648+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
649+
f.debug_list()
650+
.entries(self.0.components())
651+
.finish()
652+
}
653+
}
654+
655+
f.debug_tuple("Components")
656+
.field(&DebugHelper(self.as_path()))
657+
.finish()
658+
}
659+
}
660+
642661
impl<'a> Components<'a> {
643662
// how long is the prefix, if any?
644663
#[inline]
@@ -818,6 +837,25 @@ impl<'a> AsRef<OsStr> for Components<'a> {
818837
}
819838
}
820839

840+
#[stable(feature = "path_iter_debug", since = "1.13.0")]
841+
impl<'a> fmt::Debug for Iter<'a> {
842+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
843+
struct DebugHelper<'a>(&'a Path);
844+
845+
impl<'a> fmt::Debug for DebugHelper<'a> {
846+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
847+
f.debug_list()
848+
.entries(self.0.iter())
849+
.finish()
850+
}
851+
}
852+
853+
f.debug_tuple("Iter")
854+
.field(&DebugHelper(self.as_path()))
855+
.finish()
856+
}
857+
}
858+
821859
impl<'a> Iter<'a> {
822860
/// Extracts a slice corresponding to the portion of the path remaining for iteration.
823861
#[stable(feature = "rust1", since = "1.0.0")]
@@ -3483,4 +3521,47 @@ mod tests {
34833521
);
34843522
}
34853523
}
3524+
3525+
#[test]
3526+
fn test_components_debug() {
3527+
let path = Path::new("/tmp");
3528+
3529+
let mut components = path.components();
3530+
3531+
let expected = "Components([RootDir, Normal(\"tmp\")])";
3532+
let actual = format!("{:?}", components);
3533+
assert_eq!(expected, actual);
3534+
3535+
let _ = components.next().unwrap();
3536+
let expected = "Components([Normal(\"tmp\")])";
3537+
let actual = format!("{:?}", components);
3538+
assert_eq!(expected, actual);
3539+
3540+
let _ = components.next().unwrap();
3541+
let expected = "Components([])";
3542+
let actual = format!("{:?}", components);
3543+
assert_eq!(expected, actual);
3544+
}
3545+
3546+
#[cfg(unix)]
3547+
#[test]
3548+
fn test_iter_debug() {
3549+
let path = Path::new("/tmp");
3550+
3551+
let mut iter = path.iter();
3552+
3553+
let expected = "Iter([\"/\", \"tmp\"])";
3554+
let actual = format!("{:?}", iter);
3555+
assert_eq!(expected, actual);
3556+
3557+
let _ = iter.next().unwrap();
3558+
let expected = "Iter([\"tmp\"])";
3559+
let actual = format!("{:?}", iter);
3560+
assert_eq!(expected, actual);
3561+
3562+
let _ = iter.next().unwrap();
3563+
let expected = "Iter([])";
3564+
let actual = format!("{:?}", iter);
3565+
assert_eq!(expected, actual);
3566+
}
34863567
}

0 commit comments

Comments
 (0)
Please sign in to comment.