Skip to main content

RingBuffer

Struct RingBuffer 

Source
pub struct RingBuffer<T, const S: usize>
where T: Default + Copy,
{ /* private fields */ }

Implementations§

Source§

impl<T, const S: usize> RingBuffer<T, S>
where T: Default + Copy,

Source

pub fn new() -> Self

Creates a new instance of RingBuffer with a fixed size.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
Examples found in repository?
examples/ex.rs (line 5)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn push(&mut self, item: T) -> Result<(), RingBufferError>

Pushes an item into the ring buffer. Returns an error if the buffer is full.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(42).unwrap();
Examples found in repository?
examples/ex.rs (line 8)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn pop(&mut self) -> Option<T>

Pops an item from the ring buffer. Returns None if the buffer is empty.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(42).unwrap();
let item = rb.pop();
assert_eq!(item, Some(42));
Examples found in repository?
examples/ex.rs (line 21)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn clear(&mut self)

Clears the ring buffer, resetting its state.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(42).unwrap();
rb.clear();
assert!(rb.is_empty());
Source

pub fn pop_continuous(&mut self, count: usize) -> Result<usize, RingBufferError>

Pops a specified number of items from the ring buffer. Returns an error if the count exceeds the current length of the buffer.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(1).unwrap();
rb.push(2).unwrap();
let remaining_capacity = rb.pop_continuous(1).unwrap();
assert_eq!(remaining_capacity, 4095);
Source

pub fn capacity(&self) -> usize

Returns the capacity of the ring buffer.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
assert_eq!(rb.capacity(), 4096);
Source

pub fn remaining_capacity(&self) -> usize

Returns the remaining capacity of the ring buffer.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
assert_eq!(rb.remaining_capacity(), 4096);
rb.push(1).unwrap();
assert_eq!(rb.remaining_capacity(), 4095);
Source

pub fn is_empty(&self) -> bool

Checks if the ring buffer is empty.

Examples found in repository?
examples/ex.rs (line 24)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn is_full(&self) -> bool

Checks if the ring buffer is full.

Source

pub fn len(&self) -> usize

Returns the current length of the ring buffer.

Examples found in repository?
examples/ex.rs (line 10)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn write(&mut self, data: &[T]) -> Result<(), RingBufferError>

Writes a slice of data into the ring buffer. Returns an error if the buffer is full.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.write(&[1, 2, 3, 4]).unwrap();
Source

pub fn read(&mut self, buffer: &mut [T]) -> usize

Reads data from the ring buffer into a provided mutable slice. Returns the number of items read. If the buffer has fewer items than the slice, it fills as many as possible.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(1).unwrap();
rb.push(2).unwrap();
let mut buffer = [0; 5];
let read_count = rb.read(&mut buffer);
assert_eq!(read_count, 2);
assert_eq!(&buffer[..read_count], &[1, 2]);
Examples found in repository?
examples/ex.rs (line 13)
3fn main() {
4    const SIZE: usize = 4096; // 4KB buffer
5    let mut rb: RingBuffer<u32, SIZE> = RingBuffer::new();
6
7    for i in 0..SIZE {
8        rb.push(i as u32).unwrap();
9    }
10    println!("len={}", rb.len());
11
12    let mut buffer = [0; 5];
13    let _ = rb.read(&mut buffer);
14    println!("data={:?}", buffer);
15
16    let _ = rb.read(&mut buffer);
17    println!("data={:?}", buffer);
18    println!("len={}", rb.len());
19
20    for _ in 0..rb.len() {
21        rb.pop();
22    }
23
24    println!("is empty={}", rb.is_empty());
25}
Source

pub fn read_slice(&self, len: usize) -> Result<&[T], RingBufferError>

Reads a slice of data from the ring buffer. Returns an error if the requested length exceeds the current length of the buffer. If the data is contiguous, it returns a slice; otherwise, it returns an error.

use ring_buffer_no_std::RingBuffer;
let mut rb: RingBuffer<u32, 4096> = RingBuffer::new();
rb.push(1).unwrap();
rb.push(2).unwrap();
let slice = rb.read_slice(2).unwrap();
assert_eq!(slice, &[1, 2]);

Auto Trait Implementations§

§

impl<T, const S: usize> Freeze for RingBuffer<T, S>
where [T; S]: Freeze,

§

impl<T, const S: usize> RefUnwindSafe for RingBuffer<T, S>

§

impl<T, const S: usize> Send for RingBuffer<T, S>
where [T; S]: Send,

§

impl<T, const S: usize> Sync for RingBuffer<T, S>
where [T; S]: Sync,

§

impl<T, const S: usize> Unpin for RingBuffer<T, S>
where [T; S]: Unpin,

§

impl<T, const S: usize> UnsafeUnpin for RingBuffer<T, S>

§

impl<T, const S: usize> UnwindSafe for RingBuffer<T, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.