parity_db/
display.rs

1// Copyright 2021-2022 Parity Technologies (UK) Ltd.
2// This file is dual-licensed as Apache-2.0 or MIT.
3
4/// Simple wrapper to display hex representation of bytes.
5pub struct HexDisplay<'a>(&'a [u8]);
6
7impl<'a> HexDisplay<'a> {
8	pub fn from<R: AsRef<[u8]> + ?Sized>(d: &'a R) -> Self {
9		HexDisplay(d.as_ref())
10	}
11}
12
13impl<'a> std::fmt::Display for HexDisplay<'a> {
14	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15		for byte in self.0 {
16			write!(f, "{byte:02x}")?;
17		}
18		Ok(())
19	}
20}
21
22impl<'a> std::fmt::Debug for HexDisplay<'a> {
23	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
24		for byte in self.0 {
25			write!(f, "{byte:02x}")?;
26		}
27		Ok(())
28	}
29}
30
31pub fn hex<R: AsRef<[u8]> + ?Sized>(r: &R) -> HexDisplay<'_> {
32	HexDisplay::from(r)
33}