Skip to content

Commit 4bebfa2

Browse files
quantumclaude
authored andcommitted
fix(hashes): improve unsafe block warnings and macro cleanup
- Fix unsafe block warnings in siphash24.rs by properly wrapping unsafe operations - Improve hash utility functions with better variable naming ($generator instead of $gen) - Remove unused import in internal_macros.rs for bincode feature These changes address compiler warnings about unsafe operations and improve code clarity without changing functionality. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent dfad2aa commit 4bebfa2

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

hashes/src/internal_macros.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ macro_rules! hash_trait_impls {
160160
#[cfg(feature = "bincode")]
161161
impl<'de, $($gen: $gent),*> bincode::BorrowDecode<'de> for Hash<$($gen),*> {
162162
fn borrow_decode<D: bincode::de::BorrowDecoder<'de>>(decoder: &mut D) -> Result<Self, bincode::error::DecodeError> {
163-
use crate::Hash;
164163
use std::convert::TryInto;
165164
// Decode a borrowed reference to a byte slice
166165
let bytes: &[u8] = bincode::BorrowDecode::borrow_decode(decoder)?;

hashes/src/siphash24.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ macro_rules! load_int_le {
7373
($buf:expr, $i:expr, $int_ty:ident) => {{
7474
debug_assert!($i + mem::size_of::<$int_ty>() <= $buf.len());
7575
let mut data = 0 as $int_ty;
76-
ptr::copy_nonoverlapping(
77-
$buf.get_unchecked($i),
78-
&mut data as *mut _ as *mut u8,
79-
mem::size_of::<$int_ty>(),
80-
);
76+
unsafe {
77+
ptr::copy_nonoverlapping(
78+
$buf.get_unchecked($i),
79+
&mut data as *mut _ as *mut u8,
80+
mem::size_of::<$int_ty>(),
81+
);
82+
}
8183
data.to_le()
8284
}};
8385
}
@@ -269,7 +271,7 @@ unsafe fn u8to64_le(buf: &[u8], start: usize, len: usize) -> u64 {
269271
i += 2
270272
}
271273
if i < len {
272-
out |= u64::from(*buf.get_unchecked(start + i)) << (i * 8);
274+
out |= u64::from(unsafe { *buf.get_unchecked(start + i) }) << (i * 8);
273275
i += 1;
274276
}
275277
debug_assert_eq!(i, len);

hashes/src/util.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ macro_rules! hex_fmt_impl(
1818
($reverse:expr, $ty:ident) => (
1919
$crate::hex_fmt_impl!($reverse, $ty, );
2020
);
21-
($reverse:expr, $ty:ident, $($gen:ident: $gent:ident),*) => (
22-
impl<$($gen: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($gen),*> {
21+
($reverse:expr, $ty:ident, $($generator:ident: $gent:ident),*) => (
22+
impl<$($generator: $gent),*> $crate::_export::_core::fmt::LowerHex for $ty<$($generator),*> {
2323
#[inline]
2424
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
2525
if $reverse {
@@ -30,7 +30,7 @@ macro_rules! hex_fmt_impl(
3030
}
3131
}
3232

33-
impl<$($gen: $gent),*> $crate::_export::_core::fmt::UpperHex for $ty<$($gen),*> {
33+
impl<$($generator: $gent),*> $crate::_export::_core::fmt::UpperHex for $ty<$($generator),*> {
3434
#[inline]
3535
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
3636
if $reverse {
@@ -41,14 +41,14 @@ macro_rules! hex_fmt_impl(
4141
}
4242
}
4343

44-
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Display for $ty<$($gen),*> {
44+
impl<$($generator: $gent),*> $crate::_export::_core::fmt::Display for $ty<$($generator),*> {
4545
#[inline]
4646
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
4747
$crate::_export::_core::fmt::LowerHex::fmt(&self, f)
4848
}
4949
}
5050

51-
impl<$($gen: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($gen),*> {
51+
impl<$($generator: $gent),*> $crate::_export::_core::fmt::Debug for $ty<$($generator),*> {
5252
#[inline]
5353
fn fmt(&self, f: &mut $crate::_export::_core::fmt::Formatter) -> $crate::_export::_core::fmt::Result {
5454
write!(f, "{:#}", self)
@@ -63,14 +63,14 @@ macro_rules! borrow_slice_impl(
6363
($ty:ident) => (
6464
$crate::borrow_slice_impl!($ty, );
6565
);
66-
($ty:ident, $($gen:ident: $gent:ident),*) => (
67-
impl<$($gen: $gent),*> $crate::_export::_core::borrow::Borrow<[u8]> for $ty<$($gen),*> {
66+
($ty:ident, $($generator:ident: $gent:ident),*) => (
67+
impl<$($generator: $gent),*> $crate::_export::_core::borrow::Borrow<[u8]> for $ty<$($generator),*> {
6868
fn borrow(&self) -> &[u8] {
6969
&self[..]
7070
}
7171
}
7272

73-
impl<$($gen: $gent),*> $crate::_export::_core::convert::AsRef<[u8]> for $ty<$($gen),*> {
73+
impl<$($generator: $gent),*> $crate::_export::_core::convert::AsRef<[u8]> for $ty<$($generator),*> {
7474
fn as_ref(&self) -> &[u8] {
7575
&self[..]
7676
}

0 commit comments

Comments
 (0)