Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a TypeNameToDebug formatter to zebra_chain #2466

Merged
merged 2 commits into from
Jul 9, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 90 additions & 11 deletions zebra-chain/src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,68 @@

use std::{fmt, ops};

#[cfg(any(test, feature = "proptest-impl"))]
use proptest::prelude::*;
#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;

/// Wrapper to override `Debug`, redirecting it to the type's name.
teor2345 marked this conversation as resolved.
Show resolved Hide resolved
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct TypeNameToDebug<T>(pub T);

impl<T> fmt::Debug for TypeNameToDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(std::any::type_name::<T>())
}
}

impl<T> ops::Deref for TypeNameToDebug<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> ops::DerefMut for TypeNameToDebug<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T> From<T> for TypeNameToDebug<T> {
fn from(t: T) -> Self {
Self(t)
}
}

/// Wrapper to override `Debug`, redirecting it to the `Display` impl.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DisplayToDebug<T>(pub T);
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
pub struct DisplayToDebug<T: fmt::Display>(pub T);

impl<T> fmt::Debug for DisplayToDebug<T>
where
T: fmt::Display,
{
impl<T: fmt::Display> fmt::Debug for DisplayToDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}

impl<T> ops::Deref for DisplayToDebug<T> {
impl<T: fmt::Display> ops::Deref for DisplayToDebug<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> From<T> for DisplayToDebug<T> {
impl<T: fmt::Display> ops::DerefMut for DisplayToDebug<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<T: fmt::Display> From<T> for DisplayToDebug<T> {
fn from(t: T) -> Self {
Self(t)
}
Expand All @@ -34,7 +74,10 @@ impl<T> From<T> for DisplayToDebug<T> {
/// 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);
pub struct SummaryDebug<CollectionOrIter>(pub CollectionOrIter)
where
CollectionOrIter: IntoIterator + Clone,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator;

impl<CollectionOrIter> fmt::Debug for SummaryDebug<CollectionOrIter>
where
Expand All @@ -52,23 +95,42 @@ where
}
}

impl<CollectionOrIter> ops::Deref for SummaryDebug<CollectionOrIter> {
impl<CollectionOrIter> ops::Deref for SummaryDebug<CollectionOrIter>
where
CollectionOrIter: IntoIterator + Clone,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
type Target = CollectionOrIter;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<CollectionOrIter> From<CollectionOrIter> for SummaryDebug<CollectionOrIter> {
impl<CollectionOrIter> ops::DerefMut for SummaryDebug<CollectionOrIter>
where
CollectionOrIter: IntoIterator + Clone,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<CollectionOrIter> From<CollectionOrIter> for SummaryDebug<CollectionOrIter>
where
CollectionOrIter: IntoIterator + Clone,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
fn from(collection: CollectionOrIter) -> Self {
Self(collection)
}
}

impl<CollectionOrIter> IntoIterator for SummaryDebug<CollectionOrIter>
where
CollectionOrIter: IntoIterator,
CollectionOrIter: IntoIterator + Clone,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
type Item = <CollectionOrIter as IntoIterator>::Item;
type IntoIter = <CollectionOrIter as IntoIterator>::IntoIter;
Expand All @@ -77,3 +139,20 @@ where
self.0.into_iter()
}
}

#[cfg(any(test, feature = "proptest-impl"))]
impl<CollectionOrIter> Arbitrary for SummaryDebug<CollectionOrIter>
where
CollectionOrIter: Arbitrary + IntoIterator + Clone + 'static,
<CollectionOrIter as IntoIterator>::IntoIter: ExactSizeIterator,
{
type Parameters = <CollectionOrIter as Arbitrary>::Parameters;

fn arbitrary_with(args: Self::Parameters) -> Self::Strategy {
CollectionOrIter::arbitrary_with(args)
.prop_map_into()
.boxed()
}

type Strategy = BoxedStrategy<Self>;
}