2023-08-28 01:53:15 +02:00
|
|
|
use core::ops::Range;
|
|
|
|
|
2022-08-03 13:55:46 +02:00
|
|
|
pub struct RingBuffer<const N: usize> {
|
|
|
|
start: usize,
|
|
|
|
end: usize,
|
|
|
|
empty: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<const N: usize> RingBuffer<N> {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
start: 0,
|
|
|
|
end: 0,
|
|
|
|
empty: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-28 01:53:15 +02:00
|
|
|
pub fn push_buf(&mut self) -> Range<usize> {
|
2022-08-03 13:55:46 +02:00
|
|
|
if self.start == self.end && !self.empty {
|
|
|
|
trace!(" ringbuf: push_buf empty");
|
2023-08-28 01:53:15 +02:00
|
|
|
return 0..0;
|
2022-08-03 13:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let n = if self.start <= self.end {
|
2023-08-28 01:53:15 +02:00
|
|
|
N - self.end
|
2022-08-03 13:55:46 +02:00
|
|
|
} else {
|
|
|
|
self.start - self.end
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!(" ringbuf: push_buf {:?}..{:?}", self.end, self.end + n);
|
2023-08-28 01:53:15 +02:00
|
|
|
self.end..self.end + n
|
2022-08-03 13:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, n: usize) {
|
|
|
|
trace!(" ringbuf: push {:?}", n);
|
|
|
|
if n == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.end = self.wrap(self.end + n);
|
|
|
|
self.empty = false;
|
|
|
|
}
|
|
|
|
|
2023-08-28 01:53:15 +02:00
|
|
|
pub fn pop_buf(&mut self) -> Range<usize> {
|
2022-08-03 13:55:46 +02:00
|
|
|
if self.empty {
|
|
|
|
trace!(" ringbuf: pop_buf empty");
|
2023-08-28 01:53:15 +02:00
|
|
|
return 0..0;
|
2022-08-03 13:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let n = if self.end <= self.start {
|
2023-08-28 01:53:15 +02:00
|
|
|
N - self.start
|
2022-08-03 13:55:46 +02:00
|
|
|
} else {
|
|
|
|
self.end - self.start
|
|
|
|
};
|
|
|
|
|
|
|
|
trace!(" ringbuf: pop_buf {:?}..{:?}", self.start, self.start + n);
|
2023-08-28 01:53:15 +02:00
|
|
|
self.start..self.start + n
|
2022-08-03 13:55:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn pop(&mut self, n: usize) {
|
|
|
|
trace!(" ringbuf: pop {:?}", n);
|
|
|
|
if n == 0 {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.start = self.wrap(self.start + n);
|
|
|
|
self.empty = self.start == self.end;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_full(&self) -> bool {
|
|
|
|
self.start == self.end && !self.empty
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.empty
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(unused)]
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
if self.empty {
|
|
|
|
0
|
|
|
|
} else if self.start < self.end {
|
|
|
|
self.end - self.start
|
|
|
|
} else {
|
|
|
|
N + self.end - self.start
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.start = 0;
|
|
|
|
self.end = 0;
|
|
|
|
self.empty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap(&self, n: usize) -> usize {
|
2023-08-28 01:53:15 +02:00
|
|
|
assert!(n <= N);
|
|
|
|
if n == N {
|
2022-08-03 13:55:46 +02:00
|
|
|
0
|
|
|
|
} else {
|
|
|
|
n
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn push_pop() {
|
|
|
|
let mut rb: RingBuffer<4> = RingBuffer::new();
|
|
|
|
let buf = rb.push_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(0..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
rb.push(4);
|
|
|
|
|
|
|
|
let buf = rb.pop_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(0..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
rb.pop(1);
|
|
|
|
|
|
|
|
let buf = rb.pop_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(1..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
rb.pop(1);
|
|
|
|
|
|
|
|
let buf = rb.pop_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(2..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
rb.pop(1);
|
|
|
|
|
|
|
|
let buf = rb.pop_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(3..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
rb.pop(1);
|
|
|
|
|
|
|
|
let buf = rb.pop_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(0..0, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
|
|
|
|
let buf = rb.push_buf();
|
2023-08-28 01:53:15 +02:00
|
|
|
assert_eq!(0..4, buf);
|
2022-08-03 13:55:46 +02:00
|
|
|
}
|
|
|
|
}
|