#![no_std]
pub use bytes::Bytes;
pub use bytestring::ByteString;
use bytes::BytesMut;
#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BytesPool {
inner: BytesMut,
}
impl BytesPool {
#[inline]
pub fn new() -> Self {
Self {
inner: BytesMut::new(),
}
}
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self {
inner: BytesMut::with_capacity(capacity),
}
}
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
#[inline]
pub fn share_bytes(&mut self, bytes: &[u8]) -> Bytes {
self.inner.extend_from_slice(bytes);
self.inner.split().freeze()
}
#[inline]
pub fn share_str(&mut self, s: &str) -> ByteString {
let bytes = self.share_bytes(s.as_bytes());
unsafe { ByteString::from_bytes_unchecked(bytes) }
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional);
}
#[inline]
#[must_use = "consider BytesPool::reserve if you need an infallible reservation"]
pub fn try_reclaim(&mut self, additional: usize) -> bool {
self.inner.try_reclaim(additional)
}
}