diff --git a/compiler/rustc_ast_pretty/src/pp.rs b/compiler/rustc_ast_pretty/src/pp.rs index 4f5140349c9..43a3c792d4a 100644 --- a/compiler/rustc_ast_pretty/src/pp.rs +++ b/compiler/rustc_ast_pretty/src/pp.rs @@ -288,12 +288,11 @@ fn scan_begin(&mut self, b: BeginToken) { self.left_total = 1; self.right_total = 1; self.right = self.left; - self.buf.truncate(1); + self.buf.clear(); } else { self.right += 1; - self.buf.advance_right(); } - self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total }; + self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total }); self.scan_stack.push_front(self.right); } @@ -302,8 +301,7 @@ fn scan_end(&mut self) { self.print_end(); } else { self.right += 1; - self.buf.advance_right(); - self.buf[self.right] = BufEntry { token: Token::End, size: -1 }; + self.buf.push(BufEntry { token: Token::End, size: -1 }); self.scan_stack.push_front(self.right); } } @@ -329,9 +327,8 @@ fn scan_string(&mut self, s: Cow<'static, str>) { self.print_string(s); } else { self.right += 1; - self.buf.advance_right(); let len = s.len() as isize; - self.buf[self.right] = BufEntry { token: Token::String(s), size: len }; + self.buf.push(BufEntry { token: Token::String(s), size: len }); self.right_total += len; self.check_stream(); } diff --git a/compiler/rustc_ast_pretty/src/pp/ring.rs b/compiler/rustc_ast_pretty/src/pp/ring.rs index 7e4e353ef1f..94bb10382f8 100644 --- a/compiler/rustc_ast_pretty/src/pp/ring.rs +++ b/compiler/rustc_ast_pretty/src/pp/ring.rs @@ -22,6 +22,10 @@ pub fn new() -> Self { RingBuffer { data: VecDeque::new(), offset: 0 } } + pub fn push(&mut self, value: T) { + self.data.push_back(value); + } + pub fn advance_right(&mut self) where T: Default, @@ -34,6 +38,10 @@ pub fn advance_left(&mut self) { self.offset += 1; } + pub fn clear(&mut self) { + self.data.clear(); + } + pub fn truncate(&mut self, len: usize) { self.data.truncate(len); }