Skip to main content

BufferedQueue

Struct BufferedQueue 

Source
pub struct BufferedQueue<S: NorFlash, C: CacheImpl, const RAM_BYTES: usize> { /* private fields */ }
Expand description

A write-buffered queue that accepts items into a RAM ring and drains them to NOR flash.

§Type parameters

  • S: NOR flash driver implementing NorFlash.
  • C: Cache implementation from crate::cache.
  • RAM_BYTES: Capacity of the RAM ring buffer in bytes (includes 2-byte per-item overhead).

§Usage pattern

// Fast path — called from a tight loop:
queue.enqueue(&sample, OverflowPolicy::Err)?;

// Slow path — called from a lower-priority task or on a timer:
queue.drain_all(&mut scratch, false).await?;

// Read path — flash items are popped first, then RAM items:
if let Some(data) = queue.pop(&mut buf).await? {
    // process data
}

For ISR-safe use, enable the shared-ram-ring feature and use [SharedRamRing] instead.

Implementations§

Source§

impl<S: NorFlash, C: CacheImpl, const RAM_BYTES: usize> BufferedQueue<S, C, RAM_BYTES>

Source

pub fn new(storage: QueueStorage<S, C>) -> Self

Wrap an existing QueueStorage with a RAM ring buffer.

Source

pub fn enqueue(&mut self, data: &[u8], policy: OverflowPolicy) -> Result<(), ()>

Enqueue an item into the RAM ring buffer.

This is synchronous and never touches flash. When the ring is full the behaviour is determined by policy: return Err(()) or evict the oldest item.

Source

pub async fn drain_one( &mut self, scratch: &mut [u8], allow_overwrite: bool, ) -> Result<bool, Error<S::Error>>

Drain one item from the RAM ring to flash.

scratch is caller-provided temporary storage; it must be at least as large as the oldest pending item.

Returns Ok(true) if an item was committed to flash, Ok(false) if the ring was empty.

Source

pub async fn drain_all( &mut self, scratch: &mut [u8], allow_overwrite: bool, ) -> Result<(), Error<S::Error>>

Drain all RAM-buffered items to flash.

scratch must be large enough for the largest pending item.

Source

pub async fn pop<'d>( &mut self, data_buffer: &'d mut [u8], ) -> Result<Option<&'d mut [u8]>, Error<S::Error>>

Pop the oldest item from the queue.

Flash items are always older than RAM items, so flash is read first. If flash is empty the oldest item is taken directly from the RAM ring without writing to flash.

Source

pub async fn peek<'d>( &mut self, data_buffer: &'d mut [u8], ) -> Result<Option<&'d mut [u8]>, Error<S::Error>>

Peek at the oldest item without removing it.

Flash items are always older than RAM items, so flash is read first. If flash is empty the oldest item is read directly from the RAM ring without writing to flash.

Source

pub const fn ram_capacity_bytes() -> usize

Total capacity of the RAM ring in bytes (including 2-byte per-item length prefixes).

Source

pub fn ram_free_bytes(&self) -> usize

Free bytes remaining in the RAM ring.

Source

pub fn ram_pending_count(&self) -> usize

Number of items currently buffered in RAM (not yet committed to flash).

Source

pub fn ram_bytes_used(&self) -> usize

Bytes currently occupied in the RAM ring (including 2-byte per-item length prefixes).

Source

pub fn into_storage(self) -> QueueStorage<S, C>

Consume this BufferedQueue and return the inner QueueStorage.

Any items still in the RAM ring are discarded.

Auto Trait Implementations§

§

impl<S, C, const RAM_BYTES: usize> Freeze for BufferedQueue<S, C, RAM_BYTES>
where S: Freeze, C: Freeze,

§

impl<S, C, const RAM_BYTES: usize> RefUnwindSafe for BufferedQueue<S, C, RAM_BYTES>

§

impl<S, C, const RAM_BYTES: usize> Send for BufferedQueue<S, C, RAM_BYTES>
where S: Send, C: Send,

§

impl<S, C, const RAM_BYTES: usize> Sync for BufferedQueue<S, C, RAM_BYTES>
where S: Sync, C: Sync,

§

impl<S, C, const RAM_BYTES: usize> Unpin for BufferedQueue<S, C, RAM_BYTES>
where S: Unpin, C: Unpin,

§

impl<S, C, const RAM_BYTES: usize> UnsafeUnpin for BufferedQueue<S, C, RAM_BYTES>
where S: UnsafeUnpin, C: UnsafeUnpin,

§

impl<S, C, const RAM_BYTES: usize> UnwindSafe for BufferedQueue<S, C, RAM_BYTES>
where S: UnwindSafe, C: UnwindSafe,

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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.