Skip to content

Commit 8a0b3de

Browse files
authored
Merge pull request #111 from alexanderwiederin/display-super
Refactor(core): Add Display as a supertrait of `BlockHashExt` and `TxidExt`
2 parents fee5a6a + c1de510 commit 8a0b3de

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/core/block.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,11 @@
102102
//! - [`TransactionSpentOutputsIter`] - Iterates over coins spent by a transaction
103103
//!
104104
105-
use std::{ffi::c_void, marker::PhantomData};
105+
use std::{
106+
ffi::c_void,
107+
fmt::{self, Debug, Display, Formatter},
108+
marker::PhantomData,
109+
};
106110

107111
use libbitcoinkernel_sys::{
108112
btck_Block, btck_BlockHash, btck_BlockSpentOutputs, btck_Coin, btck_TransactionSpentOutputs,
@@ -146,7 +150,7 @@ use super::transaction::{TransactionRef, TxOutRef};
146150
/// let hash = BlockHash::from([1u8; 32]);
147151
/// display_hash(&hash);
148152
/// ```
149-
pub trait BlockHashExt: AsPtr<btck_BlockHash> {
153+
pub trait BlockHashExt: AsPtr<btck_BlockHash> + Display {
150154
/// Serializes the block hash to raw bytes.
151155
///
152156
/// Returns the 32-byte representation of the block hash in internal byte order.
@@ -322,14 +326,14 @@ impl PartialEq for BlockHash {
322326

323327
impl Eq for BlockHash {}
324328

325-
impl std::fmt::Debug for BlockHash {
326-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
329+
impl Debug for BlockHash {
330+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> fmt::Result {
327331
write!(f, "BlockHash({:?})", self.to_bytes())
328332
}
329333
}
330334

331-
impl std::fmt::Display for BlockHash {
332-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
335+
impl Display for BlockHash {
336+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
333337
let bytes = self.to_bytes();
334338
for byte in bytes.iter().rev() {
335339
write!(f, "{:02x}", byte)?;
@@ -396,14 +400,14 @@ impl<'a> PartialEq for BlockHashRef<'a> {
396400
}
397401
}
398402

399-
impl<'a> std::fmt::Debug for BlockHashRef<'a> {
400-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
403+
impl<'a> Debug for BlockHashRef<'a> {
404+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
401405
write!(f, "BlockHash({:?})", self.to_bytes())
402406
}
403407
}
404408

405-
impl<'a> std::fmt::Display for BlockHashRef<'a> {
406-
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
409+
impl<'a> Display for BlockHashRef<'a> {
410+
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
407411
let bytes = self.to_bytes();
408412
for byte in bytes.iter().rev() {
409413
write!(f, "{:02x}", byte)?;

src/core/transaction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl<'a> Clone for TxOutPointRef<'a> {
651651
impl<'a> Copy for TxOutPointRef<'a> {}
652652

653653
/// Common operations for transaction IDs, implemented by both owned and borrowed types.
654-
pub trait TxidExt: AsPtr<btck_Txid> {
654+
pub trait TxidExt: AsPtr<btck_Txid> + Display {
655655
/// Serializes the txid to raw bytes.
656656
fn to_bytes(&self) -> [u8; 32] {
657657
let mut bytes = [0u8; 32];

0 commit comments

Comments
 (0)