wgtk/util/
mod.rs

1//! Provides various internal utilities.
2
3use std::fmt::{self, Write};
4
5pub mod io;
6pub mod fnv;
7
8
9/// A helper structure for beautiful printing of bytes. 
10/// It provides format implementations for upper and
11/// lower hex formatters (`{:x}`, `{:X}`).
12pub struct BytesFmt<'a>(pub &'a [u8]);
13
14impl fmt::UpperHex for BytesFmt<'_> {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        for byte in self.0 {
17            f.write_fmt(format_args!("{:02X}", byte))?;
18        }
19        Ok(())
20    }
21}
22
23impl fmt::LowerHex for BytesFmt<'_> {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        for byte in self.0 {
26            f.write_fmt(format_args!("{:02x}", byte))?;
27        }
28        Ok(())
29    }
30}
31
32
33pub struct TruncateFmt<F>(pub F, pub usize);
34
35impl<F: fmt::Display> fmt::Display for TruncateFmt<F> {
36    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37        let mut buf = String::new();
38        buf.write_fmt(format_args!("{}", self.0))?;
39        if buf.len() > self.1 {
40            buf.truncate(self.1 - 2);
41            buf.push_str("..");
42        }
43        f.write_str(&buf)
44    }
45}