1macro_rules! derive_debug_via_id {
18 ($typename:ident) => {
19 impl ::core::fmt::Debug for $typename {
20 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
21 ::core::fmt::Debug::fmt(&self.id, f)
22 }
23 }
24 };
25}
26
27macro_rules! derive_debug_via_field {
28 ($type:ty, $field:ident) => {
29 derive_debug_via_field!($type, stringify!($type), $field);
30 };
31
32 ($type:ty, $typename:expr, $field:ident) => {
33 impl ::core::fmt::Debug for $type {
34 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
35 f.debug_struct($typename)
36 .field(stringify!($field), &self.$field)
37 .finish()
38 }
39 }
40 };
41}
42
43macro_rules! derive_debug_self_as_ref_hex_bytes {
46 ($typename:ident) => {
47 impl ::core::fmt::Debug for $typename {
48 fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
49 crate::debug::write_hex_tuple(f, stringify!($typename), self)
50 }
51 }
52 };
53}
54
55pub(crate) fn write_hex_tuple(
56 fmt: &mut core::fmt::Formatter,
57 type_name: &str,
58 value: &dyn AsRef<[u8]>,
59) -> Result<(), ::core::fmt::Error> {
60 fmt.debug_tuple(type_name)
61 .field(&HexStr(value.as_ref()))
62 .finish()
63}
64
65pub struct HexStr<'a>(pub &'a [u8]);
66
67impl core::fmt::Debug for HexStr<'_> {
68 fn fmt(&self, fmt: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
69 fmt.write_str("\"")?;
70 write_hex_bytes(fmt, self.0)?;
71 fmt.write_str("\"")?;
72 Ok(())
73 }
74}
75
76pub(crate) fn write_hex_bytes(
77 fmt: &mut core::fmt::Formatter,
78 bytes: &[u8],
79) -> Result<(), ::core::fmt::Error> {
80 for byte in bytes {
81 write!(fmt, "{:02x}", byte)?;
82 }
83 Ok(())
84}