]> git.lizzy.rs Git - rust.git/commitdiff
Implement `Debug` for `std::path::Components`.
authorCorey Farwell <coreyf@rwell.org>
Mon, 29 Aug 2016 11:17:27 +0000 (07:17 -0400)
committerCorey Farwell <coreyf@rwell.org>
Tue, 30 Aug 2016 16:40:44 +0000 (12:40 -0400)
src/libstd/path.rs

index 67219b6fd1b9c1479466f8b099d2cdb55d9381b3..791b7b2288d5dca4438da1131895bd23b682cf1a 100644 (file)
@@ -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 @@ macro_rules! tc(
                 );
         }
     }
+
+    #[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);
+    }
 }