Skip to main content

Crate embedded_buffer_pool

Crate embedded_buffer_pool 

Source
Expand description

Fixed-size BufferPool for no_std firmware using embassy_sync.

The pool holds N values of type T (1..=32 slots). A bitmask tracks which slots are free; BufferGuard and MappedBufferGuard return a slot to the pool on Drop. When the pool is empty, BufferPool::take registers a waker and completes when another task releases a buffer.

§Requirements

  • Acquire APIs take &'static self: the pool is intended to live in a static item.
  • Choose a RawMutex (M) compatible with your executor (for example the critical-section mutex in embassy_sync).
  • To build the backing [T; N] in a const context, use [expr; N] when that is valid for your type, or array_new! when you need distinct elements or T is not Copy (repeat-array syntax requires Copy in the general case).

§Example

use embedded_buffer_pool::BufferPool;
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;

/// Shared pool: two 64-byte packet buffers.
static POOL: BufferPool<CriticalSectionRawMutex, [u8; 64], 2> =
    BufferPool::new([[0u8; 64]; 2]);

fn try_fill() -> Option<()> {
    let mut guard = POOL.try_take()?;
    guard[0] = 0xAA;
    Some(())
}

async fn wait_for_buffer() {
    let mut guard = POOL.take().await;
    guard[0] = 0xBB;
}

For arrays built by repeating an expression (constructors, const values, literals), see array_new.

Macros§

array_new
Builds [T; N] by repeating the same expression N times in the source.

Structs§

BufferGuard
Exclusive handle to one pooled T. Releasing the slot on Drop.
BufferPool
Pool of N buffers of type T, synchronized with mutex M.
MappedBufferGuard
Like BufferGuard, but derefs to a borrowed U (often a slice or inner field).