Skip to content

Commit 34f2e43

Browse files
authored
fix: hash should different when map are different (#136)
1 parent 5001ae9 commit 34f2e43

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

src/cached_source.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<T: Source + Hash + PartialEq + Eq + 'static> Hash for CachedSource<T> {
174174
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
175175
(self.cached_hash.get_or_init(|| {
176176
let mut hasher = FxHasher::default();
177-
hasher.write(self.source().as_bytes());
177+
self.inner.hash(&mut hasher);
178178
hasher.finish()
179179
}))
180180
.hash(state);
@@ -208,8 +208,8 @@ mod tests {
208208
use std::borrow::Borrow;
209209

210210
use crate::{
211-
ConcatSource, OriginalSource, RawSource, SourceExt, SourceMapSource,
212-
WithoutOriginalOptions,
211+
ConcatSource, OriginalSource, RawSource, ReplaceSource, SourceExt,
212+
SourceMapSource, WithoutOriginalOptions,
213213
};
214214

215215
use super::*;
@@ -364,4 +364,27 @@ mod tests {
364364
source.source();
365365
assert_eq!(source.buffer(), buf.as_slice());
366366
}
367+
368+
#[test]
369+
fn hash_should_different_when_map_are_different() {
370+
let hash1 = {
371+
let mut source =
372+
ReplaceSource::new(OriginalSource::new("Hello", "hello.txt").boxed());
373+
source.insert(5, " world", None);
374+
let cache = CachedSource::new(source);
375+
let mut hasher = FxHasher::default();
376+
cache.hash(&mut hasher);
377+
hasher.finish()
378+
};
379+
380+
let hash2 = {
381+
let source = OriginalSource::new("Hello world", "hello.txt").boxed();
382+
let cache = CachedSource::new(source);
383+
let mut hasher = FxHasher::default();
384+
cache.hash(&mut hasher);
385+
hasher.finish()
386+
};
387+
388+
assert!(hash1 != hash2);
389+
}
367390
}

src/source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ mod tests {
524524
RawBufferSource::from("a".as_bytes()).hash(&mut state);
525525
(&RawSource::from("h") as &dyn Source).hash(&mut state);
526526
ReplaceSource::new(RawSource::from("i").boxed()).hash(&mut state);
527-
assert_eq!(format!("{:x}", state.finish()), "709931db47fa47dc");
527+
assert_eq!(format!("{:x}", state.finish()), "f4b280bd9a8d4d3b");
528528
}
529529

530530
#[test]

0 commit comments

Comments
 (0)