##// END OF EJS Templates
rust: replace Node::encode_hex with std::fmt::LowerHex...
Simon Sapin -
r47155:6380efb8 default
parent child Browse files
Show More
@@ -66,7 +66,7 b' fn bench(index: &Index, nm: &NodeTree, q'
66 .collect();
66 .collect();
67 if queries < 10 {
67 if queries < 10 {
68 let nodes_hex: Vec<String> =
68 let nodes_hex: Vec<String> =
69 nodes.iter().map(|n| n.encode_hex()).collect();
69 nodes.iter().map(|n| format!("{:x}", n)).collect();
70 println!("Nodes: {:?}", nodes_hex);
70 println!("Nodes: {:?}", nodes_hex);
71 }
71 }
72 let mut last: Option<Revision> = None;
72 let mut last: Option<Revision> = None;
@@ -76,11 +76,11 b' fn bench(index: &Index, nm: &NodeTree, q'
76 }
76 }
77 let elapsed = start.elapsed();
77 let elapsed = start.elapsed();
78 println!(
78 println!(
79 "Did {} queries in {:?} (mean {:?}), last was {:?} with result {:?}",
79 "Did {} queries in {:?} (mean {:?}), last was {:x} with result {:?}",
80 queries,
80 queries,
81 elapsed,
81 elapsed,
82 elapsed / (queries as u32),
82 elapsed / (queries as u32),
83 nodes.last().unwrap().encode_hex(),
83 nodes.last().unwrap(),
84 last
84 last
85 );
85 );
86 }
86 }
@@ -11,6 +11,7 b''
11 use bytes_cast::BytesCast;
11 use bytes_cast::BytesCast;
12 use hex::{self, FromHex, FromHexError};
12 use hex::{self, FromHex, FromHexError};
13 use std::convert::TryFrom;
13 use std::convert::TryFrom;
14 use std::fmt;
14
15
15 /// The length in bytes of a `Node`
16 /// The length in bytes of a `Node`
16 ///
17 ///
@@ -80,6 +81,15 b" impl<'a> TryFrom<&'a [u8]> for &'a Node "
80 }
81 }
81 }
82 }
82
83
84 impl fmt::LowerHex for Node {
85 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
86 for &byte in &self.data {
87 write!(f, "{:02x}", byte)?
88 }
89 Ok(())
90 }
91 }
92
83 #[derive(Debug, PartialEq)]
93 #[derive(Debug, PartialEq)]
84 pub enum NodeError {
94 pub enum NodeError {
85 ExactLengthRequired(usize, String),
95 ExactLengthRequired(usize, String),
@@ -124,14 +134,6 b' impl Node {'
124 .into())
134 .into())
125 }
135 }
126
136
127 /// Convert to hexadecimal string representation
128 ///
129 /// To be used in FFI and I/O only, in order to facilitate future
130 /// changes of hash format.
131 pub fn encode_hex(&self) -> String {
132 hex::encode(self.data)
133 }
134
135 /// Provide access to binary data
137 /// Provide access to binary data
136 ///
138 ///
137 /// This is needed by FFI layers, for instance to return expected
139 /// This is needed by FFI layers, for instance to return expected
@@ -349,7 +351,7 b' mod tests {'
349
351
350 #[test]
352 #[test]
351 fn test_node_encode_hex() {
353 fn test_node_encode_hex() {
352 assert_eq!(sample_node().encode_hex(), sample_node_hex());
354 assert_eq!(format!("{:x}", sample_node()), sample_node_hex());
353 }
355 }
354
356
355 #[test]
357 #[test]
@@ -391,7 +393,7 b' mod tests {'
391 "testgr".to_string()
393 "testgr".to_string()
392 ))
394 ))
393 );
395 );
394 let mut long = NULL_NODE.encode_hex();
396 let mut long = format!("{:x}", NULL_NODE);
395 long.push('c');
397 long.push('c');
396 match NodePrefix::from_hex(&long)
398 match NodePrefix::from_hex(&long)
397 .expect_err("should be refused as too long")
399 .expect_err("should be refused as too long")
General Comments 0
You need to be logged in to leave comments. Login now