Skip to main content

Module buffer

Module buffer 

Source
Expand description

Circular (ring) buffer implementation with secure memory management.

§Memory Safety

  • Memory is lazily allocated on first write.
  • All data is securely zeroed on drop via zeroize.
  • free() and burn_free() provide explicit cleanup for session termination.

§Performance

  • Power-of-2 sizes use bitwise AND for wrapping (preferred).
  • Non-power-of-2 sizes use standard modulo.
  • write() is a single-pass, at-most-two-copy_from_slice path — no per-byte loop.
  • Borrow conflicts are avoided by extracting Copy fields into locals before borrowing self.data, so Rust’s field-splitting NLL is never needed.

§Fixed Issues (vs original)

  • write_ptr used to panic when a write crossed the ring boundary. It now returns BufferError::WouldWrap so callers can fall back to write(). A new write_slices_mut method gives true zero-copy two-part access.
  • read and write computed positions by calling self.wrap_pos() while holding a borrow on self.data, causing borrow-checker issues. Both now use local copies of size/is_pow2 and inline the arithmetic.
  • write looped over the data in chunks even when a simple two-segment copy_from_slice suffices — replaced with a branch-free two-copy path.

Structs§

CircularBuffer
A circular (ring) buffer for efficient streaming data.