use std::borrow::{Borrow, BorrowMut};
use std::ops::{Deref, DerefMut};
use std::{fmt, ptr};
use crate::storage::StorageVec;
use crate::{buf::IntoIter, buf::UninitSlice, debug, Buf, BufMut, Bytes, BytesMut};
pub struct BytesVec {
pub(crate) inner: StorageVec,
}
impl BytesVec {
#[inline]
pub fn with_capacity(capacity: usize) -> BytesVec {
BytesVec {
inner: StorageVec::with_capacity(capacity),
}
}
pub fn copy_from_slice<T: AsRef<[u8]>>(src: T) -> Self {
BytesVec {
inner: StorageVec::from_slice(src.as_ref()),
}
}
#[inline]
pub fn new() -> BytesVec {
BytesVec {
inner: StorageVec::with_capacity(0),
}
}
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.len() == 0
}
#[inline]
pub fn capacity(&self) -> usize {
self.inner.capacity()
}
#[inline]
pub fn freeze(self) -> Bytes {
Bytes {
inner: self.inner.freeze(),
}
}
pub fn split(&mut self) -> BytesMut {
self.split_to(self.len())
}
pub fn split_to(&mut self, at: usize) -> BytesMut {
self.split_to_checked(at)
.expect("at value must be <= self.len()`")
}
pub fn split_to_checked(&mut self, at: usize) -> Option<BytesMut> {
if at <= self.len() {
Some(BytesMut {
inner: self.inner.split_to(at, false),
})
} else {
None
}
}
pub fn truncate(&mut self, len: usize) {
self.inner.truncate(len);
}
pub fn clear(&mut self) {
self.truncate(0);
}
#[inline]
pub fn resize(&mut self, new_len: usize, value: u8) {
self.inner.resize(new_len, value);
}
#[inline]
#[allow(clippy::missing_safety_doc)]
pub unsafe fn set_len(&mut self, len: usize) {
self.inner.set_len(len)
}
#[inline]
pub fn reserve(&mut self, additional: usize) {
self.inner.reserve(additional);
}
#[inline]
pub fn extend_from_slice(&mut self, extend: &[u8]) {
self.put_slice(extend);
}
#[inline]
pub fn with_bytes_mut<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut BytesMut) -> R,
{
self.inner.with_bytes_mut(f)
}
#[inline]
pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> {
self.chunk().iter()
}
}
impl Buf for BytesVec {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn chunk(&self) -> &[u8] {
self.inner.as_ref()
}
#[inline]
fn advance(&mut self, cnt: usize) {
assert!(
cnt <= self.inner.len(),
"cannot advance past `remaining` len:{} delta:{}",
self.inner.len(),
cnt
);
unsafe {
self.inner.set_start(cnt as u32);
}
}
}
impl BufMut for BytesVec {
#[inline]
fn remaining_mut(&self) -> usize {
self.capacity() - self.len()
}
#[inline]
unsafe fn advance_mut(&mut self, cnt: usize) {
self.inner.set_len(self.len() + cnt);
}
#[inline]
fn chunk_mut(&mut self) -> &mut UninitSlice {
let len = self.len();
unsafe {
let ptr = &mut self.inner.as_raw()[len..];
UninitSlice::from_raw_parts_mut(ptr.as_mut_ptr(), self.capacity() - len)
}
}
#[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.inner.put_u8(n);
}
#[inline]
fn put_i8(&mut self, n: i8) {
self.reserve(1);
self.put_u8(n as u8);
}
}
impl bytes::buf::Buf for BytesVec {
#[inline]
fn remaining(&self) -> usize {
self.len()
}
#[inline]
fn chunk(&self) -> &[u8] {
self.inner.as_ref()
}
#[inline]
fn advance(&mut self, cnt: usize) {
Buf::advance(self, cnt)
}
}
unsafe impl bytes::buf::BufMut for BytesVec {
#[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 {
let len = self.len();
unsafe {
let ptr = &mut self.inner.as_raw()[len..];
bytes::buf::UninitSlice::from_raw_parts_mut(
ptr.as_mut_ptr(),
self.capacity() - len,
)
}
}
#[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 BytesVec {
#[inline]
fn as_ref(&self) -> &[u8] {
self.inner.as_ref()
}
}
impl AsMut<[u8]> for BytesVec {
#[inline]
fn as_mut(&mut self) -> &mut [u8] {
self.inner.as_mut()
}
}
impl Deref for BytesVec {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
self.as_ref()
}
}
impl DerefMut for BytesVec {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
self.inner.as_mut()
}
}
impl Eq for BytesVec {}
impl PartialEq for BytesVec {
#[inline]
fn eq(&self, other: &BytesVec) -> bool {
self.inner.as_ref() == other.inner.as_ref()
}
}
impl Default for BytesVec {
#[inline]
fn default() -> BytesVec {
BytesVec::new()
}
}
impl Borrow<[u8]> for BytesVec {
#[inline]
fn borrow(&self) -> &[u8] {
self.as_ref()
}
}
impl BorrowMut<[u8]> for BytesVec {
#[inline]
fn borrow_mut(&mut self) -> &mut [u8] {
self.as_mut()
}
}
impl PartialEq<Bytes> for BytesVec {
fn eq(&self, other: &Bytes) -> bool {
other[..] == self[..]
}
}
impl PartialEq<BytesMut> for BytesVec {
fn eq(&self, other: &BytesMut) -> bool {
other[..] == self[..]
}
}
impl fmt::Debug for BytesVec {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&debug::BsDebug(self.inner.as_ref()), fmt)
}
}
impl fmt::Write for BytesVec {
#[inline]
fn write_str(&mut self, s: &str) -> fmt::Result {
if self.remaining_mut() >= s.len() {
self.put_slice(s.as_bytes());
Ok(())
} else {
Err(fmt::Error)
}
}
#[inline]
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
fmt::write(self, args)
}
}
impl IntoIterator for BytesVec {
type Item = u8;
type IntoIter = IntoIter<BytesVec>;
fn into_iter(self) -> Self::IntoIter {
IntoIter::new(self)
}
}
impl<'a> IntoIterator for &'a BytesVec {
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 BytesVec {
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 = BytesVec::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 BytesVec {
fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
into_iter.into_iter().copied().collect::<BytesVec>()
}
}
impl Extend<u8> for BytesVec {
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 b in iter {
self.put_u8(b);
}
}
}
impl<'a> Extend<&'a u8> for BytesVec {
fn extend<T>(&mut self, iter: T)
where
T: IntoIterator<Item = &'a u8>,
{
self.extend(iter.into_iter().copied())
}
}
impl PartialEq<[u8]> for BytesVec {
fn eq(&self, other: &[u8]) -> bool {
&**self == other
}
}
impl<const N: usize> PartialEq<[u8; N]> for BytesVec {
fn eq(&self, other: &[u8; N]) -> bool {
&**self == other
}
}
impl PartialEq<BytesVec> for [u8] {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl<const N: usize> PartialEq<BytesVec> for [u8; N] {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl<const N: usize> PartialEq<BytesVec> for &[u8; N] {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl PartialEq<str> for BytesVec {
fn eq(&self, other: &str) -> bool {
&**self == other.as_bytes()
}
}
impl PartialEq<BytesVec> for str {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl PartialEq<Vec<u8>> for BytesVec {
fn eq(&self, other: &Vec<u8>) -> bool {
*self == other[..]
}
}
impl PartialEq<BytesVec> for Vec<u8> {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl PartialEq<String> for BytesVec {
fn eq(&self, other: &String) -> bool {
*self == other[..]
}
}
impl PartialEq<BytesVec> for String {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl<'a, T: ?Sized> PartialEq<&'a T> for BytesVec
where
BytesVec: PartialEq<T>,
{
fn eq(&self, other: &&'a T) -> bool {
*self == **other
}
}
impl PartialEq<BytesVec> for &[u8] {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl PartialEq<BytesVec> for &str {
fn eq(&self, other: &BytesVec) -> bool {
*other == *self
}
}
impl PartialEq<BytesVec> for Bytes {
fn eq(&self, other: &BytesVec) -> bool {
other[..] == self[..]
}
}
impl PartialEq<BytesVec> for BytesMut {
fn eq(&self, other: &BytesVec) -> bool {
other[..] == self[..]
}
}
impl From<BytesVec> for Bytes {
#[inline]
fn from(b: BytesVec) -> Self {
b.freeze()
}
}
impl<'a> From<&'a [u8]> for BytesVec {
#[inline]
fn from(src: &'a [u8]) -> BytesVec {
BytesVec::copy_from_slice(src)
}
}
impl<const N: usize> From<[u8; N]> for BytesVec {
#[inline]
fn from(src: [u8; N]) -> BytesVec {
BytesVec::copy_from_slice(src)
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for BytesVec {
#[inline]
fn from(src: &'a [u8; N]) -> BytesVec {
BytesVec::copy_from_slice(src)
}
}
impl<'a> From<&'a str> for BytesVec {
#[inline]
fn from(src: &'a str) -> BytesVec {
BytesVec::from(src.as_bytes())
}
}