solana_account_info/
debug_account_data.rs

1//! Debug-formatting of account data.
2
3use std::{cmp, fmt};
4
5pub(crate) const MAX_DEBUG_ACCOUNT_DATA: usize = 64;
6
7/// Format data as hex.
8///
9/// If `data`'s length is greater than 0, add a field called "data" to `f`. The
10/// first 64 bytes of `data` is displayed; bytes after that are ignored.
11pub fn debug_account_data(data: &[u8], f: &mut fmt::DebugStruct<'_, '_>) {
12    let data_len = cmp::min(MAX_DEBUG_ACCOUNT_DATA, data.len());
13    if data_len > 0 {
14        f.field("data", &Hex(&data[..data_len]));
15    }
16}
17
18pub(crate) struct Hex<'a>(pub(crate) &'a [u8]);
19impl fmt::Debug for Hex<'_> {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        for &byte in self.0 {
22            write!(f, "{byte:02x}")?;
23        }
24        Ok(())
25    }
26}