about summary refs log tree commit diff
path: root/src/libsyntax/print/pprust.rs
diff options
context:
space:
mode:
authorMark Rousskov <mark.simulacrum@gmail.com>2019-07-05 19:10:18 -0400
committerMark Rousskov <mark.simulacrum@gmail.com>2019-07-10 07:12:28 -0400
commit00ca508608381f9594dc4a60d3c5b436660a2e19 (patch)
tree765417a03e822e600e0f0f92c73e829313bfd100 /src/libsyntax/print/pprust.rs
parente0ffa7c3d2115b9a1d22b6a5de288aa696abf50f (diff)
downloadrust-00ca508608381f9594dc4a60d3c5b436660a2e19.tar.gz
rust-00ca508608381f9594dc4a60d3c5b436660a2e19.zip
Move pp::Printer out field to owned String
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.
Diffstat (limited to 'src/libsyntax/print/pprust.rs')
-rw-r--r--src/libsyntax/print/pprust.rs32
1 files changed, 13 insertions, 19 deletions
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index 43714d3015b..54672d9da2e 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -87,7 +87,7 @@ impl<'a> Comments<'a> {
 }
 
 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 @@ pub trait PrintState<'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
     }