rust-bitcoin / rust-bech32

Bech32 format encoding and decoding
MIT License
93 stars 49 forks source link

`Hrp` should have a constructor from a `T: Display` #195

Closed apoelstra closed 4 weeks ago

apoelstra commented 1 month ago

This would allow easy construction from a rust-lightning RawHrp. Currently it allocates a String which is an unnecessary allocation and inconvenient.

tcharding commented 1 month ago

How would such a constructor get at the string (bytes) created by the Display implementation without allocating a string?

We could add convenience constructors but I don't really see the value, what am I missunderstanding?

    /// Constructs a `Hrp` for an `hrp` string created by calling `hrp.to_string()`. 
    pub fn from_display(hrp: impl fmt::Display) -> Result<Self, Error> {
        let s = hrp.to_string();
        Self::parse(&s)
    }

    /// Constructs a `Hrp` for an `hrp` string created by calling `hrp.to_string()`. 
    pub fn from_display_unchecked(hrp: impl fmt::Display) -> Self {
        let s = hrp.to_string();
        Self::parse_unchecked(&s)
    }
apoelstra commented 1 month ago

By implementing a fmt::Write backed by an array. I've almost finished this, I'll try to PR shortly.

clarkmoody commented 1 month ago

Concept ACK, and cool implementation in #196