Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 50d722a

Browse files
committed
Simplify ring buffer pushes
1 parent e219b2b commit 50d722a

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

compiler/rustc_ast_pretty/src/pp.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,11 @@ impl Printer {
288288
self.left_total = 1;
289289
self.right_total = 1;
290290
self.right = self.left;
291-
self.buf.truncate(1);
291+
self.buf.clear();
292292
} else {
293293
self.right += 1;
294-
self.buf.advance_right();
295294
}
296-
self.buf[self.right] = BufEntry { token: Token::Begin(b), size: -self.right_total };
295+
self.buf.push(BufEntry { token: Token::Begin(b), size: -self.right_total });
297296
self.scan_stack.push_front(self.right);
298297
}
299298

@@ -302,8 +301,7 @@ impl Printer {
302301
self.print_end();
303302
} else {
304303
self.right += 1;
305-
self.buf.advance_right();
306-
self.buf[self.right] = BufEntry { token: Token::End, size: -1 };
304+
self.buf.push(BufEntry { token: Token::End, size: -1 });
307305
self.scan_stack.push_front(self.right);
308306
}
309307
}
@@ -329,9 +327,8 @@ impl Printer {
329327
self.print_string(s);
330328
} else {
331329
self.right += 1;
332-
self.buf.advance_right();
333330
let len = s.len() as isize;
334-
self.buf[self.right] = BufEntry { token: Token::String(s), size: len };
331+
self.buf.push(BufEntry { token: Token::String(s), size: len });
335332
self.right_total += len;
336333
self.check_stream();
337334
}

compiler/rustc_ast_pretty/src/pp/ring.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ impl<T> RingBuffer<T> {
2222
RingBuffer { data: VecDeque::new(), offset: 0 }
2323
}
2424

25+
pub fn push(&mut self, value: T) {
26+
self.data.push_back(value);
27+
}
28+
2529
pub fn advance_right(&mut self)
2630
where
2731
T: Default,
@@ -34,6 +38,10 @@ impl<T> RingBuffer<T> {
3438
self.offset += 1;
3539
}
3640

41+
pub fn clear(&mut self) {
42+
self.data.clear();
43+
}
44+
3745
pub fn truncate(&mut self, len: usize) {
3846
self.data.truncate(len);
3947
}

0 commit comments

Comments
 (0)