Skip to content

Commit 00ca508

Browse files
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.
1 parent e0ffa7c commit 00ca508

File tree

4 files changed

+34
-47
lines changed

4 files changed

+34
-47
lines changed

src/librustc/hir/print.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl PpAnn for hir::Crate {
6868
}
6969

7070
pub struct State<'a> {
71-
pub s: pp::Printer<'a>,
71+
pub s: pp::Printer,
7272
comments: Option<Comments<'a>>,
7373
ann: &'a (dyn PpAnn + 'a),
7474
}
7575

7676
impl<'a> PrintState<'a> for State<'a> {
77-
fn writer(&mut self) -> &mut pp::Printer<'a> {
77+
fn writer(&mut self) -> &mut pp::Printer {
7878
&mut self.s
7979
}
8080

@@ -94,28 +94,25 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
9494
filename: FileName,
9595
input: String,
9696
ann: &'a dyn PpAnn) -> String {
97-
let mut out = String::new();
98-
let mut s = State::new_from_input(cm, sess, filename, input, &mut out, ann);
97+
let mut s = State::new_from_input(cm, sess, filename, input, ann);
9998

10099
// When printing the AST, we sometimes need to inject `#[no_std]` here.
101100
// Since you can't compile the HIR, it's not necessary.
102101

103102
s.print_mod(&krate.module, &krate.attrs);
104103
s.print_remaining_comments();
105-
s.s.eof();
106-
out
104+
s.s.eof()
107105
}
108106

109107
impl<'a> State<'a> {
110108
pub fn new_from_input(cm: &'a SourceMap,
111109
sess: &ParseSess,
112110
filename: FileName,
113111
input: String,
114-
out: &'a mut String,
115112
ann: &'a dyn PpAnn)
116113
-> State<'a> {
117114
State {
118-
s: pp::mk_printer(out),
115+
s: pp::mk_printer(),
119116
comments: Some(Comments::new(cm, sess, filename, input)),
120117
ann,
121118
}
@@ -125,17 +122,13 @@ impl<'a> State<'a> {
125122
pub fn to_string<F>(ann: &dyn PpAnn, f: F) -> String
126123
where F: FnOnce(&mut State<'_>)
127124
{
128-
let mut wr = String::new();
129-
{
130-
let mut printer = State {
131-
s: pp::mk_printer(&mut wr),
132-
comments: None,
133-
ann,
134-
};
135-
f(&mut printer);
136-
printer.s.eof();
137-
}
138-
wr
125+
let mut printer = State {
126+
s: pp::mk_printer(),
127+
comments: None,
128+
ann,
129+
};
130+
f(&mut printer);
131+
printer.s.eof()
139132
}
140133

141134
pub fn visibility_qualified<S: Into<Cow<'static, str>>>(vis: &hir::Visibility, w: S) -> String {

src/librustc_driver/pretty.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,6 @@ pub fn print_after_hir_lowering<'tcx>(
814814
&sess.parse_sess,
815815
src_name,
816816
src,
817-
out,
818817
annotation.pp_ann());
819818
for node_id in uii.all_matching_node_ids(hir_map) {
820819
let hir_id = tcx.hir().node_to_hir_id(node_id);
@@ -826,7 +825,7 @@ pub fn print_after_hir_lowering<'tcx>(
826825
pp_state.synth_comment(path);
827826
pp_state.s.hardbreak();
828827
}
829-
pp_state.s.eof();
828+
*out = pp_state.s.eof();
830829
})
831830
}
832831

src/libsyntax/print/pp.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,13 @@ crate struct PrintStackElem {
235235

236236
const SIZE_INFINITY: isize = 0xffff;
237237

238-
pub fn mk_printer(out: &mut String) -> Printer<'_> {
238+
pub fn mk_printer() -> Printer {
239239
let linewidth = 78;
240240
// Yes 55, it makes the ring buffers big enough to never fall behind.
241241
let n: usize = 55 * linewidth;
242242
debug!("mk_printer {}", linewidth);
243243
Printer {
244-
out,
244+
out: String::new(),
245245
buf_max_len: n,
246246
margin: linewidth as isize,
247247
space: linewidth as isize,
@@ -258,8 +258,8 @@ pub fn mk_printer(out: &mut String) -> Printer<'_> {
258258
}
259259
}
260260

261-
pub struct Printer<'a> {
262-
out: &'a mut String,
261+
pub struct Printer {
262+
out: String,
263263
buf_max_len: usize,
264264
/// Width of lines we're constrained to
265265
margin: isize,
@@ -300,7 +300,7 @@ impl Default for BufEntry {
300300
}
301301
}
302302

303-
impl<'a> Printer<'a> {
303+
impl Printer {
304304
pub fn last_token(&mut self) -> Token {
305305
self.buf[self.right].token.clone()
306306
}
@@ -629,8 +629,9 @@ impl<'a> Printer<'a> {
629629
self.pretty_print_end()
630630
}
631631

632-
pub fn eof(&mut self) {
633-
self.pretty_print_eof()
632+
pub fn eof(mut self) -> String {
633+
self.pretty_print_eof();
634+
self.out
634635
}
635636

636637
pub fn word<S: Into<Cow<'static, str>>>(&mut self, wrd: S) {

src/libsyntax/print/pprust.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a> Comments<'a> {
8787
}
8888

8989
pub struct State<'a> {
90-
pub s: pp::Printer<'a>,
90+
pub s: pp::Printer,
9191
comments: Option<Comments<'a>>,
9292
ann: &'a (dyn PpAnn+'a),
9393
is_expanded: bool
@@ -104,9 +104,8 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
104104
input: String,
105105
ann: &'a dyn PpAnn,
106106
is_expanded: bool) -> String {
107-
let mut out = String::new();
108107
let mut s = State {
109-
s: pp::mk_printer(&mut out),
108+
s: pp::mk_printer(),
110109
comments: Some(Comments::new(cm, sess, filename, input)),
111110
ann,
112111
is_expanded,
@@ -133,25 +132,20 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
133132

134133
s.print_mod(&krate.module, &krate.attrs);
135134
s.print_remaining_comments();
136-
s.s.eof();
137-
out
135+
s.s.eof()
138136
}
139137

140138
pub fn to_string<F>(f: F) -> String where
141139
F: FnOnce(&mut State<'_>),
142140
{
143-
let mut wr = String::new();
144-
{
145-
let mut printer = State {
146-
s: pp::mk_printer(&mut wr),
147-
comments: None,
148-
ann: &NoAnn,
149-
is_expanded: false
150-
};
151-
f(&mut printer);
152-
printer.s.eof();
153-
}
154-
wr
141+
let mut printer = State {
142+
s: pp::mk_printer(),
143+
comments: None,
144+
ann: &NoAnn,
145+
is_expanded: false
146+
};
147+
f(&mut printer);
148+
printer.s.eof()
155149
}
156150

157151
fn binop_to_string(op: BinOpToken) -> &'static str {
@@ -439,7 +433,7 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String {
439433
}
440434

441435
pub trait PrintState<'a> {
442-
fn writer(&mut self) -> &mut pp::Printer<'a>;
436+
fn writer(&mut self) -> &mut pp::Printer;
443437
fn comments(&mut self) -> &mut Option<Comments<'a>>;
444438

445439
fn word_space<S: Into<Cow<'static, str>>>(&mut self, w: S) {
@@ -760,7 +754,7 @@ pub trait PrintState<'a> {
760754
}
761755

762756
impl<'a> PrintState<'a> for State<'a> {
763-
fn writer(&mut self) -> &mut pp::Printer<'a> {
757+
fn writer(&mut self) -> &mut pp::Printer {
764758
&mut self.s
765759
}
766760

0 commit comments

Comments
 (0)