Skip to content

Commit

Permalink
Fix Hash for EndianReader (#723)
Browse files Browse the repository at this point in the history
Since we have a manual PartialEq implementation, we need a
matching manual Hash implementation.
  • Loading branch information
philipc authored Jun 28, 2024
1 parent f1097e3 commit 9810fdd
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/read/endian_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use alloc::rc::Rc;
use alloc::string::String;
use alloc::sync::Arc;
use core::fmt::Debug;
use core::hash::{Hash, Hasher};
use core::ops::{Deref, Index, Range, RangeFrom, RangeTo};
use core::slice;
use core::str;
Expand Down Expand Up @@ -116,7 +117,7 @@ pub type EndianArcSlice<Endian> = EndianReader<Endian, Arc<[u8]>>;
/// pub type MmapFileReader<Endian> = gimli::EndianReader<Endian, ArcMmapFile>;
/// # fn test(_: &MmapFileReader<gimli::NativeEndian>) { }
/// ```
#[derive(Debug, Clone, Copy, Hash)]
#[derive(Debug, Clone, Copy)]
pub struct EndianReader<Endian, T>
where
Endian: Endianity,
Expand Down Expand Up @@ -144,6 +145,17 @@ where
{
}

impl<Endian, T> Hash for EndianReader<Endian, T>
where
Endian: Endianity,
T: CloneStableDeref<Target = [u8]> + Debug,
{
fn hash<H: Hasher>(&self, state: &mut H) {
// This must match the `PartialEq` implementation.
self.bytes().hash(state);
}
}

// This is separated out from `EndianReader` so that we can avoid running afoul
// of borrowck. We need to `read_slice(&mut self, ...) -> &[u8]` and then call
// `self.endian.read_whatever` on the result. The problem is that the returned
Expand Down

0 comments on commit 9810fdd

Please sign in to comment.