Skip to content

Commit

Permalink
Improve vortex-buffer Debug repr (#1901)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatesn authored Jan 10, 2025
1 parent 14c52b0 commit b5fc561
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 26 deletions.
20 changes: 7 additions & 13 deletions vortex-buffer/src/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::any::type_name;
use std::collections::Bound;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, RangeBounds};

use bytes::{Buf, Bytes};
use vortex_error::{vortex_panic, VortexExpect};

use crate::debug::TruncatedDebug;
use crate::{Alignment, BufferMut, ByteBuffer};

/// An immutable buffer of items of `T`.
Expand Down Expand Up @@ -281,21 +283,13 @@ impl<T> Buffer<T> {
}
}

impl<T> Debug for Buffer<T> {
impl<T: Debug> Debug for Buffer<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
const TRUNC_SIZE: usize = 512;
let mut binding = f.debug_struct("Buffer");
let mut fields = binding
f.debug_struct(&format!("Buffer<{}>", type_name::<T>()))
.field("length", &self.length)
.field("alignment", &self.alignment);

let mut bytes = self.bytes.clone();
if bytes.len() > TRUNC_SIZE {
fields = fields.field("truncated", &true);
}

bytes.truncate(TRUNC_SIZE);
fields.field("bytes", &bytes).finish()
.field("alignment", &self.alignment)
.field("as_slice", &TruncatedDebug(self.as_slice()))
.finish()
}
}

Expand Down
20 changes: 7 additions & 13 deletions vortex-buffer/src/buffer_mut.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use core::mem::MaybeUninit;
use std::any::type_name;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};

use bytes::buf::UninitSlice;
use bytes::{Buf, BufMut, BytesMut};
use vortex_error::{vortex_panic, VortexExpect};

use crate::debug::TruncatedDebug;
use crate::{Alignment, Buffer, ByteBufferMut};

/// A mutable buffer that maintains a runtime-defined alignment through resizing operations.
Expand Down Expand Up @@ -338,21 +340,13 @@ impl<T> Clone for BufferMut<T> {
}
}

impl<T> Debug for BufferMut<T> {
impl<T: Debug> Debug for BufferMut<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
const TRUNC_SIZE: usize = 512;
let mut binding = f.debug_struct("Buffer");
let mut fields = binding
f.debug_struct(&format!("BufferMut<{}>", type_name::<T>()))
.field("length", &self.length)
.field("alignment", &self.alignment);

let mut bytes = self.bytes.clone();
if bytes.len() > TRUNC_SIZE {
fields = fields.field("truncated", &true);
}

bytes.truncate(TRUNC_SIZE);
fields.field("bytes", &bytes).finish()
.field("alignment", &self.alignment)
.field("as_slice", &TruncatedDebug(self.as_slice()))
.finish()
}
}

Expand Down
20 changes: 20 additions & 0 deletions vortex-buffer/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use std::fmt::{Debug, Formatter};

/// A wrapper around a slice that truncates the debug output if it is too long.
pub(crate) struct TruncatedDebug<'a, T>(pub(crate) &'a [T]);

impl<T: Debug> Debug for TruncatedDebug<'_, T> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
const TRUNC_SIZE: usize = 16;
if self.0.len() <= TRUNC_SIZE {
write!(f, "{:?}", self.0)
} else {
write!(f, "[")?;
for elem in self.0.iter().take(TRUNC_SIZE) {
write!(f, "{:?}, ", *elem)?;
}
write!(f, "...")?;
write!(f, "]")
}
}
}
1 change: 1 addition & 0 deletions vortex-buffer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ mod buffer;
mod buffer_mut;
mod bytes;
mod r#const;
mod debug;
mod macros;
mod string;

Expand Down

0 comments on commit b5fc561

Please sign in to comment.