Skip to content

Commit 2884718

Browse files
authored
Merge pull request #8895 from asder8215/feature/ringbuffer
uucore: ringbuffer pre-allocates memory based on size given
2 parents 36ca7bc + 2ba4e39 commit 2884718

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

src/uucore/src/lib/features/ringbuffer.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,13 @@ use std::collections::VecDeque;
4343
pub struct RingBuffer<T> {
4444
/// The data stored in the ring buffer.
4545
pub data: VecDeque<T>,
46-
47-
/// The maximum number of elements that the ring buffer can hold.
48-
size: usize,
4946
}
5047

5148
impl<T> RingBuffer<T> {
5249
/// Create a new ring buffer with a maximum size of `size`.
5350
pub fn new(size: usize) -> Self {
5451
Self {
55-
data: VecDeque::new(),
56-
size,
52+
data: VecDeque::with_capacity(size),
5753
}
5854
}
5955

@@ -100,10 +96,10 @@ impl<T> RingBuffer<T> {
10096
/// assert_eq!(Some(2), buf.push_back(2));
10197
/// ```
10298
pub fn push_back(&mut self, value: T) -> Option<T> {
103-
if self.size == 0 {
99+
if self.data.capacity() == 0 {
104100
return Some(value);
105101
}
106-
let result = if self.size <= self.data.len() {
102+
let result = if self.data.len() == self.data.capacity() {
107103
self.data.pop_front()
108104
} else {
109105
None

0 commit comments

Comments
 (0)