1
1
use core:: char;
2
2
use core:: fmt;
3
- use core:: fmt:: { Display , Write } ;
3
+
4
+ #[ allow( unused_macros) ]
5
+ macro_rules! write {
6
+ ( $( $ignored: tt) * ) => {
7
+ compile_error!( "use `fmt::Trait::fmt(&value, self.out)` instead of write!" )
8
+ } ;
9
+ }
4
10
5
11
// Maximum recursion depth when parsing symbols before we just bail out saying
6
12
// "this symbol is invalid"
7
13
const MAX_DEPTH : u32 = 500 ;
8
14
9
- // Approximately the maximum size of the symbol that we'll print. This is
10
- // approximate because it only limits calls writing to `LimitedFormatter`, but
11
- // not all writes exclusively go through `LimitedFormatter`. Some writes go
12
- // directly to the underlying formatter, but when that happens we always write
13
- // at least a little to the `LimitedFormatter`.
14
- const MAX_APPROX_SIZE : usize = 1_000_000 ;
15
-
16
15
/// Representation of a demangled symbol name.
17
16
pub struct Demangle < ' a > {
18
17
inner : & ' a str ,
@@ -68,19 +67,15 @@ pub fn demangle(s: &str) -> Result<(Demangle, &str), Invalid> {
68
67
Ok ( ( Demangle { inner } , & parser. sym [ parser. next ..] ) )
69
68
}
70
69
71
- impl < ' s > Display for Demangle < ' s > {
70
+ impl < ' s > fmt :: Display for Demangle < ' s > {
72
71
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
73
- let mut remaining = MAX_APPROX_SIZE ;
74
72
let mut printer = Printer {
75
73
parser : Ok ( Parser {
76
74
sym : self . inner ,
77
75
next : 0 ,
78
76
depth : 0 ,
79
77
} ) ,
80
- out : LimitedFormatter {
81
- remaining : & mut remaining,
82
- inner : f,
83
- } ,
78
+ out : f,
84
79
bound_lifetime_depth : 0 ,
85
80
} ;
86
81
printer. print_path ( true )
@@ -222,7 +217,7 @@ impl<'s> Ident<'s> {
222
217
}
223
218
}
224
219
225
- impl < ' s > Display for Ident < ' s > {
220
+ impl < ' s > fmt :: Display for Ident < ' s > {
226
221
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
227
222
self . try_small_punycode_decode ( |chars| {
228
223
for & c in chars {
@@ -605,7 +600,7 @@ impl<'s> Parser<'s> {
605
600
606
601
struct Printer < ' a , ' b : ' a , ' s > {
607
602
parser : Result < Parser < ' s > , Invalid > ,
608
- out : LimitedFormatter < ' a , ' b > ,
603
+ out : & ' a mut fmt :: Formatter < ' b > ,
609
604
bound_lifetime_depth : u32 ,
610
605
}
611
606
@@ -649,10 +644,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
649
644
fn backref_printer < ' c > ( & ' c mut self ) -> Printer < ' c , ' b , ' s > {
650
645
Printer {
651
646
parser : self . parser_mut ( ) . and_then ( |p| p. backref ( ) ) ,
652
- out : LimitedFormatter {
653
- remaining : self . out . remaining ,
654
- inner : self . out . inner ,
655
- } ,
647
+ out : self . out ,
656
648
bound_lifetime_depth : self . bound_lifetime_depth ,
657
649
}
658
650
}
@@ -686,11 +678,11 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
686
678
// Try to print lifetimes alphabetically first.
687
679
if depth < 26 {
688
680
let c = ( b'a' + depth as u8 ) as char ;
689
- c . fmt ( self . out . inner )
681
+ fmt:: Display :: fmt ( & c , self . out )
690
682
} else {
691
683
// Use `'_123` after running out of letters.
692
684
self . out . write_str ( "_" ) ?;
693
- depth . fmt ( self . out . inner )
685
+ fmt:: Display :: fmt ( & depth , self . out )
694
686
}
695
687
}
696
688
None => invalid ! ( self ) ,
@@ -750,10 +742,10 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
750
742
let dis = parse ! ( self , disambiguator) ;
751
743
let name = parse ! ( self , ident) ;
752
744
753
- name . fmt ( self . out . inner ) ?;
754
- if !self . out . inner . alternate ( ) {
745
+ fmt:: Display :: fmt ( & name , self . out ) ?;
746
+ if !self . out . alternate ( ) {
755
747
self . out . write_str ( "[" ) ?;
756
- fmt:: LowerHex :: fmt ( & dis, self . out . inner ) ?;
748
+ fmt:: LowerHex :: fmt ( & dis, self . out ) ?;
757
749
self . out . write_str ( "]" ) ?;
758
750
}
759
751
}
@@ -772,22 +764,22 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
772
764
match ns {
773
765
'C' => self . out . write_str ( "closure" ) ?,
774
766
'S' => self . out . write_str ( "shim" ) ?,
775
- _ => ns . fmt ( self . out . inner ) ?,
767
+ _ => fmt:: Display :: fmt ( & ns , self . out ) ?,
776
768
}
777
769
if !name. ascii . is_empty ( ) || !name. punycode . is_empty ( ) {
778
770
self . out . write_str ( ":" ) ?;
779
- name . fmt ( self . out . inner ) ?;
771
+ fmt:: Display :: fmt ( & name , self . out ) ?;
780
772
}
781
773
self . out . write_str ( "#" ) ?;
782
- dis . fmt ( self . out . inner ) ?;
774
+ fmt:: Display :: fmt ( & dis , self . out ) ?;
783
775
self . out . write_str ( "}" ) ?;
784
776
}
785
777
786
778
// Implementation-specific/unspecified namespaces.
787
779
None => {
788
780
if !name. ascii . is_empty ( ) || !name. punycode . is_empty ( ) {
789
781
self . out . write_str ( "::" ) ?;
790
- name . fmt ( self . out . inner ) ?;
782
+ fmt:: Display :: fmt ( & name , self . out ) ?;
791
783
}
792
784
}
793
785
}
@@ -1000,7 +992,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
1000
992
}
1001
993
1002
994
let name = parse ! ( self , ident) ;
1003
- name . fmt ( self . out . inner ) ?;
995
+ fmt:: Display :: fmt ( & name , self . out ) ?;
1004
996
self . out . write_str ( " = " ) ?;
1005
997
self . print_type ( ) ?;
1006
998
}
@@ -1039,7 +1031,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
1039
1031
_ => invalid ! ( self ) ,
1040
1032
} ;
1041
1033
1042
- if !self . out . inner . alternate ( ) {
1034
+ if !self . out . alternate ( ) {
1043
1035
self . out . write_str ( ": " ) ?;
1044
1036
let ty = basic_type ( ty_tag) . unwrap ( ) ;
1045
1037
self . out . write_str ( ty) ?;
@@ -1061,7 +1053,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
1061
1053
for c in hex. chars ( ) {
1062
1054
v = ( v << 4 ) | ( c. to_digit ( 16 ) . unwrap ( ) as u64 ) ;
1063
1055
}
1064
- v . fmt ( self . out . inner )
1056
+ fmt:: Display :: fmt ( & v , self . out )
1065
1057
}
1066
1058
1067
1059
fn print_const_int ( & mut self ) -> fmt:: Result {
@@ -1093,7 +1085,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
1093
1085
v = ( v << 4 ) | ( c. to_digit ( 16 ) . unwrap ( ) as u32 ) ;
1094
1086
}
1095
1087
if let Some ( c) = char:: from_u32 ( v) {
1096
- write ! ( self . out, "{:?}" , c )
1088
+ fmt :: Debug :: fmt ( & c , self . out )
1097
1089
} else {
1098
1090
invalid ! ( self )
1099
1091
}
@@ -1792,20 +1784,3 @@ RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRB_E"
1792
1784
) ;
1793
1785
}
1794
1786
}
1795
-
1796
- struct LimitedFormatter < ' a , ' b > {
1797
- remaining : & ' a mut usize ,
1798
- inner : & ' a mut fmt:: Formatter < ' b > ,
1799
- }
1800
-
1801
- impl Write for LimitedFormatter < ' _ , ' _ > {
1802
- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
1803
- match self . remaining . checked_sub ( s. len ( ) ) {
1804
- Some ( amt) => {
1805
- * self . remaining = amt;
1806
- self . inner . write_str ( s)
1807
- }
1808
- None => Err ( fmt:: Error ) ,
1809
- }
1810
- }
1811
- }
0 commit comments