Introduce Char enum and rewrite BytesToHexIter to return it#236
Introduce Char enum and rewrite BytesToHexIter to return it#236apoelstra merged 3 commits intorust-bitcoin:masterfrom
Char enum and rewrite BytesToHexIter to return it#236Conversation
|
It should be noted that I didn't introduce any of these traits for // These are trivially correct `unsafe` casts
impl AsRef<str> for [Char] { /* ... */ }
impl<const N: usize> AsRef<str> for [Char; N] { /* ... */ }
impl AsRef<[u8]> for [Char] { /* ... */ }
impl<const N: usize> AsRef<[u8]> for [Char; N] { /* ... */ }
impl<const N: usize> AsRef<[u8; N]> for [Char; N] { /* ... */ }
// Borrows and mutable casts just as AsRef
impl<const N: usize> From<[Char; N]> for [u8; N] { /* ... */ }These seem to have problems with orphan rules, since |
|
Frustrating. I think you're correct about the orphan rules. |
We could add a |
IMO this is too much abstraction. It will be harder for users to fight their way out of our types to get real data than it would be for them to just have to manually pull apart arrays. I also think it's "obvious" that the first nybble is the high one and the second one is the low one. I don't think I've ever seen a byte vector hex-encoded any other way. |
|
aef87f4 needs rebase |
|
Works for me! |
The set of valid hex characters is highly limited and can easily be encoded into an enum with a u8 representation. In doing so, we can enforce the set of characters in the encoding table by the type system, rather than relying on u8 directly. This new Char type can also be used as a more expressive return type in place of u8 throughout the crate. Introduce Char enum and convert Table to be defined using it instead of u8.
Currently, BytesToHexIter takes in u8 and returns char. As such, it uses an internal buffer to facilitate the 1 -> 2 ratio of input to output. With the introduction of the Char type, we can now use Char in place of char to improve type safety. Further, by returning pairs of Chars, we can eliminate bugs associated with the internal buffer and allow users to trivially control nibble order during encoding. Replace char iterator item type on BytesToHexIter with [Char; 2]. Update benchmarks as necessary.
aef87f4 to
becc2de
Compare
Currently, BytesToHexIter takes in u8 and returns char. By introducting an enum Char type, we can use that in place of char to improve type safety. Further, by returning pairs of Chars, we can eliminate bugs associated with the internal buffer and allow users to trivially control character/nibble order during encoding.
Closes #234