pub struct CallbackMemory<FReserve>{ /* private fields */ }Expand description
Implements MemoryShared by delegating to a closure.
This can be used to construct wrapping memory providers that add logic or configuration on top of an existing memory provider.
§Examples
Configure an inner memory provider with additional parameters:
use bytesbuf::mem::{CallbackMemory, GlobalPool, Memory};
// Create a callback memory that configures the inner provider.
let memory = CallbackMemory::new(move |min_len| {
// Apply custom configuration when reserving memory.
let page_aligned = true;
io_context.reserve_with_config(min_len, page_aligned)
});
let buf = memory.reserve(64);
assert!(buf.capacity() >= 64);For a complete implementation pattern, see examples/bb_has_memory_optimizing.rs.
Implementations§
Source§impl<FReserve> CallbackMemory<FReserve>
impl<FReserve> CallbackMemory<FReserve>
Sourcepub fn new(reserve_fn: FReserve) -> Self
pub fn new(reserve_fn: FReserve) -> Self
Creates a new instance implemented via the provided callback.
Sourcepub fn reserve(&self, min_bytes: usize) -> BytesBuf
pub fn reserve(&self, min_bytes: usize) -> BytesBuf
Reserves at least min_bytes bytes of memory capacity.
Returns an empty BytesBuf that can be used to fill the reserved memory with data.
The memory provider may provide more memory than requested.
The memory reservation request will always be fulfilled, obtaining more memory from the operating system if necessary.
§Zero-sized reservations
Reserving zero bytes of memory is a valid operation and will return a BytesBuf
with zero or more bytes of capacity.
§Panics
May panic if the operating system runs out of memory.