]> git.lizzy.rs Git - rust.git/commitdiff
Move pp::Printer out field to owned String
authorMark Rousskov <mark.simulacrum@gmail.com>
Fri, 5 Jul 2019 23:10:18 +0000 (19:10 -0400)
committerMark Rousskov <mark.simulacrum@gmail.com>
Wed, 10 Jul 2019 11:12:28 +0000 (07:12 -0400)
This enforces that eof() must be called to get the String out, and
generally is better from an API perspective. No users of pretty printing
pre-allocate the buffer.

src/librustc/hir/print.rs
src/librustc_driver/pretty.rs
src/libsyntax/print/pp.rs
src/libsyntax/print/pprust.rs

index 01cdd8a6f22bb1b6bc20acc1ab69b91b0ccf849d..8342331e36099241e30798c16e65a9cd2582363e 100644 (file)
@@ -68,13 +68,13 @@ fn nested(&self, state: &mut State<'_>, nested: Nested) {
 }
 
 pub struct State<'a> {
-    pub s: pp::Printer<'a>,
+    pub s: pp::Printer,
     comments: Option<Comments<'a>>,
     ann: &'a (dyn PpAnn + 'a),
 }
 
 impl<'a> PrintState<'a> for State<'a> {
-    fn writer(&mut self) -> &mut pp::Printer<'a> {
+    fn writer(&mut self) -> &mut pp::Printer {
         &mut self.s
     }
 
@@ -94,16 +94,14 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
                        filename: FileName,
                        input: String,
                        ann: &'a dyn PpAnn) -> String {
-    let mut out = String::new();
-    let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann);
+    let mut s = State::new_from_input(cm, sess, filename, input, ann);
 
     // When printing the AST, we sometimes need to inject `#[no_std]` here.
     // Since you can't compile the HIR, it's not necessary.
 
     s.print_mod(&krate.module, &krate.attrs);
     s.print_remaining_comments();
-    s.s.eof();
-    out
+    s.s.eof()
 }
 
 impl<'a> State<'a> {
@@ -111,11 +109,10 @@ pub fn new_from_input(cm: &'a SourceMap,
                           sess: &ParseSess,
                           filename: FileName,
                           input: String,
-                          out: &'a mut String,
                           ann: &'a dyn PpAnn)
                           -> State<'a> {
         State {
-            s: pp::mk_printer(out),
+            s: pp::mk_printer(),
             comments: Some(Comments::new(cm, sess, filename, input)),
             ann,
         }
@@ -125,17 +122,13 @@ pub fn new_from_input(cm: &'a SourceMap,
 pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
     where F: FnOnce(&mut State<'_>)
 {
-    let mut wr = String::new();
-    {
-        let mut printer = State {
-            s: pp::mk_printer(&mut wr),
-            comments: None,
-            ann,
-        };
-        f(&mut printer);
-        printer.s.eof();
-    }
-    wr
+    let mut printer = State {
+        s: pp::mk_printer(),
+        comments: None,
+        ann,
+    };
+    f(&mut printer);
+    printer.s.eof()
 }
 
 pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility, w: S) -> String {
index 3e6d843ffbe3104ceb1ee696029931f9aa018336..cd38eb695eb5dfbde67c61d8143fed13aa5b7759 100644 (file)
@@ -814,7 +814,6 @@ pub fn print_after_hir_lowering<'tcx>(
                                                                          &sess.parse_sess,
                                                                          src_name,
                                                                          src,
-                                                                         out,
                                                                          annotation.pp_ann());
                     for node_id in uii.all_matching_node_ids(hir_map) {
                         let hir_id = tcx.hir().node_to_hir_id(node_id);
@@ -826,7 +825,7 @@ pub fn print_after_hir_lowering<'tcx>(
                         pp_state.synth_comment(path);
                         pp_state.s.hardbreak();
                     }
-                    pp_state.s.eof();
+                    *out = pp_state.s.eof();
                 })
             }
 
index f64e95aee5bca91ee5ac4382bdf997db165a519e..ea90defcd508c10809dd9b84d6f2b8a27c4a7b66 100644 (file)
@@ -235,13 +235,13 @@ fn buf_str(buf: &[BufEntry], left: usize, right: usize, lim: usize) -> String {
 
 const SIZE_INFINITY: isize = 0xffff;
 
-pub fn mk_printer(out: &mut String) -> Printer<'_> {
+pub fn mk_printer() -> Printer {
     let linewidth = 78;
     // Yes 55, it makes the ring buffers big enough to never fall behind.
     let n: usize = 55 * linewidth;
     debug!("mk_printer {}", linewidth);
     Printer {
-        out,
+        out: String::new(),
         buf_max_len: n,
         margin: linewidth as isize,
         space: linewidth as isize,
@@ -258,8 +258,8 @@ pub fn mk_printer(out: &mut String) -> Printer<'_> {
     }
 }
 
-pub struct Printer<'a> {
-    out: &'a mut String,
+pub struct Printer {
+    out: String,
     buf_max_len: usize,
     /// Width of lines we're constrained to
     margin: isize,
@@ -300,7 +300,7 @@ fn default() -> Self {
     }
 }
 
-impl<'a> Printer<'a> {
+impl Printer {
     pub fn last_token(&mut self) -> Token {
         self.buf[self.right].token.clone()
     }
@@ -629,8 +629,9 @@ pub fn break_offset(&mut self, n: usize, off: isize) {
         self.pretty_print_end()
     }
 
-    pub fn eof(&mut self) {
-        self.pretty_print_eof()
+    pub fn eof(mut self) -> String {
+        self.pretty_print_eof();
+        self.out
     }
 
     pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {
index 43714d3015bed66155c530eb0438a976f3d6e2c8..54672d9da2e2b8ffffc70366b2befa89b597176d 100644 (file)
@@ -87,7 +87,7 @@ pub fn trailing_comment(
 }
 
 pub struct State<'a> {
-    pub s: pp::Printer<'a>,
+    pub s: pp::Printer,
     comments: Option<Comments<'a>>,
     ann: &'a (dyn PpAnn+'a),
     is_expanded: bool
@@ -104,9 +104,8 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
                        input: String,
                        ann: &'a dyn PpAnn,
                        is_expanded: bool) -> String {
-    let mut out = String::new();
     let mut s = State {
-        s: pp::mk_printer(&mut out),
+        s: pp::mk_printer(),
         comments: Some(Comments::new(cm, sess, filename, input)),
         ann,
         is_expanded,
@@ -133,25 +132,20 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
 
     s.print_mod(&krate.module, &krate.attrs);
     s.print_remaining_comments();
-    s.s.eof();
-    out
+    s.s.eof()
 }
 
 pub fn to_string<F>(f: F) -> String where
     F: FnOnce(&mut State<'_>),
 {
-    let mut wr = String::new();
-    {
-        let mut printer = State {
-            s: pp::mk_printer(&mut wr),
-            comments: None,
-            ann: &NoAnn,
-            is_expanded: false
-        };
-        f(&mut printer);
-        printer.s.eof();
-    }
-    wr
+    let mut printer = State {
+        s: pp::mk_printer(),
+        comments: None,
+        ann: &NoAnn,
+        is_expanded: false
+    };
+    f(&mut printer);
+    printer.s.eof()
 }
 
 fn binop_to_string(op: BinOpToken) -> &'static str {
@@ -439,7 +433,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
 }
 
 pub trait PrintState<'a> {
-    fn writer(&mut self) -> &mut pp::Printer<'a>;
+    fn writer(&mut self) -> &mut pp::Printer;
     fn comments(&mut self) -> &mut Option<Comments<'a>>;
 
     fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
@@ -760,7 +754,7 @@ fn nbsp(&mut self) { self.writer().word(" ") }
 }
 
 impl<'a> PrintState<'a> for State<'a> {
-    fn writer(&mut self) -> &mut pp::Printer<'a> {
+    fn writer(&mut self) -> &mut pp::Printer {
         &mut self.s
     }