rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf`
authorRaphaël Gomès <rgomes@octobus.net>
Fri, 29 Nov 2019 17:15:24 +0100
changeset 43827 c27e688fcdc3
parent 43826 5ac243a92e37
child 43828 36444dddaeb4
rust-hg-path: implement `Display` for `HgPath` and `HgPathBuf` This is useful when debugging, to get a human readable output instead of an array of `u8`. Differential Revision: https://phab.mercurial-scm.org/D7523
rust/hg-core/src/utils/hg_path.rs
--- a/rust/hg-core/src/utils/hg_path.rs	Mon Oct 14 13:57:30 2019 +0200
+++ b/rust/hg-core/src/utils/hg_path.rs	Fri Nov 29 17:15:24 2019 +0100
@@ -7,6 +7,7 @@
 
 use std::borrow::Borrow;
 use std::ffi::{OsStr, OsString};
+use std::fmt;
 use std::ops::Deref;
 use std::path::{Path, PathBuf};
 
@@ -162,6 +163,12 @@
     }
 }
 
+impl fmt::Display for HgPath {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", String::from_utf8_lossy(&self.inner))
+    }
+}
+
 #[derive(Eq, Ord, Clone, PartialEq, PartialOrd, Debug, Hash)]
 pub struct HgPathBuf {
     inner: Vec<u8>,
@@ -185,6 +192,12 @@
     }
 }
 
+impl fmt::Display for HgPathBuf {
+    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+        write!(f, "{}", String::from_utf8_lossy(&self.inner))
+    }
+}
+
 impl Deref for HgPathBuf {
     type Target = HgPath;