KokaKiwi / rust-hex

A basic crate to encode values to hexadecimal representation. Originally extracted from rustc-serialize.
https://crates.io/crates/hex
Apache License 2.0
201 stars 55 forks source link

Implement fmt::LowerHex and fmt::UpperHex #17

Closed cchudant closed 5 years ago

cchudant commented 5 years ago

https://doc.rust-lang.org/nightly/core/fmt/trait.LowerHex.html https://doc.rust-lang.org/nightly/core/fmt/trait.UpperHex.html

it would be great to directly display u8 slices to the console like this:

let data: &[u8] = ...;
println!("{:x}", data);

because we currently have to do this:

let mut s = String::new();
data.write_hex(&mut s).unwrap();
println!("{}", s);

which isn't great

LukasKalbertodt commented 5 years ago

Hi there! :)

Unfortunately, what you suggest is not possible. We can't add an impl UpperHex for &[u8] in this crate due to so called orphan rules. I.e.: either the trait or the type of an impl has to be defined in the same crate as the impl itself.

To work around this, one could have a newtype which wraps a slice. This is what is suggested in #8. So I think this issue can be closed in favor of #8.

cchudant commented 5 years ago

Oh ok, thanks