1pub struct Fifo<const N: usize> {
3 buf: [u32; N],
4 head: usize,
5 tail: usize,
6 size: usize,
7}
8
9impl<const N: usize> Default for Fifo<N> {
10 fn default() -> Self {
11 Self::new()
12 }
13}
14
15impl<const N: usize> Fifo<N> {
16 pub const fn new() -> Self {
17 Self {
18 buf: [0; N],
19 head: 0,
20 tail: 0,
21 size: N,
22 }
23 }
24
25 pub fn push(&mut self, data: u32) -> Result<(), &'static str> {
27 let next_head = (self.head + 1) % self.size;
28 if next_head == self.tail {
29 return Err("fifo full"); }
31 self.buf[self.head] = data;
32 self.head = next_head;
33 Ok(())
34 }
35
36 pub fn pop(&mut self) -> Option<u32> {
38 if self.head == self.tail {
39 return None; }
41 let data = self.buf[self.tail];
42 self.tail = (self.tail + 1) % self.size;
43 Some(data)
44 }
45
46 pub fn is_empty(&self) -> bool {
48 self.head == self.tail
49 }
50}