Patchwork D9860: rust: replace Node::encode_hex with std::fmt::LowerHex

login
register
mail settings
Submitter phabricator
Date Jan. 25, 2021, 9:12 p.m.
Message ID <differential-rev-PHID-DREV-io77vv6obiziuubsxrme-req@mercurial-scm.org>
Download mbox | patch
Permalink /patch/48171/
State Superseded
Headers show

Comments

phabricator - Jan. 25, 2021, 9:12 p.m.
SimonSapin created this revision.
Herald added a reviewer: hg-reviewers.
Herald added a subscriber: mercurial-patches.

REVISION SUMMARY
  This avoids allocating intermediate strings.

REPOSITORY
  rHG Mercurial

BRANCH
  default

REVISION DETAIL
  https://phab.mercurial-scm.org/D9860

AFFECTED FILES
  rust/hg-core/examples/nodemap/main.rs
  rust/hg-core/src/revlog/node.rs

CHANGE DETAILS




To: SimonSapin, #hg-reviewers
Cc: mercurial-patches, mercurial-devel

Patch

diff --git a/rust/hg-core/src/revlog/node.rs b/rust/hg-core/src/revlog/node.rs
--- a/rust/hg-core/src/revlog/node.rs
+++ b/rust/hg-core/src/revlog/node.rs
@@ -10,6 +10,7 @@ 
 
 use hex::{self, FromHex, FromHexError};
 use std::convert::{TryFrom, TryInto};
+use std::fmt;
 
 /// The length in bytes of a `Node`
 ///
@@ -79,6 +80,15 @@ 
     }
 }
 
+impl fmt::LowerHex for Node {
+    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+        for &byte in &self.data {
+            write!(f, "{:02x}", byte)?
+        }
+        Ok(())
+    }
+}
+
 #[derive(Debug, PartialEq)]
 pub enum NodeError {
     ExactLengthRequired(usize, String),
@@ -123,14 +133,6 @@ 
             .into())
     }
 
-    /// Convert to hexadecimal string representation
-    ///
-    /// To be used in FFI and I/O only, in order to facilitate future
-    /// changes of hash format.
-    pub fn encode_hex(&self) -> String {
-        hex::encode(self.data)
-    }
-
     /// Provide access to binary data
     ///
     /// This is needed by FFI layers, for instance to return expected
@@ -348,7 +350,7 @@ 
 
     #[test]
     fn test_node_encode_hex() {
-        assert_eq!(sample_node().encode_hex(), sample_node_hex());
+        assert_eq!(format!("{:x}", sample_node()), sample_node_hex());
     }
 
     #[test]
@@ -390,7 +392,7 @@ 
                 "testgr".to_string()
             ))
         );
-        let mut long = NULL_NODE.encode_hex();
+        let mut long = format!("{:x}", NULL_NODE);
         long.push('c');
         match NodePrefix::from_hex(&long)
             .expect_err("should be refused as too long")
diff --git a/rust/hg-core/examples/nodemap/main.rs b/rust/hg-core/examples/nodemap/main.rs
--- a/rust/hg-core/examples/nodemap/main.rs
+++ b/rust/hg-core/examples/nodemap/main.rs
@@ -66,7 +66,7 @@ 
         .collect();
     if queries < 10 {
         let nodes_hex: Vec<String> =
-            nodes.iter().map(|n| n.encode_hex()).collect();
+            nodes.iter().map(|n| format!("{:x}", n)).collect();
         println!("Nodes: {:?}", nodes_hex);
     }
     let mut last: Option<Revision> = None;
@@ -76,11 +76,11 @@ 
     }
     let elapsed = start.elapsed();
     println!(
-        "Did {} queries in {:?} (mean {:?}), last was {:?} with result {:?}",
+        "Did {} queries in {:?} (mean {:?}), last was {:x} with result {:?}",
         queries,
         elapsed,
         elapsed / (queries as u32),
-        nodes.last().unwrap().encode_hex(),
+        nodes.last().unwrap(),
         last
     );
 }