@@ -847,7 +847,7 @@ fn resolved_path<'cx>(
847
847
fn primitive_link (
848
848
f : & mut fmt:: Formatter < ' _ > ,
849
849
prim : clean:: PrimitiveType ,
850
- name : & str ,
850
+ name : & dyn std :: fmt :: Display ,
851
851
cx : & Context < ' _ > ,
852
852
) -> fmt:: Result {
853
853
primitive_link_fragment ( f, prim, name, "" , cx)
@@ -856,7 +856,7 @@ fn primitive_link(
856
856
fn primitive_link_fragment (
857
857
f : & mut fmt:: Formatter < ' _ > ,
858
858
prim : clean:: PrimitiveType ,
859
- name : & str ,
859
+ name : & dyn std :: fmt :: Display ,
860
860
fragment : & str ,
861
861
cx : & Context < ' _ > ,
862
862
) -> fmt:: Result {
@@ -907,7 +907,7 @@ fn primitive_link_fragment(
907
907
None => { }
908
908
}
909
909
}
910
- f . write_str ( name ) ?;
910
+ name . fmt ( f ) ?;
911
911
if needs_termination {
912
912
write ! ( f, "</a>" ) ?;
913
913
}
@@ -977,9 +977,9 @@ fn fmt_type<'cx>(
977
977
}
978
978
clean:: Infer => write ! ( f, "_" ) ,
979
979
clean:: Primitive ( clean:: PrimitiveType :: Never ) => {
980
- primitive_link ( f, PrimitiveType :: Never , "!" , cx)
980
+ primitive_link ( f, PrimitiveType :: Never , & "!" , cx)
981
981
}
982
- clean:: Primitive ( prim) => primitive_link ( f, prim, prim. as_sym ( ) . as_str ( ) , cx) ,
982
+ clean:: Primitive ( prim) => primitive_link ( f, prim, & prim. as_sym ( ) . as_str ( ) , cx) ,
983
983
clean:: BareFunction ( ref decl) => {
984
984
if f. alternate ( ) {
985
985
write ! (
@@ -998,16 +998,21 @@ fn fmt_type<'cx>(
998
998
decl. unsafety. print_with_space( ) ,
999
999
print_abi_with_space( decl. abi)
1000
1000
) ?;
1001
- primitive_link ( f, PrimitiveType :: Fn , "fn" , cx) ?;
1001
+ primitive_link ( f, PrimitiveType :: Fn , & "fn" , cx) ?;
1002
1002
write ! ( f, "{}" , decl. decl. print( cx) )
1003
1003
}
1004
1004
}
1005
1005
clean:: Tuple ( ref typs) => {
1006
1006
match & typs[ ..] {
1007
- & [ ] => primitive_link ( f, PrimitiveType :: Unit , "()" , cx) ,
1007
+ & [ ] => primitive_link ( f, PrimitiveType :: Unit , & "()" , cx) ,
1008
1008
[ one] => {
1009
1009
if let clean:: Generic ( name) = one {
1010
- primitive_link ( f, PrimitiveType :: Tuple , & format ! ( "({name},)" ) , cx)
1010
+ primitive_link (
1011
+ f,
1012
+ PrimitiveType :: Tuple ,
1013
+ & display_fn ( |f| write ! ( f, "({name},)" ) ) ,
1014
+ cx,
1015
+ )
1011
1016
} else {
1012
1017
write ! ( f, "(" ) ?;
1013
1018
// Carry `f.alternate()` into this display w/o branching manually.
@@ -1028,7 +1033,13 @@ fn fmt_type<'cx>(
1028
1033
primitive_link (
1029
1034
f,
1030
1035
PrimitiveType :: Tuple ,
1031
- & format ! ( "({})" , generic_names. iter( ) . map( |s| s. as_str( ) ) . join( ", " ) ) ,
1036
+ & display_fn ( |f| {
1037
+ write ! (
1038
+ f,
1039
+ "({})" ,
1040
+ generic_names. iter( ) . map( |s| s. as_str( ) ) . join( ", " )
1041
+ )
1042
+ } ) ,
1032
1043
cx,
1033
1044
)
1034
1045
} else {
@@ -1047,7 +1058,7 @@ fn fmt_type<'cx>(
1047
1058
}
1048
1059
clean:: Slice ( ref t) => match * * t {
1049
1060
clean:: Generic ( name) => {
1050
- primitive_link ( f, PrimitiveType :: Slice , & format ! ( "[{name}]" ) , cx)
1061
+ primitive_link ( f, PrimitiveType :: Slice , & display_fn ( |f| write ! ( f , "[{name}]" ) ) , cx)
1051
1062
}
1052
1063
_ => {
1053
1064
write ! ( f, "[" ) ?;
@@ -1059,7 +1070,7 @@ fn fmt_type<'cx>(
1059
1070
clean:: Generic ( name) if !f. alternate ( ) => primitive_link (
1060
1071
f,
1061
1072
PrimitiveType :: Array ,
1062
- & format ! ( "[{name}; {n}]" , n = Escape ( n) ) ,
1073
+ & display_fn ( |f| write ! ( f , "[{name}; {n}]" , n = Escape ( n) ) ) ,
1063
1074
cx,
1064
1075
) ,
1065
1076
_ => {
@@ -1069,7 +1080,12 @@ fn fmt_type<'cx>(
1069
1080
write ! ( f, "; {n}" ) ?;
1070
1081
} else {
1071
1082
write ! ( f, "; " ) ?;
1072
- primitive_link ( f, PrimitiveType :: Array , & format ! ( "{n}" , n = Escape ( n) ) , cx) ?;
1083
+ primitive_link (
1084
+ f,
1085
+ PrimitiveType :: Array ,
1086
+ & display_fn ( |f| write ! ( f, "{n}" , n = Escape ( n) ) ) ,
1087
+ cx,
1088
+ ) ?;
1073
1089
}
1074
1090
write ! ( f, "]" )
1075
1091
}
@@ -1081,30 +1097,34 @@ fn fmt_type<'cx>(
1081
1097
} ;
1082
1098
1083
1099
if matches ! ( * * t, clean:: Generic ( _) ) || t. is_assoc_ty ( ) {
1084
- let text = if f. alternate ( ) {
1085
- format ! ( "*{m} {ty:#}" , ty = t. print( cx) )
1100
+ let ( f1, f2) ;
1101
+ let display: & dyn std:: fmt:: Display = if f. alternate ( ) {
1102
+ f1 = display_fn ( |f| write ! ( f, "*{m} {ty:#}" , ty = t. print( cx) ) ) ;
1103
+ & f1
1086
1104
} else {
1087
- format ! ( "*{m} {ty}" , ty = t. print( cx) )
1105
+ f2 = display_fn ( |f| write ! ( f, "*{m} {ty}" , ty = t. print( cx) ) ) ;
1106
+ & f2
1088
1107
} ;
1089
- primitive_link ( f, clean:: PrimitiveType :: RawPointer , & text , cx)
1108
+ primitive_link ( f, clean:: PrimitiveType :: RawPointer , display , cx)
1090
1109
} else {
1091
- primitive_link ( f, clean:: PrimitiveType :: RawPointer , & format ! ( "*{m} " ) , cx) ?;
1110
+ let display = display_fn ( |f| write ! ( f, "*{m} " ) ) ;
1111
+ primitive_link ( f, clean:: PrimitiveType :: RawPointer , & display, cx) ?;
1092
1112
fmt:: Display :: fmt ( & t. print ( cx) , f)
1093
1113
}
1094
1114
}
1095
1115
clean:: BorrowedRef { lifetime : ref l, mutability, type_ : ref ty } => {
1096
- let lt = match l {
1097
- Some ( l) => format ! ( "{} " , l. print( ) ) ,
1098
- _ => String :: new ( ) ,
1099
- } ;
1116
+ let lt = display_fn ( |f| match l {
1117
+ Some ( l) => write ! ( f , "{} " , l. print( ) ) ,
1118
+ _ => Ok ( ( ) ) ,
1119
+ } ) ;
1100
1120
let m = mutability. print_with_space ( ) ;
1101
1121
let amp = if f. alternate ( ) { "&" } else { "&" } ;
1102
1122
1103
1123
if let clean:: Generic ( name) = * * ty {
1104
1124
return primitive_link (
1105
1125
f,
1106
1126
PrimitiveType :: Reference ,
1107
- & format ! ( "{amp}{lt}{m}{name}" ) ,
1127
+ & display_fn ( |f| write ! ( f , "{amp}{lt}{m}{name}" ) ) ,
1108
1128
cx,
1109
1129
) ;
1110
1130
}
@@ -1665,7 +1685,7 @@ impl clean::ImportSource {
1665
1685
}
1666
1686
let name = self . path . last ( ) ;
1667
1687
if let hir:: def:: Res :: PrimTy ( p) = self . path . res {
1668
- primitive_link ( f, PrimitiveType :: from ( p) , name. as_str ( ) , cx) ?;
1688
+ primitive_link ( f, PrimitiveType :: from ( p) , & name. as_str ( ) , cx) ?;
1669
1689
} else {
1670
1690
f. write_str ( name. as_str ( ) ) ?;
1671
1691
}
0 commit comments