@@ -37,114 +37,36 @@ use crate::html::render::Context;
3737use crate :: joined:: Joined as _;
3838use crate :: passes:: collect_intra_doc_links:: UrlFragment ;
3939
40- pub ( crate ) trait Print {
41- fn print ( self , buffer : & mut Buffer ) ;
42- }
43-
44- impl < F > Print for F
45- where
46- F : FnOnce ( & mut Buffer ) ,
47- {
48- fn print ( self , buffer : & mut Buffer ) {
49- ( self ) ( buffer)
50- }
51- }
52-
53- impl Print for String {
54- fn print ( self , buffer : & mut Buffer ) {
55- buffer. write_str ( & self ) ;
56- }
57- }
58-
59- impl Print for & ' _ str {
60- fn print ( self , buffer : & mut Buffer ) {
61- buffer. write_str ( self ) ;
62- }
63- }
40+ /// This macro is the same as [`std::write!`] for [`String`]s, but swallows the returned `Result`,
41+ /// since writing into a `String` can never fail.
42+ macro_rules! write_str {
43+ ( $dst: expr, $( $arg: tt) * ) => { {
44+ // make sure $dst is a `String` (or `&mut String`)
45+ trait AssertString {
46+ fn assert_string( & mut self ) { }
47+ }
48+ impl AssertString for :: std:: string:: String { }
49+ $dst. assert_string( ) ;
6450
65- #[ derive( Debug , Clone ) ]
66- pub ( crate ) struct Buffer {
67- for_html : bool ,
68- buffer : String ,
51+ let _ = $dst. write_fmt( :: std:: format_args!( $( $arg) * ) ) ;
52+ } } ;
6953}
54+ /// This macro is the same as [`std::writeln!`] for [`String`]s, but swallows the returned `Result`,
55+ /// since writing into a `String` can never fail.
56+ macro_rules! writeln_str {
57+ ( $dst: expr, $( $arg: tt) * ) => { {
58+ // make sure $dst is a `String` (or `&mut String`)
59+ trait AssertString {
60+ fn assert_string( & mut self ) { }
61+ }
62+ impl AssertString for :: std:: string:: String { }
63+ $dst. assert_string( ) ;
7064
71- impl core:: fmt:: Write for Buffer {
72- #[ inline]
73- fn write_str ( & mut self , s : & str ) -> fmt:: Result {
74- self . buffer . write_str ( s)
75- }
76-
77- #[ inline]
78- fn write_char ( & mut self , c : char ) -> fmt:: Result {
79- self . buffer . write_char ( c)
80- }
81-
82- #[ inline]
83- fn write_fmt ( & mut self , args : fmt:: Arguments < ' _ > ) -> fmt:: Result {
84- self . buffer . write_fmt ( args)
85- }
65+ let _ = $dst. write_fmt( :: std:: format_args_nl!( $( $arg) * ) ) ;
66+ } } ;
8667}
8768
88- impl Buffer {
89- pub ( crate ) fn empty_from ( v : & Buffer ) -> Buffer {
90- Buffer { for_html : v. for_html , buffer : String :: new ( ) }
91- }
92-
93- pub ( crate ) fn html ( ) -> Buffer {
94- Buffer { for_html : true , buffer : String :: new ( ) }
95- }
96-
97- pub ( crate ) fn new ( ) -> Buffer {
98- Buffer { for_html : false , buffer : String :: new ( ) }
99- }
100-
101- pub ( crate ) fn is_empty ( & self ) -> bool {
102- self . buffer . is_empty ( )
103- }
104-
105- pub ( crate ) fn into_inner ( self ) -> String {
106- self . buffer
107- }
108-
109- pub ( crate ) fn push ( & mut self , c : char ) {
110- self . buffer . push ( c) ;
111- }
112-
113- pub ( crate ) fn push_str ( & mut self , s : & str ) {
114- self . buffer . push_str ( s) ;
115- }
116-
117- pub ( crate ) fn push_buffer ( & mut self , other : Buffer ) {
118- self . buffer . push_str ( & other. buffer ) ;
119- }
120-
121- // Intended for consumption by write! and writeln! (std::fmt) but without
122- // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
123- // import).
124- pub ( crate ) fn write_str ( & mut self , s : & str ) {
125- self . buffer . push_str ( s) ;
126- }
127-
128- // Intended for consumption by write! and writeln! (std::fmt) but without
129- // the fmt::Result return type imposed by fmt::Write (and avoiding the trait
130- // import).
131- pub ( crate ) fn write_fmt ( & mut self , v : fmt:: Arguments < ' _ > ) {
132- self . buffer . write_fmt ( v) . unwrap ( ) ;
133- }
134-
135- pub ( crate ) fn to_display < T : Print > ( mut self , t : T ) -> String {
136- t. print ( & mut self ) ;
137- self . into_inner ( )
138- }
139-
140- pub ( crate ) fn reserve ( & mut self , additional : usize ) {
141- self . buffer . reserve ( additional)
142- }
143-
144- pub ( crate ) fn len ( & self ) -> usize {
145- self . buffer . len ( )
146- }
147- }
69+ pub ( crate ) use { write_str, writeln_str} ;
14870
14971pub ( crate ) fn print_generic_bounds < ' a , ' tcx : ' a > (
15072 bounds : & ' a [ clean:: GenericBound ] ,
@@ -767,12 +689,14 @@ pub(crate) fn href_relative_parts<'fqp>(
767689}
768690
769691pub ( crate ) fn link_tooltip ( did : DefId , fragment : & Option < UrlFragment > , cx : & Context < ' _ > ) -> String {
692+ use write_str as write;
693+
770694 let cache = cx. cache ( ) ;
771695 let Some ( ( fqp, shortty) ) = cache. paths . get ( & did) . or_else ( || cache. external_paths . get ( & did) )
772696 else {
773697 return String :: new ( ) ;
774698 } ;
775- let mut buf = Buffer :: new ( ) ;
699+ let mut buf = String :: new ( ) ;
776700 let fqp = if * shortty == ItemType :: Primitive {
777701 // primitives are documented in a crate, but not actually part of it
778702 & fqp[ fqp. len ( ) - 1 ..]
@@ -792,7 +716,7 @@ pub(crate) fn link_tooltip(did: DefId, fragment: &Option<UrlFragment>, cx: &Cont
792716 write ! ( buf, "::{component}" ) ;
793717 }
794718 }
795- buf. into_inner ( )
719+ buf
796720}
797721
798722/// Used to render a [`clean::Path`].
0 commit comments