2121//!     playground: &None, 
2222//!     heading_offset: HeadingOffset::H2, 
2323//! }; 
24- //! let html = md.into_string(); 
24+ //! let mut html = String::new(); 
25+ //! md.write_into(&mut html).unwrap(); 
2526//! // ... something using html 
2627//! ``` 
2728
2829use  std:: borrow:: Cow ; 
2930use  std:: collections:: VecDeque ; 
30- use  std:: fmt:: Write ; 
31+ use  std:: fmt:: { self ,   Write } ; 
3132use  std:: iter:: Peekable ; 
3233use  std:: ops:: { ControlFlow ,  Range } ; 
3334use  std:: path:: PathBuf ; 
@@ -1327,17 +1328,32 @@ impl LangString {
13271328    } 
13281329} 
13291330
1331+ trait  WriteSpec  { 
1332+     fn  reserve_spec ( & mut  self ,  additional :  usize ) ; 
1333+ } 
1334+ 
1335+ impl < W :  fmt:: Write >  WriteSpec  for  W  { 
1336+     #[ inline]  
1337+     default  fn  reserve_spec ( & mut  self ,  _additional :  usize )  { } 
1338+ } 
1339+ 
1340+ impl < ' a >  WriteSpec  for  & ' a  mut  String  { 
1341+     #[ inline]  
1342+     fn  reserve_spec ( & mut  self ,  additional :  usize )  { 
1343+         self . reserve ( additional) ; 
1344+     } 
1345+ } 
1346+ 
13301347impl < ' a >  Markdown < ' a >  { 
1331-     pub  fn  into_string ( self )  -> String  { 
1348+     pub  fn  write_into ( self ,   mut   f :   impl  fmt :: Write )  -> fmt :: Result  { 
13321349        // This is actually common enough to special-case 
13331350        if  self . content . is_empty ( )  { 
1334-             return  String :: new ( ) ; 
1351+             return  Ok ( ( ) ) ; 
13351352        } 
13361353
1337-         let  mut  s = String :: with_capacity ( self . content . len ( )  *  3  / 2 ) ; 
1338-         html:: push_html ( & mut  s,  self . into_iter ( ) ) ; 
1354+         f. reserve_spec ( self . content . len ( )  *  3  / 2 ) ; 
13391355
1340-         s 
1356+         html :: write_html_fmt ( f ,   self . into_iter ( ) ) 
13411357    } 
13421358
13431359    fn  into_iter ( self )  -> CodeBlocks < ' a ,  ' a ,  impl  Iterator < Item  = Event < ' a > > >  { 
@@ -1453,19 +1469,20 @@ impl MarkdownWithToc<'_> {
14531469
14541470        ( toc. into_toc ( ) ,  s) 
14551471    } 
1456-     pub ( crate )  fn  into_string ( self )  -> String  { 
1472+ 
1473+     pub ( crate )  fn  write_into ( self ,  mut  f :  impl  fmt:: Write )  -> fmt:: Result  { 
14571474        let  ( toc,  s)  = self . into_parts ( ) ; 
1458-         format ! ( "<nav id=\" rustdoc\" >{toc}</nav>{s}" ,  toc = toc. print( ) ) 
1475+         write ! ( f ,   "<nav id=\" rustdoc\" >{toc}</nav>{s}" ,  toc = toc. print( ) ) 
14591476    } 
14601477} 
14611478
14621479impl  MarkdownItemInfo < ' _ >  { 
1463-     pub ( crate )  fn  into_string ( self )  -> String  { 
1480+     pub ( crate )  fn  write_into ( self ,   mut   f :   impl  fmt :: Write )  -> fmt :: Result  { 
14641481        let  MarkdownItemInfo ( md,  ids)  = self ; 
14651482
14661483        // This is actually common enough to special-case 
14671484        if  md. is_empty ( )  { 
1468-             return  String :: new ( ) ; 
1485+             return  Ok ( ( ) ) ; 
14691486        } 
14701487        let  p = Parser :: new_ext ( md,  main_body_opts ( ) ) . into_offset_iter ( ) ; 
14711488
@@ -1475,7 +1492,7 @@ impl MarkdownItemInfo<'_> {
14751492            _ => event, 
14761493        } ) ; 
14771494
1478-         let   mut  s =  String :: with_capacity ( md. len ( )  *  3  / 2 ) ; 
1495+         f . reserve_spec ( md. len ( )  *  3  / 2 ) ; 
14791496
14801497        ids. handle_footnotes ( |ids,  existing_footnotes| { 
14811498            let  p = HeadingLinks :: new ( p,  None ,  ids,  HeadingOffset :: H1 ) ; 
@@ -1484,10 +1501,8 @@ impl MarkdownItemInfo<'_> {
14841501            let  p = p. filter ( |event| { 
14851502                !matches ! ( event,  Event :: Start ( Tag :: Paragraph )  | Event :: End ( TagEnd :: Paragraph ) ) 
14861503            } ) ; 
1487-             html:: push_html ( & mut  s,  p) ; 
1488-         } ) ; 
1489- 
1490-         s
1504+             html:: write_html_fmt ( & mut  f,  p) 
1505+         } ) 
14911506    } 
14921507} 
14931508
0 commit comments