Skip to content

Commit c74a01a

Browse files
KerollmopsHoverbear
authored andcommitted
Improve Key and Value Debug impl to follow the RFC (#27)
* Introduce the HexRepr helper type Signed-off-by: Clément Renault <[email protected]> * Keys Debug impl use the hexadecimal representation Signed-off-by: Clément Renault <[email protected]> * Values Debug impl use the hexadecimal/utf8 representation Signed-off-by: Clément Renault <[email protected]> * KvPairs Debug impl use the hexadecimal/utf8 representation Signed-off-by: Clément Renault <[email protected]>
1 parent c47b92b commit c74a01a

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

src/lib.rs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
use futures::Future;
1717
use serde_derive::*;
1818
use std::{
19+
fmt,
1920
ops::{
2021
Bound, Deref, DerefMut, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo,
2122
RangeToInclusive,
2223
},
2324
path::PathBuf,
25+
str,
2426
time::Duration,
2527
u8::{MAX as U8_MAX, MIN as U8_MIN},
2628
};
@@ -35,6 +37,17 @@ pub use crate::errors::Error;
3537
#[doc(inline)]
3638
pub use crate::errors::Result;
3739

40+
struct HexRepr<'a>(pub &'a [u8]);
41+
42+
impl<'a> fmt::Display for HexRepr<'a> {
43+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44+
for byte in self.0 {
45+
write!(f, "{:02X}", byte)?;
46+
}
47+
Ok(())
48+
}
49+
}
50+
3851
/// The key part of a key/value pair.
3952
///
4053
/// In TiKV, keys are an ordered sequence of bytes. This has an advantage over choosing `String` as
@@ -72,7 +85,7 @@ pub use crate::errors::Result;
7285
/// **But, you should not need to worry about all this:** Many functions which accept a `Key`
7386
/// accept an `Into<Key>`, which means all of the above types can be passed directly to those
7487
/// functions.
75-
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
88+
#[derive(Default, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
7689
pub struct Key(Vec<u8>);
7790

7891
impl Key {
@@ -142,6 +155,12 @@ impl DerefMut for Key {
142155
}
143156
}
144157

158+
impl fmt::Debug for Key {
159+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
160+
write!(f, "Key({})", HexRepr(&self.0))
161+
}
162+
}
163+
145164
/// The value part of a key/value pair.
146165
///
147166
/// In TiKV, values are an ordered sequence of bytes. This has an advantage over choosing `String`
@@ -179,7 +198,7 @@ impl DerefMut for Key {
179198
/// **But, you should not need to worry about all this:** Many functions which accept a `Value`
180199
/// accept an `Into<Value>`, which means all of the above types can be passed directly to those
181200
/// functions.
182-
#[derive(Default, Clone, Eq, PartialEq, Hash, Debug)]
201+
#[derive(Default, Clone, Eq, PartialEq, Hash)]
183202
pub struct Value(Vec<u8>);
184203

185204
impl Value {
@@ -222,6 +241,15 @@ impl Deref for Value {
222241
}
223242
}
224243

244+
impl fmt::Debug for Value {
245+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
246+
match str::from_utf8(&self.0) {
247+
Ok(s) => write!(f, "Value({:?})", s),
248+
Err(_) => write!(f, "Value({})", HexRepr(&self.0)),
249+
}
250+
}
251+
}
252+
225253
/// A key/value pair.
226254
///
227255
/// ```rust
@@ -235,7 +263,7 @@ impl Deref for Value {
235263
///
236264
/// Many functions which accept a `KvPair` accept an `Into<KvPair>`, which means all of the above
237265
/// types (Like a `(Key, Value)`) can be passed directly to those functions.
238-
#[derive(Default, Clone, Eq, PartialEq, Debug)]
266+
#[derive(Default, Clone, Eq, PartialEq)]
239267
pub struct KvPair(Key, Value);
240268

241269
impl KvPair {
@@ -307,6 +335,16 @@ where
307335
}
308336
}
309337

338+
impl fmt::Debug for KvPair {
339+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
340+
let KvPair(key, value) = self;
341+
match str::from_utf8(&value) {
342+
Ok(s) => write!(f, "KvPair({}, {:?})", HexRepr(&key), s),
343+
Err(_) => write!(f, "KvPair({}, {})", HexRepr(&key), HexRepr(&value)),
344+
}
345+
}
346+
}
347+
310348
/// The configuration for either a [`raw::Client`](raw/struct.Client.html) or a
311349
/// [`transaction::Client`](transaction/struct.Client.html).
312350
///

0 commit comments

Comments
 (0)