2020-11-30 13:30:37 -08:00
|
|
|
//! Format wrappers for Zebra
|
|
|
|
|
2021-06-02 06:18:04 -07:00
|
|
|
use std::{fmt, ops};
|
2020-11-30 13:30:37 -08:00
|
|
|
|
2021-06-02 06:18:04 -07:00
|
|
|
/// Wrapper to override `Debug`, redirecting it to the `Display` impl.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2020-11-30 13:30:37 -08:00
|
|
|
pub struct DisplayToDebug<T>(pub T);
|
|
|
|
|
|
|
|
impl<T> fmt::Debug for DisplayToDebug<T>
|
|
|
|
where
|
|
|
|
T: fmt::Display,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
self.0.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:18:04 -07:00
|
|
|
impl<T> ops::Deref for DisplayToDebug<T> {
|
|
|
|
type Target = T;
|
2020-11-30 13:30:37 -08:00
|
|
|
|
2021-06-02 06:18:04 -07:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
2020-11-30 13:30:37 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:18:04 -07:00
|
|
|
impl<T> From<T> for DisplayToDebug<T> {
|
|
|
|
fn from(t: T) -> Self {
|
|
|
|
Self(t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Wrapper to override `Debug` to display a shorter summary of the type.
|
|
|
|
///
|
|
|
|
/// For collections and exact size iterators, it only displays the
|
|
|
|
/// collection/iterator type, the item type, and the length.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct SummaryDebug<CollectionOrIter>(pub CollectionOrIter);
|
|
|
|
|
|
|
|
impl<CollectionOrIter> fmt::Debug for SummaryDebug<CollectionOrIter>
|
|
|
|
where
|
|
|
|
CollectionOrIter: IntoIterator + Clone,
|
|
|
|
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
|
|
|
|
{
|
2020-11-30 13:30:37 -08:00
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-06-02 06:18:04 -07:00
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}<{}>, len={}",
|
|
|
|
std::any::type_name::<CollectionOrIter>(),
|
|
|
|
std::any::type_name::<<CollectionOrIter as IntoIterator>::Item>(),
|
|
|
|
self.0.clone().into_iter().len()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CollectionOrIter> ops::Deref for SummaryDebug<CollectionOrIter> {
|
|
|
|
type Target = CollectionOrIter;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CollectionOrIter> From<CollectionOrIter> for SummaryDebug<CollectionOrIter> {
|
|
|
|
fn from(collection: CollectionOrIter) -> Self {
|
|
|
|
Self(collection)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<CollectionOrIter> IntoIterator for SummaryDebug<CollectionOrIter>
|
|
|
|
where
|
|
|
|
CollectionOrIter: IntoIterator,
|
|
|
|
{
|
|
|
|
type Item = <CollectionOrIter as IntoIterator>::Item;
|
|
|
|
type IntoIter = <CollectionOrIter as IntoIterator>::IntoIter;
|
|
|
|
|
|
|
|
fn into_iter(self) -> Self::IntoIter {
|
|
|
|
self.0.into_iter()
|
2020-11-30 13:30:37 -08:00
|
|
|
}
|
|
|
|
}
|