Skip to main content

BytesPool

Struct BytesPool 

Source
pub struct BytesPool { /* private fields */ }
Expand description

A pool for creating byte-slices and strings that can be cheaply cloned and shared across threads without allocating memory. Byte-slices are shared as Bytes, and strings are shared as ByteStrings.

Internally, a BytesPool is a wrapper around a BytesMut buffer from the bytes crate. It shares data by appending the data to its buffer and then splitting the buffer off with BytesMut::split. This only allocates memory if the buffer needs to resize.

Implementations§

Source§

impl BytesPool

Source

pub fn new() -> Self

Creates a new BytesPool with default capacity.

Resulting object has unspecified capacity. This function does not allocate.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::new();
Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new BytesPool with the specified capacity.

The returned BytesPool will be able to hold at least capacity bytes without reallocating.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::with_capacity(64);
Source

pub fn capacity(&self) -> usize

Returns the number of bytes the BytesPool can hold without reallocating.

§Examples
use bytes_pool::BytesPool;

let b = BytesPool::with_capacity(64);
assert_eq!(b.capacity(), 64);
Source

pub fn share_bytes(&mut self, bytes: &[u8]) -> Bytes

Creates an immutable slice of bytes that can be shared across threads and cheaply cloned.

No allocation is performed unless the internal buffer needs to be resized to accomodate the additional bytes.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::with_capacity(64);

let bytes = pool.share_bytes(b"hello world");

assert_eq!(bytes, &b"hello world"[..]);
Source

pub fn share_str(&mut self, s: &str) -> ByteString

Creates an immutable string that can be shared across threads and cheaply cloned.

No allocation is performed unless the internal buffer needs to be resized to accomodate the additional bytes.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::with_capacity(64);

let s = pool.share_str("hello world");

assert_eq!(s, "hello world");
Source

pub fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional bytes to be inserted into the given BytesPool.

More than additional bytes may be reserved in order to avoid frequent reallocations. A call to reserve may result in an allocation.

This function performs the same optimizations as BytesMut::reserve.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::with_capacity(128);
let bytes = pool.share_bytes(&[0; 64][..]);

assert_eq!(pool.capacity(), 64);

pool.reserve(128);

assert_eq!(pool.capacity(), 128);
§Panics

Panics if the new capacity overflows usize.

Source

pub fn try_reclaim(&mut self, additional: usize) -> bool

Attempts to cheaply reclaim already allocated capacity for at least additional more bytes to be inserted into the given BytesPool and returns true if it succeeded.

try_reclaim behaves exactly like reserve, except that it never allocates new storage and returns a bool indicating whether it was successful in doing so:

try_reclaim returns false under these conditions:

  • The spare capacity left is less than additional bytes AND
  • The existing allocation cannot be reclaimed cheaply or it was less than additional bytes in size

Reclaiming the allocation cheaply is possible if the BytesPool has no outstanding references through Bytes or ByteStrings which point to its underlying storage.

§Examples
use bytes_pool::BytesPool;

let mut pool = BytesPool::with_capacity(64);
assert_eq!(true, pool.try_reclaim(64));
assert_eq!(64, pool.capacity());

let mut bytes = pool.share_bytes(b"abcd");
assert_eq!(60, pool.capacity());
assert_eq!(false, pool.try_reclaim(64));
// pool has capacity for 60 bytes
assert_eq!(true, pool.try_reclaim(60));

drop(bytes);

assert_eq!(true, pool.try_reclaim(64));

Trait Implementations§

Source§

impl Clone for BytesPool

Source§

fn clone(&self) -> BytesPool

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BytesPool

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BytesPool

Source§

fn default() -> BytesPool

Returns the “default value” for a type. Read more
Source§

impl Hash for BytesPool

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for BytesPool

Source§

fn cmp(&self, other: &BytesPool) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for BytesPool

Source§

fn eq(&self, other: &BytesPool) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BytesPool

Source§

fn partial_cmp(&self, other: &BytesPool) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for BytesPool

Source§

impl StructuralPartialEq for BytesPool

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.