mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-17 00:00:14 +02:00
Various cleanup (#635)
This commit is contained in:
+117
-51
@@ -1,8 +1,9 @@
|
||||
#[cfg(feature = "std")]
|
||||
use crate::buf::{reader, Reader};
|
||||
use crate::buf::{take, Chain, Take};
|
||||
|
||||
use core::{cmp, mem, ptr};
|
||||
#[cfg(feature = "std")]
|
||||
use crate::{min_u64_usize, saturating_sub_usize_u64};
|
||||
use crate::{panic_advance, panic_does_not_fit};
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
use std::io::IoSlice;
|
||||
@@ -11,7 +12,12 @@ use alloc::boxed::Box;
|
||||
|
||||
macro_rules! buf_get_impl {
|
||||
($this:ident, $typ:tt::$conv:tt) => {{
|
||||
const SIZE: usize = mem::size_of::<$typ>();
|
||||
const SIZE: usize = core::mem::size_of::<$typ>();
|
||||
|
||||
if $this.remaining() < SIZE {
|
||||
panic_advance(SIZE, $this.remaining());
|
||||
}
|
||||
|
||||
// try to convert directly from the bytes
|
||||
// this Option<ret> trick is to avoid keeping a borrow on self
|
||||
// when advance() is called (mut borrow) and to call bytes() only once
|
||||
@@ -32,19 +38,30 @@ macro_rules! buf_get_impl {
|
||||
}
|
||||
}};
|
||||
(le => $this:ident, $typ:tt, $len_to_read:expr) => {{
|
||||
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
||||
const SIZE: usize = core::mem::size_of::<$typ>();
|
||||
|
||||
// The same trick as above does not improve the best case speed.
|
||||
// It seems to be linked to the way the method is optimised by the compiler
|
||||
let mut buf = [0; (mem::size_of::<$typ>())];
|
||||
$this.copy_to_slice(&mut buf[..($len_to_read)]);
|
||||
let mut buf = [0; SIZE];
|
||||
|
||||
let subslice = match buf.get_mut(..$len_to_read) {
|
||||
Some(subslice) => subslice,
|
||||
None => panic_does_not_fit(SIZE, $len_to_read),
|
||||
};
|
||||
|
||||
$this.copy_to_slice(subslice);
|
||||
return $typ::from_le_bytes(buf);
|
||||
}};
|
||||
(be => $this:ident, $typ:tt, $len_to_read:expr) => {{
|
||||
debug_assert!(mem::size_of::<$typ>() >= $len_to_read);
|
||||
const SIZE: usize = core::mem::size_of::<$typ>();
|
||||
|
||||
let mut buf = [0; (mem::size_of::<$typ>())];
|
||||
$this.copy_to_slice(&mut buf[mem::size_of::<$typ>() - ($len_to_read)..]);
|
||||
let slice_at = match SIZE.checked_sub($len_to_read) {
|
||||
Some(slice_at) => slice_at,
|
||||
None => panic_does_not_fit(SIZE, $len_to_read),
|
||||
};
|
||||
|
||||
let mut buf = [0; SIZE];
|
||||
$this.copy_to_slice(&mut buf[slice_at..]);
|
||||
return $typ::from_be_bytes(buf);
|
||||
}};
|
||||
}
|
||||
@@ -247,23 +264,18 @@ pub trait Buf {
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if `self.remaining() < dst.len()`
|
||||
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
||||
let mut off = 0;
|
||||
/// This function panics if `self.remaining() < dst.len()`.
|
||||
fn copy_to_slice(&mut self, mut dst: &mut [u8]) {
|
||||
if self.remaining() < dst.len() {
|
||||
panic_advance(dst.len(), self.remaining());
|
||||
}
|
||||
|
||||
assert!(self.remaining() >= dst.len());
|
||||
while !dst.is_empty() {
|
||||
let src = self.chunk();
|
||||
let cnt = usize::min(src.len(), dst.len());
|
||||
|
||||
while off < dst.len() {
|
||||
let cnt;
|
||||
|
||||
unsafe {
|
||||
let src = self.chunk();
|
||||
cnt = cmp::min(src.len(), dst.len() - off);
|
||||
|
||||
ptr::copy_nonoverlapping(src.as_ptr(), dst[off..].as_mut_ptr(), cnt);
|
||||
|
||||
off += cnt;
|
||||
}
|
||||
dst[..cnt].copy_from_slice(&src[..cnt]);
|
||||
dst = &mut dst[cnt..];
|
||||
|
||||
self.advance(cnt);
|
||||
}
|
||||
@@ -286,7 +298,9 @@ pub trait Buf {
|
||||
///
|
||||
/// This function panics if there is no more remaining data in `self`.
|
||||
fn get_u8(&mut self) -> u8 {
|
||||
assert!(self.remaining() >= 1);
|
||||
if self.remaining() < 1 {
|
||||
panic_advance(1, 0);
|
||||
}
|
||||
let ret = self.chunk()[0];
|
||||
self.advance(1);
|
||||
ret
|
||||
@@ -309,7 +323,9 @@ pub trait Buf {
|
||||
///
|
||||
/// This function panics if there is no more remaining data in `self`.
|
||||
fn get_i8(&mut self) -> i8 {
|
||||
assert!(self.remaining() >= 1);
|
||||
if self.remaining() < 1 {
|
||||
panic_advance(1, 0);
|
||||
}
|
||||
let ret = self.chunk()[0] as i8;
|
||||
self.advance(1);
|
||||
ret
|
||||
@@ -877,7 +893,8 @@ pub trait Buf {
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if there is not enough remaining data in `self`.
|
||||
/// This function panics if there is not enough remaining data in `self`, or
|
||||
/// if `nbytes` is greater than 8.
|
||||
fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
|
||||
if cfg!(target_endian = "big") {
|
||||
self.get_uint(nbytes)
|
||||
@@ -901,7 +918,8 @@ pub trait Buf {
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if there is not enough remaining data in `self`.
|
||||
/// This function panics if there is not enough remaining data in `self`, or
|
||||
/// if `nbytes` is greater than 8.
|
||||
fn get_int(&mut self, nbytes: usize) -> i64 {
|
||||
buf_get_impl!(be => self, i64, nbytes);
|
||||
}
|
||||
@@ -921,7 +939,8 @@ pub trait Buf {
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if there is not enough remaining data in `self`.
|
||||
/// This function panics if there is not enough remaining data in `self`, or
|
||||
/// if `nbytes` is greater than 8.
|
||||
fn get_int_le(&mut self, nbytes: usize) -> i64 {
|
||||
buf_get_impl!(le => self, i64, nbytes);
|
||||
}
|
||||
@@ -944,7 +963,8 @@ pub trait Buf {
|
||||
///
|
||||
/// # Panics
|
||||
///
|
||||
/// This function panics if there is not enough remaining data in `self`.
|
||||
/// This function panics if there is not enough remaining data in `self`, or
|
||||
/// if `nbytes` is greater than 8.
|
||||
fn get_int_ne(&mut self, nbytes: usize) -> i64 {
|
||||
if cfg!(target_endian = "big") {
|
||||
self.get_int(nbytes)
|
||||
@@ -1103,7 +1123,9 @@ pub trait Buf {
|
||||
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
|
||||
use super::BufMut;
|
||||
|
||||
assert!(len <= self.remaining(), "`len` greater than remaining");
|
||||
if self.remaining() < len {
|
||||
panic_advance(len, self.remaining());
|
||||
}
|
||||
|
||||
let mut ret = crate::BytesMut::with_capacity(len);
|
||||
ret.put(self.take(len));
|
||||
@@ -1195,135 +1217,168 @@ pub trait Buf {
|
||||
|
||||
macro_rules! deref_forward_buf {
|
||||
() => {
|
||||
#[inline]
|
||||
fn remaining(&self) -> usize {
|
||||
(**self).remaining()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn chunk(&self) -> &[u8] {
|
||||
(**self).chunk()
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
#[inline]
|
||||
fn chunks_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize {
|
||||
(**self).chunks_vectored(dst)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn advance(&mut self, cnt: usize) {
|
||||
(**self).advance(cnt)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn has_remaining(&self) -> bool {
|
||||
(**self).has_remaining()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
||||
(**self).copy_to_slice(dst)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u8(&mut self) -> u8 {
|
||||
(**self).get_u8()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i8(&mut self) -> i8 {
|
||||
(**self).get_i8()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u16(&mut self) -> u16 {
|
||||
(**self).get_u16()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u16_le(&mut self) -> u16 {
|
||||
(**self).get_u16_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u16_ne(&mut self) -> u16 {
|
||||
(**self).get_u16_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i16(&mut self) -> i16 {
|
||||
(**self).get_i16()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i16_le(&mut self) -> i16 {
|
||||
(**self).get_i16_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i16_ne(&mut self) -> i16 {
|
||||
(**self).get_i16_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u32(&mut self) -> u32 {
|
||||
(**self).get_u32()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u32_le(&mut self) -> u32 {
|
||||
(**self).get_u32_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u32_ne(&mut self) -> u32 {
|
||||
(**self).get_u32_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i32(&mut self) -> i32 {
|
||||
(**self).get_i32()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i32_le(&mut self) -> i32 {
|
||||
(**self).get_i32_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i32_ne(&mut self) -> i32 {
|
||||
(**self).get_i32_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u64(&mut self) -> u64 {
|
||||
(**self).get_u64()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u64_le(&mut self) -> u64 {
|
||||
(**self).get_u64_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_u64_ne(&mut self) -> u64 {
|
||||
(**self).get_u64_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i64(&mut self) -> i64 {
|
||||
(**self).get_i64()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i64_le(&mut self) -> i64 {
|
||||
(**self).get_i64_le()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_i64_ne(&mut self) -> i64 {
|
||||
(**self).get_i64_ne()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_uint(&mut self, nbytes: usize) -> u64 {
|
||||
(**self).get_uint(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_uint_le(&mut self, nbytes: usize) -> u64 {
|
||||
(**self).get_uint_le(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_uint_ne(&mut self, nbytes: usize) -> u64 {
|
||||
(**self).get_uint_ne(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_int(&mut self, nbytes: usize) -> i64 {
|
||||
(**self).get_int(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_int_le(&mut self, nbytes: usize) -> i64 {
|
||||
(**self).get_int_le(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn get_int_ne(&mut self, nbytes: usize) -> i64 {
|
||||
(**self).get_int_ne(nbytes)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
|
||||
(**self).copy_to_bytes(len)
|
||||
}
|
||||
@@ -1351,41 +1406,52 @@ impl Buf for &[u8] {
|
||||
|
||||
#[inline]
|
||||
fn advance(&mut self, cnt: usize) {
|
||||
if self.len() < cnt {
|
||||
panic_advance(cnt, self.len());
|
||||
}
|
||||
|
||||
*self = &self[cnt..];
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
||||
if self.len() < dst.len() {
|
||||
panic_advance(dst.len(), self.len());
|
||||
}
|
||||
|
||||
dst.copy_from_slice(&self[..dst.len()]);
|
||||
self.advance(dst.len());
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "std")]
|
||||
impl<T: AsRef<[u8]>> Buf for std::io::Cursor<T> {
|
||||
#[inline]
|
||||
fn remaining(&self) -> usize {
|
||||
let len = self.get_ref().as_ref().len();
|
||||
let pos = self.position();
|
||||
|
||||
if pos >= len as u64 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
len - pos as usize
|
||||
saturating_sub_usize_u64(self.get_ref().as_ref().len(), self.position())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn chunk(&self) -> &[u8] {
|
||||
let slice = self.get_ref().as_ref();
|
||||
let pos = min_u64_usize(self.position(), slice.len());
|
||||
&slice[pos..]
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn advance(&mut self, cnt: usize) {
|
||||
let len = self.get_ref().as_ref().len();
|
||||
let pos = self.position();
|
||||
|
||||
if pos >= len as u64 {
|
||||
return &[];
|
||||
// We intentionally allow `cnt == 0` here even if `pos > len`.
|
||||
let max_cnt = saturating_sub_usize_u64(len, pos);
|
||||
if cnt > max_cnt {
|
||||
panic_advance(cnt, max_cnt);
|
||||
}
|
||||
|
||||
&self.get_ref().as_ref()[pos as usize..]
|
||||
}
|
||||
|
||||
fn advance(&mut self, cnt: usize) {
|
||||
let pos = (self.position() as usize)
|
||||
.checked_add(cnt)
|
||||
.expect("overflow");
|
||||
|
||||
assert!(pos <= self.get_ref().as_ref().len());
|
||||
self.set_position(pos as u64);
|
||||
// This will not overflow because either `cnt == 0` or the sum is not
|
||||
// greater than `len`.
|
||||
self.set_position(pos + cnt as u64);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user