to_string/
lib.rs

1use std::fmt::{Debug, Binary, LowerHex, Octal, LowerExp, Pointer};
2
3pub trait BinaryDisplay {
4    /// ```
5    /// use to_string::BinaryDisplay;
6    /// assert_eq!("0b0", 0.to_binary_string())
7    /// ```
8    fn to_binary_string(&self) -> String;
9    /// ```
10    /// use to_string::BinaryDisplay;
11    /// assert_eq!("0b0", 0.to_bin())
12    /// ```
13    fn to_bin(&self) -> String { self.to_binary_string() }
14}
15
16impl<T : Binary> BinaryDisplay for T {
17    fn to_binary_string(&self) -> String {
18        format!("{:#b}", self)
19    }
20}
21
22pub trait HexaDisplay {
23    /// ```
24    /// use to_string::HexaDisplay;
25    /// assert_eq!("0x12", 0x12.to_hexa_string());
26    /// assert_eq!("0xffffffee", (-0x12).to_hexa_string())
27    /// ```
28    fn to_hexa_string(&self) -> String;
29    /// ```
30    /// use to_string::HexaDisplay;
31    /// assert_eq!("0x12", 0x12.to_hexa());
32    /// assert_eq!("0xffffffee", (-0x12).to_hexa())
33    /// ```
34    fn to_hexa(&self) -> String { self.to_hexa_string() }
35}
36
37impl<T : LowerHex> HexaDisplay for T {
38    fn to_hexa_string(&self) -> String {
39        format!("{:#x}", self)
40    }
41}
42
43pub trait OctalDisplay {
44    /// ```
45    /// use to_string::OctalDisplay;
46    /// assert_eq!("0o12", 0o12.to_octal_string())
47    /// ```
48    fn to_octal_string(&self) -> String;
49    /// ```
50    /// use to_string::OctalDisplay;
51    /// assert_eq!("0o12", 0o12.to_octal())
52    /// ```
53    fn to_octal(&self) -> String { self.to_octal_string() }
54
55}
56
57impl<T : Octal> OctalDisplay for T {
58    fn to_octal_string(&self) -> String {
59        format!("{:#o}", self)
60    }
61}
62
63pub trait ExpDisplay {
64    /// ```
65    /// use to_string::ExpDisplay;
66    /// assert_eq!("4.21e1", (42.1).to_exp_string())
67    /// ```
68    fn to_exp_string(&self) -> String;
69    /// ```
70    /// use to_string::ExpDisplay;
71    /// assert_eq!("4.21e1", (42.1).to_exp())
72    /// ```
73    fn to_exp(&self) -> String { self.to_exp_string() }
74}
75
76impl<T : LowerExp> ExpDisplay for T {
77    fn to_exp_string(&self) -> String {
78        format!("{:#e}", self)
79    }
80}
81
82pub trait PointerDisplay {
83    /// ```
84    /// use to_string::PointerDisplay;
85    /// let val = 12;
86    /// println!("{}", (&val).to_pointer()) // something like "0x00007fff5af30fa8"
87    /// ```
88    fn to_pointer_string(&self) -> String;
89    /// ```
90    /// use to_string::PointerDisplay;
91    /// let val = 12;
92    /// println!("{}", (&val).to_pointer()) // something like "0x00007fff5af30fa8"
93    /// ```
94    fn to_pointer(&self) -> String { self.to_pointer_string() }
95}
96
97impl<T : Pointer> PointerDisplay for T {
98    fn to_pointer_string(&self) -> String {
99        format!("{:#p}", self)
100    }
101}
102
103pub trait DebugDisplay {
104    /// ```
105    /// use to_string::DebugDisplay;
106    /// assert_eq!("12", 12.to_debug_string())
107    /// ```
108    fn to_debug_string(&self) -> String;
109    /// ```
110    /// use to_string::DebugDisplay;
111    /// assert_eq!("12", 12.to_debug_string())
112    /// ```
113    fn to_debug(&self) -> String { self.to_debug_string() }
114}
115
116impl<T : Debug> DebugDisplay for T {
117    fn to_debug_string(&self) -> String {
118        format!("{:#?}", self)
119    }
120}
121