diff options
| author | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-07-09 09:26:50 -0400 |
|---|---|---|
| committer | Mark Rousskov <mark.simulacrum@gmail.com> | 2019-07-10 07:13:20 -0400 |
| commit | cab453250a3ceae5cf0cf7eac836c03b37e4ca8e (patch) | |
| tree | fc27173efd1862f9117f140a2adaef9cb216c524 /src/libsyntax/print | |
| parent | e91dbc5916f508849532d1c7b3069b6c0c3a609b (diff) | |
| download | rust-cab453250a3ceae5cf0cf7eac836c03b37e4ca8e.tar.gz rust-cab453250a3ceae5cf0cf7eac836c03b37e4ca8e.zip | |
Move pp::Printer helpers to direct impl
Diffstat (limited to 'src/libsyntax/print')
| -rw-r--r-- | src/libsyntax/print/helpers.rs | 34 | ||||
| -rw-r--r-- | src/libsyntax/print/pp.rs | 6 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 54 |
3 files changed, 49 insertions, 45 deletions
diff --git a/src/libsyntax/print/helpers.rs b/src/libsyntax/print/helpers.rs new file mode 100644 index 00000000000..3449e07f456 --- /dev/null +++ b/src/libsyntax/print/helpers.rs @@ -0,0 +1,34 @@ +use std::borrow::Cow; +use crate::print::pp::Printer; + +impl Printer { + pub fn word_space<W: Into<Cow<'static, str>>>(&mut self, w: W) { + self.word(w); + self.space(); + } + + pub fn popen(&mut self) { + self.word("("); + } + + pub fn pclose(&mut self) { + self.word(")"); + } + + pub fn hardbreak_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { + self.hardbreak() + } + } + + pub fn space_if_not_bol(&mut self) { + if !self.is_beginning_of_line() { self.space(); } + } + + pub fn nbsp(&mut self) { self.word(" ") } + + pub fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) { + self.word(w); + self.nbsp() + } +} diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index be5a0098578..660e77f77d0 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -597,7 +597,7 @@ impl Printer { // Convenience functions to talk to the printer. /// "raw box" - crate fn rbox(&mut self, indent: usize, b: Breaks) { + pub fn rbox(&mut self, indent: usize, b: Breaks) { self.scan_begin(BeginToken { offset: indent as isize, breaks: b @@ -605,7 +605,7 @@ impl Printer { } /// Inconsistent breaking box - crate fn ibox(&mut self, indent: usize) { + pub fn ibox(&mut self, indent: usize) { self.rbox(indent, Breaks::Inconsistent) } @@ -621,7 +621,7 @@ impl Printer { }) } - crate fn end(&mut self) { + pub fn end(&mut self) { self.scan_end() } diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 288417fcd89..98c629addc8 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -432,37 +432,22 @@ fn visibility_qualified(vis: &ast::Visibility, s: &str) -> String { format!("{}{}", to_string(|s| s.print_visibility(vis)), s) } -pub trait PrintState<'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) { - self.writer().word(w); - self.writer().space() - } - - fn popen(&mut self) { self.writer().word("(") } - - fn pclose(&mut self) { self.writer().word(")") } - - fn hardbreak_if_not_bol(&mut self) { - if !self.writer().is_beginning_of_line() { - self.writer().hardbreak() - } - } - - // "raw box" - fn rbox(&mut self, u: usize, b: pp::Breaks) { - self.writer().rbox(u, b) +impl std::ops::Deref for State<'_> { + type Target = pp::Printer; + fn deref(&self) -> &Self::Target { + &self.s } +} - fn ibox(&mut self, u: usize) { - self.writer().ibox(u); +impl std::ops::DerefMut for State<'_> { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.s } +} - fn end(&mut self) { - self.writer().end() - } +pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefMut { + fn writer(&mut self) -> &mut pp::Printer; + fn comments(&mut self) -> &mut Option<Comments<'a>>; fn commasep<T, F>(&mut self, b: Breaks, elts: &[T], mut op: F) where F: FnMut(&mut Self, &T), @@ -728,12 +713,6 @@ pub trait PrintState<'a> { } self.end(); } - - fn space_if_not_bol(&mut self) { - if !self.writer().is_beginning_of_line() { self.writer().space(); } - } - - fn nbsp(&mut self) { self.writer().word(" ") } } impl<'a> PrintState<'a> for State<'a> { @@ -747,15 +726,6 @@ impl<'a> PrintState<'a> for State<'a> { } impl<'a> State<'a> { - pub fn cbox(&mut self, u: usize) { - self.s.cbox(u); - } - - crate fn word_nbsp<S: Into<Cow<'static, str>>>(&mut self, w: S) { - self.s.word(w); - self.nbsp() - } - crate fn head<S: Into<Cow<'static, str>>>(&mut self, w: S) { let w = w.into(); // outer-box is consistent |
