pub struct BufferPool { /* private fields */ }Expand description
A lock-free pool of pre-allocated receive buffers.
BufferPool avoids heap allocation in the hot path by recycling
bytes::BytesMut buffers via a bounded, lock-free queue.
When the pool is exhausted, BufferPool::get falls back to allocating a
new zeroed buffer — the pool never blocks.
§Example
use binger_udp::BufferPool;
let pool = BufferPool::new(16, 2048);
let mut buf = pool.get();
buf.extend_from_slice(b"hello");
pool.put(buf);
assert_eq!(pool.available(), 1);Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn new(capacity: usize, buf_size: usize) -> Self
pub fn new(capacity: usize, buf_size: usize) -> Self
Creates a new buffer pool with capacity buffers, each of buf_size
bytes.
Buffers are lazily allocated — the constructor itself does not allocate.
Sourcepub fn get(&self) -> BytesMut
pub fn get(&self) -> BytesMut
Retrieves a buffer from the pool, or allocates a new zeroed one if the pool is empty.
Returned buffers are cleared before being handed out.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for BufferPool
impl RefUnwindSafe for BufferPool
impl Send for BufferPool
impl Sync for BufferPool
impl Unpin for BufferPool
impl UnsafeUnpin for BufferPool
impl UnwindSafe for BufferPool
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more