Change io crate & add a small shell
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
use core::mem::MaybeUninit;
|
||||
use core::{
|
||||
mem::MaybeUninit,
|
||||
ops::{Index, IndexMut},
|
||||
};
|
||||
|
||||
#[derive(Debug, Copy)]
|
||||
pub struct CircularBuffer<T, const SIZE: usize> {
|
||||
@@ -49,13 +52,36 @@ impl<T, const SIZE: usize> CircularBuffer<T, SIZE> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn push(&mut self, value: T) {
|
||||
pub fn push(&mut self, value: T) -> bool {
|
||||
self.buffer[self.tail] = MaybeUninit::new(value);
|
||||
self.tail = (self.tail + 1) % SIZE;
|
||||
if self.is_full() {
|
||||
self.head = (self.head + 1) % SIZE;
|
||||
true
|
||||
} else {
|
||||
self.len += 1;
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, const SIZE: usize> Index<usize> for CircularBuffer<T, SIZE> {
|
||||
type Output = T;
|
||||
|
||||
fn index(&self, index: usize) -> &Self::Output {
|
||||
if self.len() > index {
|
||||
unsafe { self.buffer[(self.head + index) % SIZE].assume_init_ref() }
|
||||
} else {
|
||||
panic!("index is bigger than circular buffer")
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T, const SIZE: usize> IndexMut<usize> for CircularBuffer<T, SIZE> {
|
||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
||||
if self.len() > index {
|
||||
unsafe { self.buffer[(self.head + index) % SIZE].assume_init_mut() }
|
||||
} else {
|
||||
panic!("index is bigger than circular buffer")
|
||||
}
|
||||
self.tail = (self.tail + 1) % SIZE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user