Skip to content

Commit

Permalink
read: shorten Debug output for EndianSlice
Browse files Browse the repository at this point in the history
These often contain entire sections, and it's not useful to
display everything in the section.
  • Loading branch information
philipc committed Nov 22, 2023
1 parent 531636d commit 5839e49
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/read/endian_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use alloc::borrow::Cow;
#[cfg(feature = "read")]
use alloc::string::String;
use core::fmt;
use core::ops::{Deref, Range, RangeFrom, RangeTo};
use core::str;

Expand All @@ -13,7 +14,7 @@ use crate::read::{Error, Reader, ReaderOffsetId, Result};
/// A `&[u8]` slice with endianity metadata.
///
/// This implements the `Reader` trait, which is used for all reading of DWARF sections.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Default, Clone, Copy, PartialEq, Eq, Hash)]
pub struct EndianSlice<'input, Endian>
where
Endian: Endianity,
Expand Down Expand Up @@ -177,6 +178,44 @@ where
}
}

impl<'input, Endian: Endianity> fmt::Debug for EndianSlice<'input, Endian> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
fmt.debug_tuple("EndianSlice")
.field(&self.endian)
.field(&DebugBytes(self.slice))
.finish()
}
}

struct DebugBytes<'input>(&'input [u8]);

impl<'input> core::fmt::Debug for DebugBytes<'input> {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> core::result::Result<(), fmt::Error> {
let mut list = fmt.debug_list();
list.entries(self.0.iter().take(8).copied().map(DebugByte));
if self.0.len() > 8 {
list.entry(&DebugLen(self.0.len()));
}
list.finish()
}
}

struct DebugByte(u8);

impl fmt::Debug for DebugByte {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "0x{:02x}", self.0)
}
}

struct DebugLen(usize);

impl fmt::Debug for DebugLen {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(fmt, "...; {}", self.0)
}
}

impl<'input, Endian> Reader for EndianSlice<'input, Endian>
where
Endian: Endianity,
Expand Down

0 comments on commit 5839e49

Please sign in to comment.