aws_lc_rs/
debug.rs

1// Copyright 2018 Trent Clarke.
2// SPDX-License-Identifier: ISC
3// Modifications copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
4// SPDX-License-Identifier: Apache-2.0 OR ISC
5
6// Generates an implementation of the Debug trait for a type that defers to the
7// Debug implementation for a given field.
8
9#![allow(missing_docs)]
10
11macro_rules! derive_debug_via_id {
12    ($typename:ident) => {
13        impl ::core::fmt::Debug for $typename {
14            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
15                ::core::fmt::Debug::fmt(&self.id, f)
16            }
17        }
18    };
19}
20pub(crate) use derive_debug_via_id;
21
22#[allow(unused_macros)]
23macro_rules! derive_debug_via_field {
24    ($type:ty, $field:ident) => {
25        derive_debug_via_field!($type, stringify!($type), $field);
26    };
27
28    ($type:ty, $typename:expr, $field:ident) => {
29        impl ::core::fmt::Debug for $type {
30            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
31                f.debug_struct($typename)
32                    .field(stringify!($field), &self.$field)
33                    .finish()
34            }
35        }
36    };
37}
38
39// Generates an implementation of the Debug trait for a type that outputs the
40// hex encoding of the byte slice representation of the value.
41#[allow(unused_macros)]
42macro_rules! derive_debug_self_as_ref_hex_bytes {
43    ($typename:ident) => {
44        impl ::core::fmt::Debug for $typename {
45            fn fmt(&self, f: &mut ::core::fmt::Formatter) -> Result<(), ::core::fmt::Error> {
46                crate::debug::write_hex_tuple(f, stringify!($typename), self)
47            }
48        }
49    };
50}
51
52pub(crate) fn write_hex_bytes(
53    fmt: &mut core::fmt::Formatter,
54    bytes: &[u8],
55) -> Result<(), core::fmt::Error> {
56    for byte in bytes {
57        write!(fmt, "{byte:02x}")?;
58    }
59    Ok(())
60}