Skip to content

Commit

Permalink
fix(primitives): custom Commitment Debug implementation (#758)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmg-duarte authored Feb 17, 2025
1 parent d8d3a6e commit fa3873a
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion primitives/src/commitment/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ pub mod commd;
pub mod piece;
mod zero;

extern crate alloc;

use core::{fmt::Display, marker::PhantomData};

use cid::{multihash::Multihash, Cid};
Expand Down Expand Up @@ -108,7 +110,7 @@ impl core::fmt::Debug for CommitmentError {
}

#[cfg_attr(feature = "serde", derive(::serde::Deserialize, ::serde::Serialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Commitment<Kind>
where
Kind: CommitmentKind,
Expand Down Expand Up @@ -165,6 +167,27 @@ where
}
}

impl<Kind> core::fmt::Debug for Commitment<Kind>
where
Kind: CommitmentKind,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use alloc::{format, string::String};

let mut hex = String::with_capacity(2 + &self.raw.len() * 2);
hex.push_str("0x");
f.debug_struct(&format!("Commitment<{}>", core::any::type_name::<Kind>()))
.field(
"raw",
&self.raw.iter().fold(hex, |mut acc, b| {
acc.push_str(&format!("{:0x}", b));
acc
}),
)
.finish()
}
}

impl<Kind> Display for Commitment<Kind>
where
Kind: CommitmentKind,
Expand Down

0 comments on commit fa3873a

Please sign in to comment.