pub struct BufferPool { /* private fields */ }Expand description
A pool of reusable, aligned buffers.
Buffers are organized into power-of-two size classes. When a buffer is requested, the smallest size class that fits is used. Buffers are automatically returned to the pool when dropped.
§Alignment
Buffer alignment is guaranteed only at the base pointer (when cursor == 0).
After calling Buf::advance, the pointer returned by as_mut_ptr() may
no longer be aligned. For direct I/O operations that require alignment,
do not advance the buffer before use.
Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn try_alloc(&self, capacity: usize) -> Result<IoBufMut, PoolError>
pub fn try_alloc(&self, capacity: usize) -> Result<IoBufMut, PoolError>
Attempts to allocate a pooled buffer.
Unlike Self::alloc, this method does not fall back to untracked
allocation.
The returned buffer has len() == 0 and capacity() >= capacity.
§Initialization
The returned buffer contains uninitialized memory. Do not read from it until data has been written.
§Errors
PoolError::Oversized:capacityexceedsmax_sizePoolError::Exhausted: Pool exhausted for required size class
Sourcepub fn alloc(&self, capacity: usize) -> IoBufMut
pub fn alloc(&self, capacity: usize) -> IoBufMut
Allocates a buffer with capacity for at least capacity bytes.
The returned buffer has len() == 0 and capacity() >= capacity,
matching the semantics of IoBufMut::with_capacity and
bytes::BytesMut::with_capacity. Use BufMut::put_slice or other
BufMut methods to write data to the buffer.
If the pool can provide a buffer (capacity within limits and pool not exhausted), returns a pooled buffer that will be returned to the pool when dropped. Otherwise, falls back to an untracked aligned heap allocation that is deallocated when dropped.
Use Self::try_alloc if you need pooled-only behavior.
§Initialization
The returned buffer contains uninitialized memory. Do not read from it until data has been written.
Sourcepub unsafe fn alloc_len(&self, len: usize) -> IoBufMut
pub unsafe fn alloc_len(&self, len: usize) -> IoBufMut
Allocates a buffer and sets its readable length to len without
initializing bytes.
Equivalent to Self::alloc followed by IoBufMut::set_len.
§Safety
Caller must ensure all bytes are initialized before any read operation.
Sourcepub fn try_alloc_zeroed(&self, len: usize) -> Result<IoBufMut, PoolError>
pub fn try_alloc_zeroed(&self, len: usize) -> Result<IoBufMut, PoolError>
Attempts to allocate a zero-initialized pooled buffer.
Unlike Self::alloc_zeroed, this method does not fall back to
untracked allocation.
The returned buffer has len() == len and capacity() >= len.
§Initialization
Bytes in 0..len are initialized to zero. Bytes in len..capacity
may be uninitialized.
§Errors
PoolError::Oversized:lenexceedsmax_sizePoolError::Exhausted: Pool exhausted for required size class
Sourcepub fn alloc_zeroed(&self, len: usize) -> IoBufMut
pub fn alloc_zeroed(&self, len: usize) -> IoBufMut
Allocates a zero-initialized buffer with readable length len.
The returned buffer has len() == len and capacity() >= len.
If the pool can provide a buffer (len within limits and pool not exhausted), returns a pooled buffer that will be returned to the pool when dropped. Otherwise, falls back to an untracked aligned heap allocation that is deallocated when dropped.
Use this for read APIs that require an initialized &mut [u8].
This avoids unsafe set_len at callsites.
Use Self::try_alloc_zeroed if you need pooled-only behavior.
§Initialization
Bytes in 0..len are initialized to zero. Bytes in len..capacity
may be uninitialized.
Sourcepub fn config(&self) -> &BufferPoolConfig
pub fn config(&self) -> &BufferPoolConfig
Returns the pool configuration.
Trait Implementations§
Source§impl Clone for BufferPool
impl Clone for BufferPool
Source§fn clone(&self) -> BufferPool
fn clone(&self) -> BufferPool
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto 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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more