use std::{cmp, hash, mem, ops};
use crate::{Buf, BytesMut, storage::INLINE_CAP, storage::Storage};
pub struct Bytes {
pub(crate) storage: Storage,
}
impl Bytes {
#[inline]
pub const fn new() -> Bytes {
Bytes {
storage: Storage::empty(),
}
}
#[inline]
#[must_use]
pub const fn from_static(bytes: &'static [u8]) -> Bytes {
Bytes {
storage: Storage::from_static(bytes),
}
}
#[inline]
pub fn len(&self) -> usize {
self.storage.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.storage.is_empty()
}
pub fn is_inline(&self) -> bool {
self.storage.is_inline()
}
#[must_use]
pub fn copy_from_slice(data: &[u8]) -> Self {
Bytes {
storage: Storage::from_slice(data),
}
}
#[must_use]
pub fn slice(&self, range: impl ops::RangeBounds<usize>) -> Bytes {
self.slice_checked(range)
.expect("Requires that `begin <= end` and `end <= self.len()`")
}
#[must_use]
pub fn slice_checked(&self, range: impl ops::RangeBounds<usize>) -> Option<Bytes> {
use std::ops::Bound;
let len = self.len();
let begin = match range.start_bound() {
Bound::Included(&n) => n,
Bound::Excluded(&n) => n.checked_add(1)?,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&n) => n.checked_add(1)?,
Bound::Excluded(&n) => n,
Bound::Unbounded => len,
};
if begin <= end && end <= len {
if end - begin <= INLINE_CAP {
Some(Bytes {
storage: Storage::from_slice(&self[begin..end]),
})
} else {
let mut ret = self.clone();
unsafe {
ret.storage.set_end(end);
ret.storage.set_start(begin);
}
Some(ret)
}
} else {
None
}
}
#[must_use]
pub fn slice_ref(&self, subset: &[u8]) -> Bytes {
self.slice_ref_checked(subset)
.expect("Given `subset` slice is not contained within the `Bytes` buffer")
}
#[must_use]
pub fn slice_ref_checked(&self, subset: &[u8]) -> Option<Bytes> {
let bytes_p = self.as_ptr() as usize;
let bytes_len = self.len();
let sub_p = subset.as_ptr() as usize;
let sub_len = subset.len();
if sub_p >= bytes_p && sub_p + sub_len <= bytes_p + bytes_len {
let sub_offset = sub_p - bytes_p;
Some(self.slice(sub_offset..(sub_offset + sub_len)))
} else {
None
}
}
#[must_use]
#[inline]
pub fn split_off(&mut self, at: usize) -> Bytes {
self.split_off_checked(at)
.expect("at value must be <= self.len()`")
}
#[must_use]
#[inline]
pub fn split_off_checked(&mut self, at: usize) -> Option<Bytes> {
if at <= self.len() {
if at == self.len() {
Some(Bytes::new())
} else if at == 0 {
Some(mem::take(self))
} else {
Some(Bytes {
storage: self.storage.split_off(at, true),
})
}
} else {
None
}
}
#[must_use]
#[inline]
pub fn split_to(&mut self, at: usize) -> Bytes {
self.split_to_checked(at)
.expect("at value must be <= self.len()`")
}
#[must_use]
#[inline]
pub fn split_to_checked(&mut self, at: usize) -> Option<Bytes> {
if at <= self.len() {
if at == self.len() {
Some(mem::take(self))
} else if at == 0 {
Some(Bytes::new())
} else {
Some(Bytes {
storage: self.storage.split_to(at),
})
}
} else {
None
}
}
#[inline]
pub fn advance_to(&mut self, cnt: usize) {
unsafe {
self.storage.set_start(cnt);
}
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.storage.truncate(len);
}
#[inline]
pub fn trimdown(&mut self) {
self.storage.trimdown();
}
#[inline]
pub fn clear(&mut self) {
self.storage = Storage::empty();
}
pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> {
self.chunk().iter()
}
#[inline]
#[doc(hidden)]
pub fn info(&self) -> crate::info::Info {
self.storage.info()
}
}
impl_buf!(Bytes {
#[inline]
fn get_u8(&mut self) -> u8 {
self.storage.get_u8()
}
});
impl_slice_traits!(Bytes);
impl Clone for Bytes {
#[inline]
fn clone(&self) -> Bytes {
Bytes {
storage: self.storage.clone(),
}
}
}
impl From<&Bytes> for Bytes {
fn from(src: &Bytes) -> Bytes {
src.clone()
}
}
impl From<crate::ByteString> for Bytes {
fn from(src: crate::ByteString) -> Bytes {
src.into_bytes()
}
}
impl From<&crate::ByteString> for Bytes {
fn from(src: &crate::ByteString) -> Bytes {
src.clone().into_bytes()
}
}
impl From<Vec<u8>> for Bytes {
fn from(src: Vec<u8>) -> Bytes {
Bytes {
storage: Storage::from_slice(&src),
}
}
}
impl From<String> for Bytes {
fn from(src: String) -> Bytes {
Bytes {
storage: Storage::from_slice(src.as_bytes()),
}
}
}
impl From<&'static [u8]> for Bytes {
fn from(src: &'static [u8]) -> Bytes {
Bytes::from_static(src)
}
}
impl From<&'static str> for Bytes {
fn from(src: &'static str) -> Bytes {
Bytes::from_static(src.as_bytes())
}
}
impl<'a, const N: usize> From<&'a [u8; N]> for Bytes {
fn from(src: &'a [u8; N]) -> Bytes {
Bytes::copy_from_slice(src)
}
}
impl FromIterator<u8> for Bytes {
fn from_iter<T: IntoIterator<Item = u8>>(into_iter: T) -> Self {
BytesMut::from_iter(into_iter).freeze()
}
}
impl<'a> FromIterator<&'a u8> for Bytes {
fn from_iter<T: IntoIterator<Item = &'a u8>>(into_iter: T) -> Self {
BytesMut::from_iter(into_iter).freeze()
}
}
impl Eq for Bytes {}
impl PartialEq for Bytes {
fn eq(&self, other: &Bytes) -> bool {
self.storage.as_ref() == other.storage.as_ref()
}
}
impl PartialOrd for Bytes {
fn partial_cmp(&self, other: &Bytes) -> Option<cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Bytes {
fn cmp(&self, other: &Bytes) -> cmp::Ordering {
self.storage.as_ref().cmp(other.storage.as_ref())
}
}
impl_read!(Bytes);
impl hash::Hash for Bytes {
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
let s: &[u8] = self.as_ref();
s.hash(state);
}
}
impl_partial_eq!(Bytes);
impl_partial_ord!(Bytes);
#[cfg(test)]
#[allow(unused_must_use)]
mod tests {
use std::collections::HashMap;
use super::*;
use crate::BufMut;
const LONG: &[u8] = b"mary had a1 little la2mb, little lamb, little lamb, little lamb, little lamb, little lamb \
mary had a little lamb, little lamb, little lamb, little lamb, little lamb, little lamb \
mary had a little lamb, little lamb, little lamb, little lamb, little lamb, little lamb \0";
#[test]
#[allow(clippy::op_ref, clippy::cmp_owned)]
fn partial_ord_reverse() {
let b = Bytes::from_static(b"b");
assert!(b"a"[..] < b);
assert!(*b"a" < b);
assert!(*"a" < b);
assert!(b"a".to_vec() < b);
assert!(String::from("a") < b);
assert!(&b"a"[..] < b);
assert!("a" < b);
assert!(b"c"[..] > b);
assert!("c" > b);
assert_eq!(b"b"[..].partial_cmp(&b), Some(cmp::Ordering::Equal));
}
#[test]
fn slice_checked_max_bounds() {
use std::ops::Bound;
let b = Bytes::from(LONG.to_vec());
assert!(b.slice_checked(..=usize::MAX).is_none());
assert!(
b.slice_checked((Bound::Excluded(usize::MAX), Bound::Unbounded))
.is_none()
);
}
#[test]
#[allow(
clippy::op_ref,
clippy::len_zero,
clippy::nonminimal_bool,
clippy::unnecessary_fallible_conversions
)]
fn bytes() {
let mut b = Bytes::from(LONG.to_vec());
b.advance_to(10);
assert_eq!(&b, &LONG[10..]);
b.advance_to(10);
assert_eq!(&b[..], &LONG[20..]);
assert_eq!(&b, &LONG[20..]);
b.clear();
assert!(b.is_inline());
assert!(b.is_empty());
assert_eq!(b.len(), 0);
let mut b = Bytes::from(LONG);
b.advance_to(10);
assert_eq!(&b, &LONG[10..]);
b.advance_to(10);
assert_eq!(&b[..], &LONG[20..]);
assert_eq!(&b, &LONG[20..]);
b.clear();
assert!(b.is_empty());
assert_eq!(b.len(), 0);
let mut b = Bytes::from(LONG);
b.split_off(10);
assert_eq!(&b, &LONG[..10]);
b.advance_to(5);
assert_eq!(&b, &LONG[5..10]);
let mut b = Bytes::copy_from_slice(&LONG[..15]);
assert!(b.is_inline());
b.split_off(10);
assert_eq!(&b, &LONG[..10]);
b.advance_to(1);
assert_eq!(&b, &LONG[1..10]);
let mut b = Bytes::from(b"123");
assert!(&b"12"[..] < &b);
assert_eq!("123", &b);
assert!("12" < &b);
assert!("12" < b);
assert_eq!(b.get_u8(), b'1');
assert_eq!("23", &b);
let mut b = Bytes::from(&Bytes::from(LONG));
assert_eq!(b, LONG);
assert_eq!(b.get_u8(), LONG[0]);
assert_eq!(b.get_u8(), LONG[1]);
assert_eq!(b.len(), LONG.len() - 2);
let b = Bytes::from(BytesMut::from(LONG));
assert_eq!(b, LONG);
let mut b: Bytes = BytesMut::try_from(b).unwrap().freeze();
assert_eq!(b, LONG);
assert!(!(b > b));
assert_eq!(<Bytes as Buf>::remaining(&b), LONG.len());
assert_eq!(<Bytes as Buf>::chunk(&b), LONG);
<Bytes as Buf>::advance(&mut b, 10);
assert_eq!(Buf::chunk(&b), &LONG[10..]);
<Bytes as Buf>::advance(&mut b, 10);
assert_eq!(Buf::chunk(&b), &LONG[20..]);
let mut h: HashMap<Bytes, usize> = HashMap::default();
h.insert(b.clone(), 1);
assert_eq!(h.get(&b), Some(&1));
let mut b = BytesMut::try_from(LONG).unwrap();
assert_eq!(b, LONG);
assert_eq!(<BytesMut as Buf>::remaining(&b), LONG.len());
assert_eq!(<BytesMut as BufMut>::remaining_mut(&b), 0);
assert_eq!(<BytesMut as Buf>::chunk(&b), LONG);
<BytesMut as Buf>::advance(&mut b, 10);
assert_eq!(<BytesMut as Buf>::chunk(&b), &LONG[10..]);
let mut b = BytesMut::with_capacity(12);
<BytesMut as BufMut>::put_i8(&mut b, 1);
assert_eq!(b, b"\x01".as_ref());
<BytesMut as BufMut>::put_u8(&mut b, 2);
assert_eq!(b, b"\x01\x02".as_ref());
<BytesMut as BufMut>::put_slice(&mut b, b"12345");
assert_eq!(b, b"\x01\x0212345".as_ref());
<BytesMut as BufMut>::chunk_mut(&mut b).write_byte(0, b'1');
unsafe { <BytesMut as BufMut>::advance_mut(&mut b, 1) };
assert_eq!(b, b"\x01\x02123451".as_ref());
let mut iter = Bytes::from(LONG.to_vec()).into_iter();
assert_eq!(iter.next(), Some(LONG[0]));
assert_eq!(iter.next(), Some(LONG[1]));
assert_eq!(iter.next(), Some(LONG[2]));
assert_eq!(iter.next(), Some(LONG[3]));
assert_eq!(iter.get_ref(), &LONG[4..]);
assert_eq!(iter.get_mut(), &LONG[4..]);
let b = iter.into_inner();
assert_eq!(b, &LONG[4..]);
let mut b = Bytes::copy_from_slice(b"123");
assert!(b.is_inline());
assert_eq!(b.storage.capacity(), 23);
b.truncate(2);
assert_eq!(b, *b"12");
assert_eq!(bytes::buf::Buf::get_u8(&mut b), 49);
assert_eq!(b.len(), 1);
}
#[test]
fn bytes_read() {
use std::io::Read;
let mut b = Bytes::copy_from_slice(b"123");
let mut buf = [0; 10];
assert_eq!(b.read(&mut buf).unwrap(), 3);
assert_eq!(b.len(), 0);
assert_eq!(buf, [49, 50, 51, 0, 0, 0, 0, 0, 0, 0]);
}
#[test]
fn from_byte_string() {
let s = crate::ByteString::from_static("string");
assert_eq!(Bytes::from(&s), "string");
assert_eq!(Bytes::from(s), "string");
}
}