@@ -2,189 +2,163 @@ use core::fmt::{Display, Formatter, Result};
22
33#[ allow( missing_debug_implementations) ]
44#[ unstable( feature = "ub_checks" , issue = "none" ) ]
5- pub struct DisplayWrapper < T > ( #[ unstable( feature = "ub_checks" , issue = "none" ) ] pub T ) ;
5+ pub enum DisplayWrapper < ' a > {
6+ Bool ( bool ) ,
7+ Char ( char ) ,
8+ Str ( & ' a str ) ,
9+ Ptr ( * const ( ) ) ,
10+ Uint ( u128 ) ,
11+ Int ( i128 ) ,
12+ }
613
7- trait Displayable : Sized + Clone + Copy {
8- const IS_POINTER : bool = false ;
9- const IS_INT : bool = false ;
10- const IS_UINT : bool = false ;
11- const IS_CHAR : bool = false ;
12- const IS_STR : bool = false ;
13- #[ inline]
14- fn as_char ( self ) -> char {
15- unimplemented ! ( )
14+ impl From < bool > for DisplayWrapper < ' _ > {
15+ fn from ( b : bool ) -> Self {
16+ Self :: Bool ( b)
1617 }
17- #[ inline]
18- fn addr ( self ) -> usize {
19- unimplemented ! ( )
20- }
21- #[ inline]
22- fn as_u128 ( self ) -> u128 {
23- unimplemented ! ( )
24- }
25- #[ inline]
26- fn as_i128 ( self ) -> i128 {
27- unimplemented ! ( )
18+ }
19+
20+ impl < ' a > From < & ' a str > for DisplayWrapper < ' a > {
21+ fn from ( s : & ' a str ) -> Self {
22+ Self :: Str ( s)
2823 }
2924}
3025
31- impl Displayable for char {
32- const IS_CHAR : bool = true ;
33- #[ inline]
34- fn as_char ( self ) -> char {
35- self
26+ impl From < char > for DisplayWrapper < ' _ > {
27+ fn from ( c : char ) -> Self {
28+ Self :: Char ( c)
3629 }
3730}
3831
39- impl < T > Displayable for * const T {
40- const IS_POINTER : bool = true ;
41- #[ inline]
42- fn addr ( self ) -> usize {
43- self . addr ( )
32+ impl From < * const ( ) > for DisplayWrapper < ' _ > {
33+ fn from ( c : * const ( ) ) -> Self {
34+ Self :: Ptr ( c)
4435 }
4536}
46- impl < T > Displayable for * mut T {
47- const IS_POINTER : bool = true ;
48- #[ inline]
49- fn addr ( self ) -> usize {
50- self . addr ( )
37+ impl From < * mut ( ) > for DisplayWrapper < ' _ > {
38+ fn from ( c : * mut ( ) ) -> Self {
39+ Self :: Ptr ( c as * const ( ) )
5140 }
5241}
5342
54- impl Displayable for u8 {
55- const IS_UINT : bool = true ;
56- #[ inline]
57- fn as_u128 ( self ) -> u128 {
58- self as u128
43+ impl From < u8 > for DisplayWrapper < ' _ > {
44+ fn from ( c : u8 ) -> Self {
45+ Self :: Uint ( c as u128 )
5946 }
6047}
61- impl Displayable for u32 {
62- const IS_UINT : bool = true ;
63- #[ inline]
64- fn as_u128 ( self ) -> u128 {
65- self as u128
48+ impl From < u16 > for DisplayWrapper < ' _ > {
49+ fn from ( c : u16 ) -> Self {
50+ Self :: Uint ( c as u128 )
6651 }
6752}
68- impl Displayable for u64 {
69- const IS_UINT : bool = true ;
70- #[ inline]
71- fn as_u128 ( self ) -> u128 {
72- self as u128
53+ impl From < u32 > for DisplayWrapper < ' _ > {
54+ fn from ( c : u32 ) -> Self {
55+ Self :: Uint ( c as u128 )
7356 }
7457}
75- impl Displayable for usize {
76- const IS_UINT : bool = true ;
77- #[ inline]
78- fn as_u128 ( self ) -> u128 {
79- self as u128
58+ impl From < u64 > for DisplayWrapper < ' _ > {
59+ fn from ( c : u64 ) -> Self {
60+ Self :: Uint ( c as u128 )
8061 }
8162}
82- impl Displayable for u128 {
83- const IS_UINT : bool = true ;
84- #[ inline]
85- fn as_u128 ( self ) -> u128 {
86- self
63+ impl From < u128 > for DisplayWrapper < ' _ > {
64+ fn from ( c : u128 ) -> Self {
65+ Self :: Uint ( c as u128 )
66+ }
67+ }
68+ impl From < usize > for DisplayWrapper < ' _ > {
69+ fn from ( c : usize ) -> Self {
70+ Self :: Uint ( c as u128 )
8771 }
8872}
8973
90- impl Displayable for isize {
91- const IS_INT : bool = true ;
92- #[ inline]
93- fn as_i128 ( self ) -> i128 {
94- self as i128
74+ impl From < i8 > for DisplayWrapper < ' _ > {
75+ fn from ( c : i8 ) -> Self {
76+ Self :: Int ( c as i128 )
9577 }
9678}
97- impl Displayable for i8 {
98- const IS_INT : bool = true ;
99- #[ inline]
100- fn as_i128 ( self ) -> i128 {
101- self as i128
79+ impl From < i16 > for DisplayWrapper < ' _ > {
80+ fn from ( c : i16 ) -> Self {
81+ Self :: Int ( c as i128 )
10282 }
10383}
104- impl Displayable for i16 {
105- const IS_INT : bool = true ;
106- #[ inline]
107- fn as_i128 ( self ) -> i128 {
108- self as i128
84+ impl From < i32 > for DisplayWrapper < ' _ > {
85+ fn from ( c : i32 ) -> Self {
86+ Self :: Int ( c as i128 )
10987 }
11088}
111- impl Displayable for i32 {
112- const IS_INT : bool = true ;
113- #[ inline]
114- fn as_i128 ( self ) -> i128 {
115- self as i128
89+ impl From < i64 > for DisplayWrapper < ' _ > {
90+ fn from ( c : i64 ) -> Self {
91+ Self :: Int ( c as i128 )
11692 }
11793}
118- impl Displayable for i64 {
119- const IS_INT : bool = true ;
120- #[ inline]
121- fn as_i128 ( self ) -> i128 {
122- self as i128
94+ impl From < i128 > for DisplayWrapper < ' _ > {
95+ fn from ( c : i128 ) -> Self {
96+ Self :: Int ( c as i128 )
12397 }
12498}
125- impl Displayable for i128 {
126- const IS_INT : bool = true ;
127- #[ inline]
128- fn as_i128 ( self ) -> i128 {
129- self
99+ impl From < isize > for DisplayWrapper < ' _ > {
100+ fn from ( c : isize ) -> Self {
101+ Self :: Int ( c as i128 )
130102 }
131103}
132104
133105#[ unstable( feature = "ub_checks" , issue = "none" ) ]
134- impl < T : Displayable > Display for DisplayWrapper < T > {
106+ impl Display for DisplayWrapper < ' _ > {
135107 #[ inline]
136108 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
137109 const HEX : [ u8 ; 16 ] = * b"0123456789abcdef" ;
138- assert ! ( T :: IS_POINTER ^ T :: IS_UINT ^ T :: IS_INT ^ T :: IS_CHAR ) ;
139- if T :: IS_CHAR {
140- let mut buf = [ 0u8 ; 4 ] ;
141- let s = self . 0 . as_char ( ) . encode_utf8 ( & mut buf) ;
142- return f. write_str ( s) ;
143- }
144- /*
145- if T::IS_STR {
146- return f.write_str(self.0.as_str());
147- }
148- */
149-
150110 let mut buf = [ 0u8 ; 42 ] ;
151111 let mut cur = buf. len ( ) ;
152112
153- if T :: IS_POINTER {
154- let mut n = self . 0 . addr ( ) ;
155- while n >= 16 {
156- let d = n % 16 ;
157- n /= 16 ;
158- cur -= 1 ;
159- buf[ cur] = HEX [ d] ;
113+ match * self {
114+ Self :: Bool ( _) | Self :: Str ( _) => panic ! ( ) ,
115+ Self :: Char ( c) => {
116+ let mut buf = [ 0u8 ; 4 ] ;
117+ let s = c. encode_utf8 ( & mut buf) ;
118+ return f. write_str ( s) ;
160119 }
161- cur -= 1 ;
162- buf[ cur] = HEX [ n] ;
120+ Self :: Ptr ( ptr) => {
121+ let mut n = ptr. addr ( ) ;
122+ while n >= 16 {
123+ let d = n % 16 ;
124+ n /= 16 ;
125+ cur -= 1 ;
126+ buf[ cur] = HEX [ d] ;
127+ }
128+ cur -= 1 ;
129+ buf[ cur] = HEX [ n] ;
163130
164- cur -= 1 ;
165- buf[ cur] = b'x' ;
166- cur -= 1 ;
167- buf[ cur] = b'0' ;
168- } else {
169- let mut is_negative = false ;
170- let mut n = if T :: IS_INT {
171- let signed = self . 0 . as_i128 ( ) ;
172- is_negative = signed < 0 ;
173- ( !( signed as u128 ) ) . wrapping_add ( 1 )
174- } else {
175- self . 0 . as_u128 ( )
176- } ;
177- while n >= 10 {
178- let d = n % 10 ;
179- n /= 10 ;
180131 cur -= 1 ;
181- buf[ cur] = ( d as u8 ) + b'0' ;
132+ buf[ cur] = b'x' ;
133+ cur -= 1 ;
134+ buf[ cur] = b'0' ;
135+ }
136+ Self :: Uint ( mut n) => {
137+ while n >= 10 {
138+ let d = n % 10 ;
139+ n /= 10 ;
140+ cur -= 1 ;
141+ buf[ cur] = ( d as u8 ) + b'0' ;
142+ }
143+ cur -= 1 ;
144+ buf[ cur] = ( n as u8 ) + b'0' ;
182145 }
183- cur -= 1 ;
184- buf[ cur] = ( n as u8 ) + b'0' ;
185- if is_negative {
146+ Self :: Int ( n) => {
147+ let is_negative = n < 0 ;
148+ let mut n = ( !( n as u128 ) ) . wrapping_add ( 1 ) ;
149+
150+ while n >= 10 {
151+ let d = n % 10 ;
152+ n /= 10 ;
153+ cur -= 1 ;
154+ buf[ cur] = ( d as u8 ) + b'0' ;
155+ }
186156 cur -= 1 ;
187- buf[ cur] = b'-' ;
157+ buf[ cur] = ( n as u8 ) + b'0' ;
158+ if is_negative {
159+ cur -= 1 ;
160+ buf[ cur] = b'-' ;
161+ }
188162 }
189163 }
190164 // SAFETY: The buffer is initially ASCII and we only write ASCII bytes to it.
0 commit comments