From e97cc7362acee46ad8652097fa7ba2106fb7533c Mon Sep 17 00:00:00 2001 From: polazarus Date: Sun, 9 Feb 2025 21:47:47 +0100 Subject: [PATCH] fix lints --- src/bytes.rs | 18 +++++++----------- src/bytes/borsh.rs | 8 ++++---- src/bytes/raw.rs | 2 +- src/bytes/raw/borrowed.rs | 2 +- src/smart.rs | 4 ++-- src/string.rs | 4 ++-- src/string/borsh.rs | 6 +++--- src/string/bstr.rs | 2 +- 8 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index b586fbf..a73bd36 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -287,13 +287,12 @@ where SplitMut::Inline(inline) => inline.as_mut_ptr(), SplitMut::Allocated(heap) => unsafe { heap.as_mut_ptr_unchecked() }, SplitMut::Borrowed(_) => { - #[cfg(debug_assertions)] - { + if cfg!(debug_assertions) { panic!("mutable pointer of borrowed string"); - } - #[cfg(not(debug_assertions))] - { - unreachable_unchecked() + } else { + unsafe { + unreachable_unchecked(); + } } } } @@ -359,12 +358,9 @@ where SplitMut::Inline(inline) => inline.as_mut_slice(), SplitMut::Allocated(allocated) => unsafe { allocated.as_mut_slice_unchecked() }, SplitMut::Borrowed(_) => { - #[cfg(debug_assertions)] - { + if cfg!(debug_assertions) { panic!("mutable slice of borrowed string"); - } - #[cfg(not(debug_assertions))] - { + } else { unsafe { unreachable_unchecked() } } } diff --git a/src/bytes/borsh.rs b/src/bytes/borsh.rs index ad4eaa5..d25cfff 100644 --- a/src/bytes/borsh.rs +++ b/src/bytes/borsh.rs @@ -9,7 +9,7 @@ use crate::Backend; #[cfg(test)] mod tests; -impl<'borrow, B: Backend> BorshDeserialize for HipByt<'borrow, B> { +impl BorshDeserialize for HipByt<'_, B> { fn deserialize_reader(reader: &mut R) -> io::Result { let len = u32::deserialize_reader(reader)? as usize; if len == 0 { @@ -17,8 +17,8 @@ impl<'borrow, B: Backend> BorshDeserialize for HipByt<'borrow, B> { } else { let mut result = Self::with_capacity(len); let slice = result.spare_capacity_mut(); - for i in 0..len { - slice[i].write(u8::deserialize_reader(reader)?); + for byte in slice.iter_mut().take(len) { + byte.write(u8::deserialize_reader(reader)?); } unsafe { result.set_len(len); @@ -28,7 +28,7 @@ impl<'borrow, B: Backend> BorshDeserialize for HipByt<'borrow, B> { } } -impl<'borrow, B: Backend> BorshSerialize for HipByt<'borrow, B> { +impl BorshSerialize for HipByt<'_, B> { fn serialize(&self, writer: &mut W) -> io::Result<()> { self.as_slice().serialize(writer) } diff --git a/src/bytes/raw.rs b/src/bytes/raw.rs index 4bd4017..98440dd 100644 --- a/src/bytes/raw.rs +++ b/src/bytes/raw.rs @@ -467,7 +467,7 @@ impl<'borrow, B: Backend> HipByt<'borrow, B> { /// /// Returns `None` if this byte string is not allocated. #[inline] - pub(super) fn take_allocated(&mut self) -> Option> { + pub(super) const fn take_allocated(&mut self) -> Option> { match self.split() { Split::Allocated(&allocated) => { // Takes a copy of allocated diff --git a/src/bytes/raw/borrowed.rs b/src/bytes/raw/borrowed.rs index 005f631..b8cea5a 100644 --- a/src/bytes/raw/borrowed.rs +++ b/src/bytes/raw/borrowed.rs @@ -8,7 +8,7 @@ use super::TAG_BORROWED; #[cfg(test)] mod tests; -const TAG_NZ: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(TAG_BORROWED) }; +const TAG_NZ: NonZeroU8 = NonZeroU8::new(TAG_BORROWED).unwrap(); /// Borrowed slice representation. /// diff --git a/src/smart.rs b/src/smart.rs index dc44735..58324b6 100644 --- a/src/smart.rs +++ b/src/smart.rs @@ -269,7 +269,7 @@ where /// /// Any caller should check the uniqueness first with [`Self::is_unique`]. #[inline] - pub unsafe fn as_mut_unchecked(&mut self) -> &mut T { + pub const unsafe fn as_mut_unchecked(&mut self) -> &mut T { // SAFETY: uniqueness precondition unsafe { &mut self.0.as_mut().value } } @@ -281,7 +281,7 @@ where /// - Any caller should check the uniqueness first with [`Self::is_unique`]. /// - The referenced value must outlive `'a`. #[inline] - pub(crate) unsafe fn as_mut_unchecked_extended<'a>(&mut self) -> &'a mut T + pub(crate) const unsafe fn as_mut_unchecked_extended<'a>(&mut self) -> &'a mut T where Self: 'a, { diff --git a/src/string.rs b/src/string.rs index a5cd87c..08bf7a1 100644 --- a/src/string.rs +++ b/src/string.rs @@ -782,8 +782,8 @@ where /// [`from_utf8_unchecked`]: HipStr::from_utf8_unchecked /// [`into_bytes`]: HipStr::into_bytes #[inline] - pub fn from_utf8(bytes: HipByt<'borrow, B>) -> Result> { - match core::str::from_utf8(&bytes) { + pub const fn from_utf8(bytes: HipByt<'borrow, B>) -> Result> { + match core::str::from_utf8(bytes.as_slice()) { Ok(_) => { // SAFETY: checked above Ok(unsafe { Self::from_utf8_unchecked(bytes) }) diff --git a/src/string/borsh.rs b/src/string/borsh.rs index 8491728..5ffea56 100644 --- a/src/string/borsh.rs +++ b/src/string/borsh.rs @@ -12,9 +12,9 @@ use crate::Backend; #[cfg(test)] mod tests; -impl<'borrow, B: Backend> BorshDeserialize for HipStr<'borrow, B> { +impl BorshDeserialize for HipStr<'_, B> { fn deserialize_reader(reader: &mut R) -> io::Result { - let bytes: HipByt<'borrow, B> = HipByt::deserialize_reader(reader)?; + let bytes: HipByt = HipByt::deserialize_reader(reader)?; Self::try_from(bytes).map_err(|err| { let msg = err.to_string(); Error::new(ErrorKind::InvalidData, msg) @@ -22,7 +22,7 @@ impl<'borrow, B: Backend> BorshDeserialize for HipStr<'borrow, B> { } } -impl<'borrow, B: Backend> BorshSerialize for HipStr<'borrow, B> { +impl BorshSerialize for HipStr<'_, B> { fn serialize(&self, writer: &mut W) -> io::Result<()> { self.as_bytes().serialize(writer) } diff --git a/src/string/bstr.rs b/src/string/bstr.rs index a130b43..71a035a 100644 --- a/src/string/bstr.rs +++ b/src/string/bstr.rs @@ -57,7 +57,7 @@ symmetric_ord! { [B] [where B: Backend] (&BString, HipStr<'_, B>) = bstr_cmp; } -impl<'a, B> TryFrom for HipStr<'a, B> +impl TryFrom for HipStr<'_, B> where B: Backend, {