use std::{borrow::Borrow, borrow::BorrowMut, fmt, io, ops::Deref, ops::DerefMut, ptr};
use crate::{Buf, BufMut, Bytes, buf::IntoIter, buf::UninitSlice, storage::StorageVec};
pub struct BytesMut {
pub(crate) storage: StorageVec,
}
impl BytesMut {
#[inline]
#[must_use]
pub fn with_capacity(capacity: usize) -> BytesMut {
BytesMut {
storage: StorageVec::with_capacity(capacity),
}
}
#[inline]
#[must_use]
pub fn copy_from_slice<T: AsRef<[u8]>>(src: T) -> Self {
let slice = src.as_ref();
BytesMut {
storage: StorageVec::from_slice(slice.len(), slice),
}
}
#[inline]
#[must_use]
pub fn new() -> BytesMut {
BytesMut {
storage: StorageVec::with_capacity(crate::storage::MIN_CAPACITY),
}
}
#[inline]
pub fn len(&self) -> usize {
self.storage.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.storage.len() == 0
}
#[inline]
pub fn capacity(&self) -> usize {
self.storage.capacity()
}
#[inline]
#[must_use]
pub fn freeze(self) -> Bytes {
Bytes {
storage: self.storage.freeze(),
}
}
#[inline]
#[must_use]
pub fn take(&mut self) -> Bytes {
Bytes {
storage: self.storage.split_to(self.len()),
}
}
#[inline]
#[must_use]
pub fn split_to(&mut self, at: usize) -> Bytes {
self.split_to_checked(at)
.expect("at value must be <= self.len()`")
}
#[inline]
pub fn advance_to(&mut self, cnt: usize) {
unsafe {
self.storage.set_start(cnt as u32);
}
}
#[inline]
#[must_use]
pub fn split_to_checked(&mut self, at: usize) -> Option<Bytes> {
if at <= self.len() {
Some(Bytes {
storage: self.storage.split_to(at),
})
} else {
None
}
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.storage.truncate(len);
}
#[inline]
pub fn clear(&mut self) {
self.truncate(0);
}
#[inline]
pub fn resize(&mut self, new_len: usize, value: u8) {
self.storage.resize(new_len, value);
}
#[inline]
pub unsafe fn set_len(&mut self, len: usize) {
self.storage.set_len(len);
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.storage.reserve(additional);
}
#[inline]
pub fn reserve_capacity(&mut self, cap: usize) {
self.storage.reserve_capacity(cap);
}
#[inline]
pub fn extend_from_slice(&mut self, extend: &[u8]) {
self.put_slice(extend);
}
#[inline]
pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> {
self.chunk().iter()
}
}
impl Buf for BytesMut {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn chunk(&self) -> &[u8] {
self.storage.as_ref()
}
#[inline]
fn advance(&mut self, cnt: usize) {
self.advance_to(cnt);
}
}
impl BufMut for BytesMut {
#[inline]
fn remaining_mut(&self) -> usize {
self.storage.remaining()
}
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
self.storage.set_len(self.len() + cnt);
}
#[inline]
fn chunk_mut(&mut self) -> &mut UninitSlice {
unsafe {
let ptr = &mut self.storage.as_ptr();
UninitSlice::from_raw_parts_mut(ptr.add(self.len()), self.remaining_mut())
}
}
#[inline]
fn put_slice(&mut self, src: &[u8]) {
let len = src.len();
self.reserve(len);
unsafe {
ptr::copy_nonoverlapping(src.as_ptr(), self.chunk_mut().as_mut_ptr(), len);
self.advance_mut(len);
}
}
#[inline]
fn put_u8(&mut self, n: u8) {
self.reserve(1);
self.storage.put_u8(n);
}
#[inline]
fn put_i8(&mut self, n: i8) {
self.put_u8(n as u8);
}
}
impl bytes::buf::Buf for BytesMut {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn chunk(&self) -> &[u8] {
self.storage.as_ref()
}
#[inline]
fn advance(&mut self, cnt: usize) {
self.advance_to(cnt);
}
}
unsafe impl bytes::buf::BufMut for BytesMut {
#[inline]
fn remaining_mut(&self) -> usize {
BufMut::remaining_mut(self)
}
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
BufMut::advance_mut(self, cnt);
}
#[inline]
fn chunk_mut(&mut self) -> &mut bytes::buf::UninitSlice {
unsafe {
let ptr = self.storage.as_ptr();
bytes::buf::UninitSlice::from_raw_parts_mut(
ptr.add(self.len()),
BufMut::remaining_mut(self),
)
}
}
#[inline]
fn put_slice(&mut self, src: &[u8]) {
BufMut::put_slice(self, src);
}
#[inline]
fn put_u8(&mut self, n: u8) {
BufMut::put_u8(self, n);
}
#[inline]
fn put_i8(&mut self, n: i8) {
BufMut::put_i8(self, n);
}
}
impl AsRef<[u8]> for BytesMut {
#[inline]
fn as_ref(&self) -> &[u8] {
self.storage.as_ref()
}
}
impl AsMut<[u8]> for BytesMut {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self.storage.as_mut()
}
}
impl Deref for BytesMut {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
self.as_ref()
}
}
impl DerefMut for BytesMut {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
self.storage.as_mut()
}
}
impl Eq for BytesMut {}
impl PartialEq for BytesMut {
#[inline]
fn eq(&self, other: &BytesMut) -> bool {
self.storage.as_ref() == other.storage.as_ref()
}
}
impl Default for BytesMut {
#[inline]
fn default() -> BytesMut {
BytesMut::new()
}
}
impl Borrow<[u8]> for BytesMut {
#[inline]
fn borrow(&self) -> &[u8] {
self.as_ref()
}
}
impl BorrowMut<[u8]> for BytesMut {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
self.as_mut()
}
}
impl PartialEq<Bytes> for BytesMut {
fn eq(&self, other: &Bytes) -> bool {
other[..] == self[..]
}
}
impl fmt::Debug for BytesMut {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&crate::debug::BsDebug(self.storage.as_ref()), fmt)
}
}
impl fmt::Write for BytesMut {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
self.extend_from_slice(s.as_bytes());
Ok(())
}
}
impl io::Write for BytesMut {
fn write(&mut self, src: &[u8]) -> Result<usize, io::Error> {
self.extend_from_slice(src);
Ok(src.len())
}
fn flush(&mut self) -> Result<(), io::Error> {
Ok(())
}
}
impl Clone for BytesMut {
#[inline]
fn clone(&self) -> BytesMut {
BytesMut::from(&self[..])
}
}
impl IntoIterator for BytesMut {
type Item = u8;
type IntoIter = IntoIter<BytesMut>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
impl<'a> IntoIterator for &'a BytesMut {
type Item = &'a u8;
type IntoIter = std::slice::Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
self.as_ref().iter()
}
}
impl FromIterator<u8> for BytesMut {
fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
let iter = into_iter.into_iter();
let (min, maybe_max) = iter.size_hint();
let mut out = BytesMut::with_capacity(maybe_max.unwrap_or(min));
for i in iter {
out.reserve(1);
out.put_u8(i);
}
out
}
}
impl<'a> FromIterator<&'a u8> for BytesMut {
fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
into_iter.into_iter().copied().collect::<BytesMut>()
}
}
impl Extend<u8> for BytesMut {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = u8>,
{
let iter = iter.into_iter();
let (lower, _) = iter.size_hint();
self.reserve(lower);
for (idx, b) in iter.enumerate() {
if idx >= lower {
self.reserve(1);
}
self.put_u8(b);
}
}
}
impl<'a> Extend<&'a u8> for BytesMut {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = &'a u8>,
{
self.extend(iter.into_iter().copied());
}
}
impl PartialEq<[u8]> for BytesMut {
fn eq(&self, other: &[u8]) -> bool {
&**self == other
}
}
impl<const N: usize> PartialEq<[u8; N]> for BytesMut {
fn eq(&self, other: &[u8; N]) -> bool {
&**self == other
}
}
impl PartialEq<BytesMut> for [u8] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl<const N: usize> PartialEq<BytesMut> for [u8; N] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl<const N: usize> PartialEq<BytesMut> for &[u8; N] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialEq<str> for BytesMut {
fn eq(&self, other: &str) -> bool {
&**self == other.as_bytes()
}
}
impl PartialEq<BytesMut> for str {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialEq<Vec<u8>> for BytesMut {
fn eq(&self, other: &Vec<u8>) -> bool {
*self == other[..]
}
}
impl PartialEq<BytesMut> for Vec<u8> {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialEq<String> for BytesMut {
fn eq(&self, other: &String) -> bool {
*self == other[..]
}
}
impl PartialEq<BytesMut> for String {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl<'a, T: ?Sized> PartialEq<&'a T> for BytesMut
where
BytesMut: PartialEq<T>,
{
fn eq(&self, other: &&'a T) -> bool {
*self == **other
}
}
impl PartialEq<BytesMut> for &[u8] {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialEq<BytesMut> for &str {
fn eq(&self, other: &BytesMut) -> bool {
*other == *self
}
}
impl PartialEq<BytesMut> for Bytes {
fn eq(&self, other: &BytesMut) -> bool {
other[..] == self[..]
}
}
impl From<BytesMut> for Bytes {
#[inline]
fn from(b: BytesMut) -> Self {
b.freeze()
}
}
impl<'a> From<&'a [u8]> for BytesMut {
#[inline]
fn from(src: &'a [u8]) -> BytesMut {
BytesMut::copy_from_slice(src)
}
}
impl<const N: usize> From<[u8; N]> for BytesMut {
#[inline]
fn from(src: [u8; N]) -> BytesMut {
BytesMut::copy_from_slice(src)
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for BytesMut {
#[inline]
fn from(src: &'a [u8; N]) -> BytesMut {
BytesMut::copy_from_slice(src)
}
}
impl<'a> From<&'a str> for BytesMut {
#[inline]
fn from(src: &'a str) -> BytesMut {
BytesMut::from(src.as_bytes())
}
}
impl From<Bytes> for BytesMut {
#[inline]
fn from(src: Bytes) -> BytesMut {
BytesMut::copy_from_slice(&src[..])
}
}
impl From<&Bytes> for BytesMut {
#[inline]
fn from(src: &Bytes) -> BytesMut {
BytesMut::copy_from_slice(&src[..])
}
}