pub struct CircularBuffer<T, const N: usize> { /* private fields */ }Expand description
A fixed-capacity circular buffer.
The buffer has the following properties:
- No restrictions on a power of two on the value of N.
- The full size of buffer is used (no wasted space).
- When the buffer is full, writing will return an error until the buffer is read from.
§Invariants
For a buffer with N > 0, the following invariants are maintained:
0 <= self.len <= N: The number of elements is always between 0 and the capacity, inclusive.0 <= self.head < N: The head index is always within the array bounds.0 <= self.tail < N: The tail index is always within the array bounds.self.head == (self.tail + self.len) % N: The head is advanced from the tail by the length, modulo N.- The
self.lenpopulated elements are stored inbufferat indices(self.tail) % N,(self.tail + 1) % N, …,(self.tail + self.len - 1) % N.
The case N = 0 is valid and handled; push_back will always return
Error::ResourceExhausted and pop_front will always return None,
preventing any out-of-bounds access or division by zero.