diff --git a/ntex-bytes/src/buf/buf_impl.rs b/ntex-bytes/src/buf/buf_impl.rs index d00c9b12..724ae24f 100644 --- a/ntex-bytes/src/buf/buf_impl.rs +++ b/ntex-bytes/src/buf/buf_impl.rs @@ -1,4 +1,4 @@ -use std::{cmp, io::IoSlice, mem, ptr}; +use std::{cmp, mem, ptr}; macro_rules! buf_get_impl { ($this:ident, $typ:tt::$conv:tt) => {{ @@ -120,46 +120,6 @@ pub trait Buf { /// empty slice. fn chunk(&self) -> &[u8]; - /// Fills `dst` with potentially multiple slices starting at `self`'s - /// current position. - /// - /// If the `Buf` is backed by disjoint slices of bytes, `bytes_vectored` enables - /// fetching more than one slice at once. `dst` is a slice of `IoSlice` - /// references, enabling the slice to be directly used with [`writev`] - /// without any further conversion. The sum of the lengths of all the - /// buffers in `dst` will be less than or equal to `Buf::remaining()`. - /// - /// The entries in `dst` will be overwritten, but the data **contained** by - /// the slices **will not** be modified. If `bytes_vectored` does not fill every - /// entry in `dst`, then `dst` is guaranteed to contain all remaining slices - /// in `self. - /// - /// This is a lower level function. Most operations are done with other - /// functions. - /// - /// # Implementer notes - /// - /// This function should never panic. Once the end of the buffer is reached, - /// i.e., `Buf::remaining` returns 0, calls to `bytes_vectored` must return 0 - /// without mutating `dst`. - /// - /// Implementations should also take care to properly handle being called - /// with `dst` being a zero length slice. - /// - /// [`writev`]: http://man7.org/linux/man-pages/man2/readv.2.html - fn bytes_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize { - if dst.is_empty() { - return 0; - } - - if self.has_remaining() { - dst[0] = IoSlice::new(self.chunk()); - 1 - } else { - 0 - } - } - /// Advance the internal cursor of the Buf /// /// The next call to `bytes` will return a slice starting `cnt` bytes @@ -271,6 +231,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is no more remaining data in `self`. + #[inline] fn get_u8(&mut self) -> u8 { assert!(self.remaining() >= 1); let ret = self.chunk()[0]; @@ -294,6 +255,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is no more remaining data in `self`. + #[inline] fn get_i8(&mut self) -> i8 { assert!(self.remaining() >= 1); let ret = self.chunk()[0] as i8; @@ -317,6 +279,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u16(&mut self) -> u16 { buf_get_impl!(self, u16::from_be_bytes); } @@ -337,6 +300,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u16_le(&mut self) -> u16 { buf_get_impl!(self, u16::from_le_bytes); } @@ -357,6 +321,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i16(&mut self) -> i16 { buf_get_impl!(self, i16::from_be_bytes); } @@ -377,6 +342,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i16_le(&mut self) -> i16 { buf_get_impl!(self, i16::from_le_bytes); } @@ -397,6 +363,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u32(&mut self) -> u32 { buf_get_impl!(self, u32::from_be_bytes); } @@ -417,6 +384,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u32_le(&mut self) -> u32 { buf_get_impl!(self, u32::from_le_bytes); } @@ -437,6 +405,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i32(&mut self) -> i32 { buf_get_impl!(self, i32::from_be_bytes); } @@ -457,6 +426,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i32_le(&mut self) -> i32 { buf_get_impl!(self, i32::from_le_bytes); } @@ -477,6 +447,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u64(&mut self) -> u64 { buf_get_impl!(self, u64::from_be_bytes); } @@ -497,6 +468,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u64_le(&mut self) -> u64 { buf_get_impl!(self, u64::from_le_bytes); } @@ -517,6 +489,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i64(&mut self) -> i64 { buf_get_impl!(self, i64::from_be_bytes); } @@ -537,6 +510,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i64_le(&mut self) -> i64 { buf_get_impl!(self, i64::from_le_bytes); } @@ -557,6 +531,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u128(&mut self) -> u128 { buf_get_impl!(self, u128::from_be_bytes); } @@ -577,6 +552,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_u128_le(&mut self) -> u128 { buf_get_impl!(self, u128::from_le_bytes); } @@ -597,6 +573,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i128(&mut self) -> i128 { buf_get_impl!(self, i128::from_be_bytes); } @@ -617,6 +594,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_i128_le(&mut self) -> i128 { buf_get_impl!(self, i128::from_le_bytes); } @@ -637,6 +615,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_uint(&mut self, nbytes: usize) -> u64 { buf_get_impl!(be => self, u64, nbytes); } @@ -657,6 +636,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_uint_le(&mut self, nbytes: usize) -> u64 { buf_get_impl!(le => self, u64, nbytes); } @@ -677,6 +657,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_int(&mut self, nbytes: usize) -> i64 { buf_get_impl!(be => self, i64, nbytes); } @@ -697,6 +678,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_int_le(&mut self, nbytes: usize) -> i64 { buf_get_impl!(le => self, i64, nbytes); } @@ -718,6 +700,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_f32(&mut self) -> f32 { f32::from_bits(Self::get_u32(self)) } @@ -739,6 +722,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_f32_le(&mut self) -> f32 { f32::from_bits(Self::get_u32_le(self)) } @@ -760,6 +744,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_f64(&mut self) -> f64 { f64::from_bits(Self::get_u64(self)) } @@ -781,6 +766,7 @@ pub trait Buf { /// # Panics /// /// This function panics if there is not enough remaining data in `self`. + #[inline] fn get_f64_le(&mut self) -> f64 { f64::from_bits(Self::get_u64_le(self)) } @@ -795,6 +781,8 @@ pub trait Buf { /// let bytes = "hello world".to_bytes(); /// assert_eq!(&bytes[..], &b"hello world"[..]); /// ``` + #[inline] + #[allow(clippy::wrong_self_convention)] fn to_bytes(&mut self) -> crate::Bytes { use super::BufMut; let mut ret = crate::BytesMut::with_capacity(self.remaining()); @@ -804,36 +792,33 @@ pub trait Buf { } impl Buf for &mut T { + #[inline] fn remaining(&self) -> usize { (**self).remaining() } + #[inline] fn chunk(&self) -> &[u8] { (**self).chunk() } - fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize { - (**self).bytes_vectored(dst) - } - + #[inline] fn advance(&mut self, cnt: usize) { (**self).advance(cnt) } } impl Buf for Box { + #[inline] fn remaining(&self) -> usize { (**self).remaining() } + #[inline] fn chunk(&self) -> &[u8] { (**self).chunk() } - fn bytes_vectored<'b>(&'b self, dst: &mut [IoSlice<'b>]) -> usize { - (**self).bytes_vectored(dst) - } - fn advance(&mut self, cnt: usize) { (**self).advance(cnt) } @@ -883,9 +868,7 @@ impl Buf for Option<[u8; 1]> { } fn chunk(&self) -> &[u8] { - self.as_ref() - .map(AsRef::as_ref) - .unwrap_or(Default::default()) + self.as_ref().map(AsRef::as_ref).unwrap_or_default() } fn advance(&mut self, cnt: usize) { diff --git a/ntex-bytes/src/buf/buf_mut.rs b/ntex-bytes/src/buf/buf_mut.rs index 530ba039..4087222a 100644 --- a/ntex-bytes/src/buf/buf_mut.rs +++ b/ntex-bytes/src/buf/buf_mut.rs @@ -88,6 +88,7 @@ pub trait BufMut { /// the call must behave as if `cnt == self.remaining_mut()`. /// /// A call with `cnt == 0` should never panic and be a no-op. + #[allow(clippy::missing_safety_doc)] unsafe fn advance_mut(&mut self, cnt: usize); /// Returns true if there is space in `self` for more bytes. diff --git a/ntex-bytes/src/buf/uninit_slice.rs b/ntex-bytes/src/buf/uninit_slice.rs index d85c098a..b8b212f9 100644 --- a/ntex-bytes/src/buf/uninit_slice.rs +++ b/ntex-bytes/src/buf/uninit_slice.rs @@ -142,6 +142,7 @@ impl UninitSlice { /// assert_eq!(len, 3); /// ``` #[inline] + #[allow(clippy::len_without_is_empty)] pub fn len(&self) -> usize { self.0.len() } diff --git a/ntex-bytes/src/bytes.rs b/ntex-bytes/src/bytes.rs index 17b1d322..3ef49229 100644 --- a/ntex-bytes/src/bytes.rs +++ b/ntex-bytes/src/bytes.rs @@ -884,7 +884,7 @@ impl Bytes { /// assert_eq!(iter.next().map(|b| *b), Some(b'c')); /// assert_eq!(iter.next(), None); /// ``` - pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> { + pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> { self.chunk().iter() } } @@ -1022,7 +1022,7 @@ impl FromIterator for Bytes { impl<'a> FromIterator<&'a u8> for BytesMut { fn from_iter>(into_iter: T) -> Self { - BytesMut::from_iter(into_iter.into_iter().map(|b| *b)) + BytesMut::from_iter(into_iter.into_iter().copied()) } } @@ -1095,7 +1095,7 @@ impl<'a> IntoIterator for &'a Bytes { type IntoIter = std::slice::Iter<'a, u8>; fn into_iter(self) -> Self::IntoIter { - self.as_ref().into_iter() + self.as_ref().iter() } } @@ -1133,7 +1133,7 @@ impl<'a> Extend<&'a u8> for Bytes { where T: IntoIterator, { - self.extend(iter.into_iter().map(|b| *b)) + self.extend(iter.into_iter().copied()) } } @@ -1479,6 +1479,7 @@ impl BytesMut { /// This method will panic if `len` is out of bounds for the underlying /// slice or if it comes after the `end` of the configured window. #[inline] + #[allow(clippy::missing_safety_doc)] pub unsafe fn set_len(&mut self, len: usize) { self.inner.set_len(len) } @@ -1614,7 +1615,7 @@ impl BytesMut { /// assert_eq!(iter.next().map(|b| *b), Some(b'c')); /// assert_eq!(iter.next(), None); /// ``` - pub fn iter<'a>(&'a self) -> std::slice::Iter<'a, u8> { + pub fn iter(&'_ self) -> std::slice::Iter<'_, u8> { self.chunk().iter() } } @@ -1870,7 +1871,7 @@ impl<'a> IntoIterator for &'a BytesMut { type IntoIter = std::slice::Iter<'a, u8>; fn into_iter(self) -> Self::IntoIter { - self.as_ref().into_iter() + self.as_ref().iter() } } @@ -1895,7 +1896,7 @@ impl<'a> Extend<&'a u8> for BytesMut { where T: IntoIterator, { - self.extend(iter.into_iter().map(|b| *b)) + self.extend(iter.into_iter().copied()) } } @@ -2000,13 +2001,13 @@ impl Inner { let len = self.inline_len(); assert!(len < INLINE_CAP); unsafe { - *self.inline_ptr().offset(len as isize) = n; + *self.inline_ptr().add(len) = n; } self.set_inline_len(len + 1); } else { assert!(self.len < self.cap); unsafe { - *self.ptr.offset(self.len as isize) = n; + *self.ptr.add(self.len) = n; } self.len += 1; } @@ -2109,7 +2110,7 @@ impl Inner { } unsafe { - ptr = self.ptr.offset(self.len as isize); + ptr = self.ptr.add(self.len); } if ptr == other.ptr && self.kind() == KIND_ARC && other.kind() == KIND_ARC { debug_assert_eq!(self.arc.load(Acquire), other.arc.load(Acquire)); @@ -2163,7 +2164,7 @@ impl Inner { let new_len = len - start; let dst = self.inline_ptr(); - let src = (dst as *const u8).offset(start as isize); + let src = (dst as *const u8).add(start); ptr::copy(src, dst, new_len); @@ -2194,7 +2195,7 @@ impl Inner { // Updating the start of the view is setting `ptr` to point to the // new start and updating the `len` field to reflect the new length // of the view. - self.ptr = self.ptr.offset(start as isize); + self.ptr = self.ptr.add(start); if self.len >= start { self.len -= start; @@ -2229,12 +2230,10 @@ impl Inner { // Always check `inline` first, because if the handle is using inline // data storage, all of the `Inner` struct fields will be gibberish. - if kind == KIND_INLINE { + if kind == KIND_INLINE || kind == KIND_VEC { // Inlined buffers can always be mutated as the data is never shared // across handles. true - } else if kind == KIND_VEC { - true } else if kind == KIND_STATIC { false } else { @@ -2462,7 +2461,7 @@ impl Inner { v.reserve(additional); // Update the info - self.ptr = v.as_mut_ptr().offset(off as isize); + self.ptr = v.as_mut_ptr().add(off); self.len = v.len() - off; self.cap = v.capacity() - off; @@ -2569,19 +2568,13 @@ impl Inner { /// safe to check VEC_KIND #[inline] fn is_shared(&mut self) -> bool { - match self.kind() { - KIND_VEC => false, - _ => true, - } + !matches!(self.kind(), KIND_VEC) } /// Used for `debug_assert` statements #[inline] fn is_static(&mut self) -> bool { - match self.kind() { - KIND_STATIC => true, - _ => false, - } + matches!(self.kind(), KIND_STATIC) } #[inline] @@ -2651,7 +2644,7 @@ impl Inner { unsafe { let p: &mut AtomicPtr = &mut self.arc; - let p: &mut usize = mem::transmute(p); + let p: &mut usize = &mut *(p as *mut _ as *mut usize); *p = (pos << VEC_POS_OFFSET) | (prev & NOT_VEC_POS_MASK); } } @@ -2854,7 +2847,7 @@ impl PartialOrd for str { impl PartialEq> for BytesMut { fn eq(&self, other: &Vec) -> bool { - *self == &other[..] + *self == other[..] } } @@ -2878,7 +2871,7 @@ impl PartialOrd for Vec { impl PartialEq for BytesMut { fn eq(&self, other: &String) -> bool { - *self == &other[..] + *self == other[..] } } @@ -2992,7 +2985,7 @@ impl PartialOrd for str { impl PartialEq> for Bytes { fn eq(&self, other: &Vec) -> bool { - *self == &other[..] + *self == other[..] } } @@ -3016,7 +3009,7 @@ impl PartialOrd for Vec { impl PartialEq for Bytes { fn eq(&self, other: &String) -> bool { - *self == &other[..] + *self == other[..] } } @@ -3082,13 +3075,13 @@ where impl PartialEq for Bytes { fn eq(&self, other: &BytesMut) -> bool { - &other[..] == &self[..] + other[..] == self[..] } } impl PartialEq for BytesMut { fn eq(&self, other: &Bytes) -> bool { - &other[..] == &self[..] + other[..] == self[..] } } diff --git a/ntex-bytes/src/debug.rs b/ntex-bytes/src/debug.rs index 32d88680..cd9df0d9 100644 --- a/ntex-bytes/src/debug.rs +++ b/ntex-bytes/src/debug.rs @@ -28,7 +28,7 @@ impl fmt::Debug for BsDebug<'_> { } else if c == b'\0' { write!(fmt, "\\0")?; // ASCII printable - } else if c >= 0x20 && c < 0x7f { + } else if (0x20..0x7f).contains(&c) { write!(fmt, "{}", c as char)?; } else { write!(fmt, "\\x{:02x}", c)?; diff --git a/ntex-codec/src/framed.rs b/ntex-codec/src/framed.rs index e51e0169..8c8236fb 100644 --- a/ntex-codec/src/framed.rs +++ b/ntex-codec/src/framed.rs @@ -550,10 +550,9 @@ impl FramedParts { #[cfg(test)] mod tests { - use bytes::Bytes; - use futures::future::lazy; - use futures::Sink; + use futures::{future::lazy, Sink}; use ntex::testing::Io; + use ntex_bytes::Bytes; use super::*; use crate::BytesCodec;