paritytech / paritytech/parity-common

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

Open
#656 2 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
311
Forks
245
PR merge metrics
No merged PRs in 30d

Description

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

  • More suitable implementations of formatting traits (Shortened is useful for quick eye-checking, and when Display-ing to user we usually need full hash.)
  • Generates same amount of code, do not increase load to compiler

Disadvantages

  • It's a breaking change

Alternative solution

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

Advantages

  • Non-breaking change

Disadvantage

  • Generates more code, slowing compilation a bit
  • Leaves unsuitable implementation of formatting traits

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the construct_fixed_hash! macro and inspect the generated Debug and Display formatting implementations. Run the reproduction snippet to confirm the current shortened output and compare the compatibility implications of the proposed formatting changes. Done means deciding on an approach and ensuring to_string() returns the full H160 representation without regressing the relevant formatting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.