rust-bakery / nom

Rust parser combinator framework
MIT License
9.47k stars 806 forks source link

dbg_dmp!() doesn't work for &str #613

Open vadixidav opened 6 years ago

vadixidav commented 6 years ago

Currently dbg_dmp!() only works for &[u8]. It could easily be adapted to work for strings.

I recommend using str::as_bytes() to get the bytes and hex dump the bytes of the string in the HexDisplay trait impl.

vadixidav commented 6 years ago

I do this in my code currently by abusing the fact that the macros don't trait bound:

// Not using HexDisplay in this scope.
trait DbgFakeHex {
    fn to_hex(&self, chunk_size: usize) -> String;
}

impl DbgFakeHex for str {
    fn to_hex(&self, chunk_size: usize) -> String {
        use nom::HexDisplay;
        self.as_bytes().to_hex(chunk_size)
    }
}
stephanemagnenat commented 3 years ago

This would still be useful...