Skip to content

Commit 6c7a748

Browse files
committed
refactor: use DefaultHasher instead of custom fnv hash implementation
1 parent 790f736 commit 6c7a748

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

sailfish-compiler/src/procmacro.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use proc_macro2::{Span, TokenStream};
22
use quote::{quote, ToTokens};
3+
use std::collections::hash_map::DefaultHasher;
34
use std::env;
5+
use std::hash::{Hash, Hasher};
46
use std::path::{Path, PathBuf};
57
use syn::parse::{Parse, ParseStream, Result as ParseResult};
68
use syn::punctuated::Punctuated;
@@ -134,31 +136,23 @@ fn resolve_template_file(path: &str, template_dirs: &[PathBuf]) -> Option<PathBu
134136
fn filename_hash(path: &Path) -> String {
135137
use std::fmt::Write;
136138

137-
const FNV_PRIME: u64 = 1_099_511_628_211;
138-
const FNV_OFFSET_BASIS: u64 = 14_695_981_039_346_656_037;
139-
140-
let mut hash = String::with_capacity(16);
139+
let mut path_with_hash = String::with_capacity(16);
141140

142141
if let Some(n) = path.file_name() {
143142
let mut filename = &*n.to_string_lossy();
144143
if let Some(p) = filename.find('.') {
145144
filename = &filename[..p];
146145
}
147-
hash.push_str(filename);
148-
hash.push('-');
149-
}
150-
151-
// calculate 64bit hash
152-
let mut h = FNV_OFFSET_BASIS;
153-
for b in (&*path.to_string_lossy()).bytes() {
154-
h = h.wrapping_mul(FNV_PRIME);
155-
h ^= b as u64;
146+
path_with_hash.push_str(filename);
147+
path_with_hash.push('-');
156148
}
157149

158-
// convert 64bit hash into ascii
159-
let _ = write!(hash, "{:016x}", h);
150+
let mut hasher = DefaultHasher::new();
151+
path.hash(&mut hasher);
152+
let hash = hasher.finish();
153+
let _ = write!(path_with_hash, "{:016x}", hash);
160154

161-
hash
155+
path_with_hash
162156
}
163157

164158
fn compile(

0 commit comments

Comments
 (0)