@@ -5,8 +5,15 @@ use core::fmt::{Display, Formatter, Result};
55pub struct DisplayWrapper < T > ( #[ unstable( feature = "ub_checks" , issue = "none" ) ] pub T ) ;
66
77trait Displayable : Sized + Clone + Copy {
8- const IS_POINTER : bool ;
9- const SIGNED : bool ;
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 ! ( )
16+ }
1017 #[ inline]
1118 fn addr ( self ) -> usize {
1219 unimplemented ! ( )
@@ -21,86 +28,128 @@ trait Displayable: Sized + Clone + Copy {
2128 }
2229}
2330
31+ impl Displayable for char {
32+ const IS_CHAR : bool = true ;
33+ #[ inline]
34+ fn as_char ( self ) -> char {
35+ self
36+ }
37+ }
38+
2439impl < T > Displayable for * const T {
2540 const IS_POINTER : bool = true ;
26- const SIGNED : bool = false ;
2741 #[ inline]
2842 fn addr ( self ) -> usize {
2943 self . addr ( )
3044 }
3145}
3246impl < T > Displayable for * mut T {
3347 const IS_POINTER : bool = true ;
34- const SIGNED : bool = false ;
3548 #[ inline]
3649 fn addr ( self ) -> usize {
3750 self . addr ( )
3851 }
3952}
53+
4054impl Displayable for u8 {
41- const IS_POINTER : bool = false ;
42- const SIGNED : bool = false ;
55+ const IS_UINT : bool = true ;
4356 #[ inline]
4457 fn as_u128 ( self ) -> u128 {
4558 self as u128
4659 }
4760}
4861impl Displayable for u32 {
49- const IS_POINTER : bool = false ;
50- const SIGNED : bool = false ;
62+ const IS_UINT : bool = true ;
5163 #[ inline]
5264 fn as_u128 ( self ) -> u128 {
5365 self as u128
5466 }
5567}
5668impl Displayable for u64 {
57- const IS_POINTER : bool = false ;
58- const SIGNED : bool = false ;
69+ const IS_UINT : bool = true ;
5970 #[ inline]
6071 fn as_u128 ( self ) -> u128 {
6172 self as u128
6273 }
6374}
6475impl Displayable for usize {
65- const IS_POINTER : bool = false ;
66- const SIGNED : bool = false ;
76+ const IS_UINT : bool = true ;
6777 #[ inline]
6878 fn as_u128 ( self ) -> u128 {
6979 self as u128
7080 }
7181}
7282impl Displayable for u128 {
73- const IS_POINTER : bool = false ;
74- const SIGNED : bool = false ;
83+ const IS_UINT : bool = true ;
7584 #[ inline]
7685 fn as_u128 ( self ) -> u128 {
7786 self
7887 }
7988}
8089
8190impl Displayable for isize {
82- const IS_POINTER : bool = false ;
83- const SIGNED : bool = true ;
91+ const IS_INT : bool = true ;
92+ #[ inline]
93+ fn as_i128 ( self ) -> i128 {
94+ self as i128
95+ }
96+ }
97+ impl Displayable for i8 {
98+ const IS_INT : bool = true ;
99+ #[ inline]
100+ fn as_i128 ( self ) -> i128 {
101+ self as i128
102+ }
103+ }
104+ impl Displayable for i16 {
105+ const IS_INT : bool = true ;
106+ #[ inline]
107+ fn as_i128 ( self ) -> i128 {
108+ self as i128
109+ }
110+ }
111+ impl Displayable for i32 {
112+ const IS_INT : bool = true ;
113+ #[ inline]
114+ fn as_i128 ( self ) -> i128 {
115+ self as i128
116+ }
117+ }
118+ impl Displayable for i64 {
119+ const IS_INT : bool = true ;
84120 #[ inline]
85121 fn as_i128 ( self ) -> i128 {
86122 self as i128
87123 }
88124}
89125impl Displayable for i128 {
90- const IS_POINTER : bool = false ;
91- const SIGNED : bool = true ;
126+ const IS_INT : bool = true ;
92127 #[ inline]
93128 fn as_i128 ( self ) -> i128 {
94129 self
95130 }
96131}
132+
97133#[ unstable( feature = "ub_checks" , issue = "none" ) ]
98134impl < T : Displayable > Display for DisplayWrapper < T > {
99135 #[ inline]
100136 fn fmt ( & self , f : & mut Formatter < ' _ > ) -> Result {
101137 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+
102150 let mut buf = [ 0u8 ; 42 ] ;
103151 let mut cur = buf. len ( ) ;
152+
104153 if T :: IS_POINTER {
105154 let mut n = self . 0 . addr ( ) ;
106155 while n >= 16 {
@@ -118,7 +167,7 @@ impl<T: Displayable> Display for DisplayWrapper<T> {
118167 buf[ cur] = b'0' ;
119168 } else {
120169 let mut is_negative = false ;
121- let mut n = if T :: SIGNED {
170+ let mut n = if T :: IS_INT {
122171 let signed = self . 0 . as_i128 ( ) ;
123172 is_negative = signed < 0 ;
124173 ( !( signed as u128 ) ) . wrapping_add ( 1 )
0 commit comments