1 // Copyright 2018 Trent Clarke.
2 //
3 // Permission to use, copy, modify, and/or distribute this software for any
4 // purpose with or without fee is hereby granted, provided that the above
5 // copyright notice and this permission notice appear in all copies.
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
8 // WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 // MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
10 // SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 // WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 // OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 // CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14
15 // Generates an implementation of the Debug trait for a type that defers to the
16 // Debug implementation for a given field.
17 macro_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
27 macro_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
43 // Generates an implementation of the Debug trait for a type that outputs the
44 // hex encoding of the byte slice representation of the value.
45 macro_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
write_hex_tuple( fmt: &mut core::fmt::Formatter, type_name: &str, value: &dyn AsRef<[u8]>, ) -> Result<(), ::core::fmt::Error>55 pub(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
65 pub struct HexStr<'a>(pub &'a [u8]);
66
67 impl core::fmt::Debug for HexStr<'_> {
fmt(&self, fmt: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error>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
write_hex_bytes( fmt: &mut core::fmt::Formatter, bytes: &[u8], ) -> Result<(), ::core::fmt::Error>76 pub(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 }
85