Skip to content

Commit 811dfe4

Browse files
committed
persist: tweak Display and Debug formatting of ids
All three of shard, writer, and reader start with unique letters, so add the letter to the beginning of the Display impl so they can easily be distinguished. Also manually implement the Debug impl so it prints like a UUID instead of the raw bytes.
1 parent 524ec56 commit 811dfe4

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

src/persist-client/src/lib.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,18 @@ impl Location {
112112
}
113113

114114
/// An opaque identifier for a persist durable TVC (aka shard).
115-
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
115+
#[derive(Clone, Copy, PartialEq, Eq, Hash, Deserialize, Serialize)]
116116
pub struct ShardId([u8; 16]);
117117

118118
impl std::fmt::Display for ShardId {
119119
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120-
std::fmt::Display::fmt(&Uuid::from_bytes(self.0), f)
120+
write!(f, "s{}", Uuid::from_bytes(self.0))
121+
}
122+
}
123+
124+
impl std::fmt::Debug for ShardId {
125+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
126+
write!(f, "ShardId({})", Uuid::from_bytes(self.0))
121127
}
122128
}
123129

@@ -414,4 +420,32 @@ mod tests {
414420

415421
Ok(())
416422
}
423+
424+
#[test]
425+
fn fmt_ids() {
426+
assert_eq!(
427+
format!("{}", ShardId([0u8; 16])),
428+
"s00000000-0000-0000-0000-000000000000"
429+
);
430+
assert_eq!(
431+
format!("{:?}", ShardId([0u8; 16])),
432+
"ShardId(00000000-0000-0000-0000-000000000000)"
433+
);
434+
assert_eq!(
435+
format!("{}", WriterId([0u8; 16])),
436+
"w00000000-0000-0000-0000-000000000000"
437+
);
438+
assert_eq!(
439+
format!("{:?}", WriterId([0u8; 16])),
440+
"WriterId(00000000-0000-0000-0000-000000000000)"
441+
);
442+
assert_eq!(
443+
format!("{}", ReaderId([0u8; 16])),
444+
"r00000000-0000-0000-0000-000000000000"
445+
);
446+
assert_eq!(
447+
format!("{:?}", ReaderId([0u8; 16])),
448+
"ReaderId(00000000-0000-0000-0000-000000000000)"
449+
);
450+
}
417451
}

src/persist-client/src/read.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@ use crate::r#impl::machine::Machine;
3232
use crate::ShardId;
3333

3434
/// An opaque identifier for a reader of a persist durable TVC (aka shard).
35-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
35+
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
3636
pub struct ReaderId(pub(crate) [u8; 16]);
3737

3838
impl std::fmt::Display for ReaderId {
3939
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
40-
std::fmt::Display::fmt(&Uuid::from_bytes(self.0), f)
40+
write!(f, "r{}", Uuid::from_bytes(self.0))
41+
}
42+
}
43+
44+
impl std::fmt::Debug for ReaderId {
45+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
46+
write!(f, "ReaderId({})", Uuid::from_bytes(self.0))
4147
}
4248
}
4349

src/persist-client/src/write.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,18 @@ use crate::r#impl::machine::Machine;
3636
/// Deserialize. It's difficult to reason about the behavior if multiple writers
3737
/// accidentally end up concurrently using the same [WriterId] and we haven't
3838
/// (yet) found a need for it.
39-
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
39+
#[derive(Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
4040
pub struct WriterId(pub(crate) [u8; 16]);
4141

4242
impl std::fmt::Display for WriterId {
4343
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44-
std::fmt::Display::fmt(&Uuid::from_bytes(self.0), f)
44+
write!(f, "w{}", Uuid::from_bytes(self.0))
45+
}
46+
}
47+
48+
impl std::fmt::Debug for WriterId {
49+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50+
write!(f, "WriterId({})", Uuid::from_bytes(self.0))
4551
}
4652
}
4753

0 commit comments

Comments
 (0)