diff options
| author | David Tolnay <dtolnay@gmail.com> | 2022-01-18 19:07:12 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2022-01-18 19:07:12 -0800 |
| commit | 50d722a6917eb150362ff05e4881e63b3e7adc62 (patch) | |
| tree | 662c5141f3c8437c3c3b80de52625aafc12ddaa1 | |
| parent | e219b2b5f90bf48bf56533c16745f2ce85216a2c (diff) | |
| download | rust-50d722a6917eb150362ff05e4881e63b3e7adc62.tar.gz rust-50d722a6917eb150362ff05e4881e63b3e7adc62.zip | |
Simplify ring buffer pushes
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pp.rs | 11 | ||||
| -rw-r--r-- | compiler/rustc_ast_pretty/src/pp/ring.rs | 8 |
2 files changed, 12 insertions, 7 deletions
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 @@ impl Printer { 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 @@ impl Printer { 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 @@ impl Printer { 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 @@ impl<T> RingBuffer<T> { 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 @@ impl<T> RingBuffer<T> { self.offset += 1; } + pub fn clear(&mut self) { + self.data.clear(); + } + pub fn truncate(&mut self, len: usize) { self.data.truncate(len); } |
