paritytech / parity-common

Collection of crates used in Parity projects
https://www.paritytech.io/
Apache License 2.0
281 stars 213 forks source link

[fixed-hash] to_string() gives shortened version instead of full #656

Open mo-rijndael opened 2 years ago

mo-rijndael commented 2 years ago

How to reproduce

here is snippet

// fixed-hash is 0.7.0 (latest)
use fixed_hash::construct_fixed_hash;
construct_fixed_hash! {struct H160(20);}

fn main() {
    let hash = H160::zero();
    // Debug formatting
    println!("{:?}", hash); // 0x0000000000000000000000000000000000000000

    // Display formatting
    println!("{}", hash); // 0x0000…0000

    // ToString
    println!("{}", hash.to_string()); // 0x0000…0000
}

Expected results

to_string() returns string representation of H160

Real results

to_string() returns shortened version

Source of problem

This occurs due to default implementation of ToString in standard library: impl<T: Display + ?Sized> ToString for T

Proposed solution

swap realizations for Debug and Display

Advantages

Alternative solution

add codegen for ToString, which will return full version of hash

Advantages

hexcowboy commented 1 year ago

Also came here to argue in favor of this.

additional context: https://github.com/gakonst/ethers-rs/issues/2175

Kuly14 commented 1 year ago

add codegen for ToString, which will return full version of hash

Wouldn't the alternate solution also be a breaking change? If somebody already is using to_string() and counts that it will only give him the shortened version? Just curious.

If we could get this changed it would be great IMO.