Split puffin-cache into Puffin-specific and generic utilities (#728)

This crate started off as generic caching utilities, but we started
adding a lot of Puffin-specific stuff (like the cache buckets
abstraction that knows about Git vs. direct URL vs. indexes and so on).
This PR moves the generic stuff into a new `cache-key` crate.
This commit is contained in:
Charlie Marsh 2023-12-25 09:38:56 -05:00 committed by GitHub
parent 4acf02f6b3
commit 6ff21374dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 83 additions and 50 deletions

View file

@ -0,0 +1,22 @@
use std::hash::Hasher;
use crate::cache_key::{CacheKey, CacheKeyHasher};
/// Compute a hex string hash of a `CacheKey` object.
///
/// The value returned by [`digest`] should be stable across releases and platforms.
pub fn digest<H: CacheKey>(hashable: &H) -> String {
to_hex(cache_key_u64(hashable))
}
/// Convert a u64 to a hex string.
fn to_hex(num: u64) -> String {
hex::encode(num.to_le_bytes())
}
/// Compute a u64 hash of a [`CacheKey`] object.
fn cache_key_u64<H: CacheKey>(hashable: &H) -> u64 {
let mut hasher = CacheKeyHasher::new();
hashable.cache_key(&mut hasher);
hasher.finish()
}