split / cleanup core utils.
Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
5fe5ab279c
commit
90d9a997a5
13 changed files with 205 additions and 191 deletions
40
src/core/utils/debug.rs
Normal file
40
src/core/utils/debug.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use std::fmt;
|
||||
|
||||
/// Debug-formats the given slice, but only up to the first `max_len` elements.
|
||||
/// Any further elements are replaced by an ellipsis.
|
||||
///
|
||||
/// See also [`slice_truncated()`],
|
||||
pub struct TruncatedSlice<'a, T> {
|
||||
inner: &'a [T],
|
||||
max_len: usize,
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for TruncatedSlice<'_, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
if self.inner.len() <= self.max_len {
|
||||
write!(f, "{:?}", self.inner)
|
||||
} else {
|
||||
f.debug_list()
|
||||
.entries(&self.inner[..self.max_len])
|
||||
.entry(&"...")
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// See [`TruncatedSlice`]. Useful for `#[instrument]`:
|
||||
///
|
||||
/// ```
|
||||
/// use conduit_core::utils::debug::slice_truncated;
|
||||
///
|
||||
/// #[tracing::instrument(fields(foos = slice_truncated(foos, 42)))]
|
||||
/// fn bar(foos: &[&str]);
|
||||
/// ```
|
||||
pub fn slice_truncated<T: fmt::Debug>(
|
||||
slice: &[T], max_len: usize,
|
||||
) -> tracing::field::DebugValue<TruncatedSlice<'_, T>> {
|
||||
tracing::field::debug(TruncatedSlice {
|
||||
inner: slice,
|
||||
max_len,
|
||||
})
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue