use alloc::boxed::Box;
use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt;
use core::ops::{Deref, Range};
#[derive(Clone, Default)]
pub struct Bytes {
buf: Arc<[u8]>,
start: usize,
len: usize,
}
impl Bytes {
#[inline]
pub fn new() -> Self {
Self {
buf: Arc::from(&[][..]),
start: 0,
len: 0,
}
}
#[inline]
pub fn from_static(s: &'static [u8]) -> Self {
Self {
buf: Arc::from(s),
start: 0,
len: s.len(),
}
}
#[inline]
pub fn len(&self) -> usize {
self.len
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
#[inline]
pub fn slice(&self, range: Range<usize>) -> Self {
assert!(range.start <= range.end && range.end <= self.len);
Self {
buf: self.buf.clone(),
start: self.start + range.start,
len: range.end - range.start,
}
}
#[inline]
pub fn slice_from(&self, from: usize) -> Self {
assert!(from <= self.len);
Self {
buf: self.buf.clone(),
start: self.start + from,
len: self.len - from,
}
}
#[inline]
pub fn slice_to(&self, to: usize) -> Self {
assert!(to <= self.len);
Self {
buf: self.buf.clone(),
start: self.start,
len: to,
}
}
#[inline]
pub fn split_to(&mut self, at: usize) -> Self {
assert!(at <= self.len);
let out = Self {
buf: self.buf.clone(),
start: self.start,
len: at,
};
self.start += at;
self.len -= at;
out
}
#[inline]
pub fn split_off(&mut self, at: usize) -> Self {
assert!(at <= self.len);
let out = Self {
buf: self.buf.clone(),
start: self.start + at,
len: self.len - at,
};
self.len = at;
out
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
&self.buf[self.start..self.start + self.len]
}
#[inline]
pub fn to_vec(&self) -> Vec<u8> {
self.as_slice().to_vec()
}
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.to_vec()
}
#[inline]
pub fn to_str(&self) -> Result<&str, core::str::Utf8Error> {
core::str::from_utf8(self.as_slice())
}
}
impl Deref for Bytes {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
self.as_slice()
}
}
impl AsRef<[u8]> for Bytes {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_slice()
}
}
impl From<Vec<u8>> for Bytes {
#[inline]
fn from(v: Vec<u8>) -> Self {
let len = v.len();
Self {
buf: Arc::from(v.into_boxed_slice()),
start: 0,
len,
}
}
}
impl From<Box<[u8]>> for Bytes {
#[inline]
fn from(b: Box<[u8]>) -> Self {
let len = b.len();
Self {
buf: Arc::from(b),
start: 0,
len,
}
}
}
impl From<&[u8]> for Bytes {
#[inline]
fn from(s: &[u8]) -> Self {
Self::from(s.to_vec())
}
}
impl From<&str> for Bytes {
#[inline]
fn from(s: &str) -> Self {
Self::from(s.as_bytes())
}
}
impl From<String> for Bytes {
#[inline]
fn from(s: String) -> Self {
Self::from(s.into_bytes())
}
}
impl PartialEq for Bytes {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for Bytes {}
impl PartialEq<[u8]> for Bytes {
#[inline]
fn eq(&self, other: &[u8]) -> bool {
self.as_slice() == other
}
}
impl PartialEq<str> for Bytes {
#[inline]
fn eq(&self, other: &str) -> bool {
self.as_slice() == other.as_bytes()
}
}
impl PartialOrd for Bytes {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Bytes {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
self.as_slice().cmp(other.as_slice())
}
}
impl core::hash::Hash for Bytes {
#[inline]
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state)
}
}
impl fmt::Debug for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Bytes(len={})", self.len)
}
}
impl fmt::Display for Bytes {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match core::str::from_utf8(self.as_slice()) {
Ok(s) => f.write_str(s),
Err(_) => write!(f, "<invalid utf8, {} bytes>", self.len),
}
}
}
#[derive(Clone, Default)]
pub struct BytesMut {
buf: Vec<u8>,
}
impl BytesMut {
#[inline]
pub fn new() -> Self {
Self { buf: Vec::new() }
}
#[inline]
pub fn with_capacity(cap: usize) -> Self {
Self {
buf: Vec::with_capacity(cap),
}
}
#[inline]
pub fn from_vec(v: Vec<u8>) -> Self {
Self { buf: v }
}
#[inline]
pub fn len(&self) -> usize {
self.buf.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
&self.buf
}
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
&mut self.buf
}
#[inline]
pub fn clear(&mut self) {
self.buf.clear();
}
#[inline]
pub fn reserve(&mut self, extra: usize) {
self.buf.reserve(extra);
}
#[inline]
pub fn extend_from_slice(&mut self, s: &[u8]) {
self.buf.extend_from_slice(s);
}
#[inline]
pub fn extend_from_slices<'a>(&mut self, slices: impl IntoIterator<Item = &'a [u8]>) {
for s in slices {
self.buf.extend_from_slice(s);
}
}
#[inline]
pub fn put_u8(&mut self, b: u8) {
self.buf.push(b);
}
#[inline]
pub fn put_u16(&mut self, v: u16) {
self.buf.extend_from_slice(&v.to_be_bytes());
}
#[inline]
pub fn put_u24(&mut self, v: u32) {
debug_assert!(v <= 0xFF_FFFF);
self.buf.push((v >> 16) as u8);
self.buf.push((v >> 8) as u8);
self.buf.push(v as u8);
}
#[inline]
pub fn put_u32(&mut self, v: u32) {
self.buf.extend_from_slice(&v.to_be_bytes());
}
#[inline]
pub fn put_u64(&mut self, v: u64) {
self.buf.extend_from_slice(&v.to_be_bytes());
}
#[inline]
pub fn put_bytes(&mut self, b: &Bytes) {
self.buf.extend_from_slice(b.as_slice());
}
#[inline]
pub fn freeze(self) -> Bytes {
Bytes::from(self.buf)
}
#[inline]
pub fn truncate(&mut self, len: usize) {
self.buf.truncate(len);
}
#[inline]
pub fn resize(&mut self, new_len: usize, value: u8) {
self.buf.resize(new_len, value);
}
#[inline]
pub fn split_off(&mut self, at: usize) -> BytesMut {
BytesMut {
buf: self.buf.split_off(at),
}
}
#[inline]
pub fn split_to(&mut self, at: usize) -> BytesMut {
assert!(at <= self.buf.len());
let head = self.buf[..at].to_vec();
self.buf.drain(..at);
BytesMut { buf: head }
}
#[inline]
pub fn into_vec(self) -> Vec<u8> {
self.buf
}
}
impl Deref for BytesMut {
type Target = [u8];
#[inline]
fn deref(&self) -> &[u8] {
&self.buf
}
}
impl core::ops::DerefMut for BytesMut {
#[inline]
fn deref_mut(&mut self) -> &mut [u8] {
&mut self.buf
}
}
impl AsRef<[u8]> for BytesMut {
#[inline]
fn as_ref(&self) -> &[u8] {
&self.buf
}
}
impl fmt::Debug for BytesMut {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "BytesMut(len={})", self.buf.len())
}
}