cuprate_helper/
fmt.rs

1//! String formatting.
2
3/// A type that can be represented in hexadecimal (with a `0x` prefix).
4pub trait HexPrefix {
5    /// Turn `self` into a hexadecimal string prefixed with `0x`.
6    fn hex_prefix(self) -> String;
7}
8
9macro_rules! impl_hex_prefix {
10    ($(
11        $t:ty
12    ),*) => {
13        $(
14            impl HexPrefix for $t {
15                fn hex_prefix(self) -> String {
16                    format!("{:#x}", self)
17                }
18            }
19        )*
20    };
21}
22
23impl_hex_prefix!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, isize);
24
25impl HexPrefix for (u64, u64) {
26    /// Combine the low and high bits of a [`u128`] as a lower-case hexadecimal string prefixed with `0x`.
27    ///
28    /// ```rust
29    /// # use cuprate_helper::fmt::HexPrefix;
30    /// assert_eq!((0, 0).hex_prefix(), "0x0");
31    /// assert_eq!((0, u64::MAX).hex_prefix(), "0xffffffffffffffff0000000000000000");
32    /// assert_eq!((u64::MAX, 0).hex_prefix(), "0xffffffffffffffff");
33    /// assert_eq!((u64::MAX, u64::MAX).hex_prefix(), "0xffffffffffffffffffffffffffffffff");
34    /// ```
35    fn hex_prefix(self) -> String {
36        format!(
37            "{:#x}",
38            crate::map::combine_low_high_bits_to_u128(self.0, self.1)
39        )
40    }
41}
42
43#[cfg(test)]
44mod tests {}