pub struct BufferPool { /* private fields */ }Expand description
A pool of reusable byte buffers.
Each buffer starts at chunk_size bytes and grows as needed.
When returned via give_back, the buffer is reset (length = 0)
and made available for the next take() call.
Implementations§
Source§impl BufferPool
impl BufferPool
Sourcepub fn new(chunk_size: usize, max_idle: usize) -> Self
pub fn new(chunk_size: usize, max_idle: usize) -> Self
Create a new buffer pool.
chunk_size— minimum capacity per buffer (default 4096).max_idle— max number of buffers to keep in the pool (default 64).
Sourcepub fn take(&self) -> Vec<u8> ⓘ
pub fn take(&self) -> Vec<u8> ⓘ
Take a buffer from the pool, or allocate a fresh one.
The returned buffer has length 0 and capacity >= chunk_size.
Sourcepub fn take_sized(&self, min_capacity: usize) -> Vec<u8> ⓘ
pub fn take_sized(&self, min_capacity: usize) -> Vec<u8> ⓘ
Take a buffer pre-sized to at least min_capacity.
Sourcepub fn give_back(&self, buf: Vec<u8>)
pub fn give_back(&self, buf: Vec<u8>)
Return a buffer to the pool for reuse.
The buffer is cleared (length set to 0) but its allocation is kept.
Sourcepub fn idle_count(&self) -> usize
pub fn idle_count(&self) -> usize
Number of buffers currently idle in the pool.
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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.