librashader_cache/
key.rs

1/// Trait for objects that can be used as part of a key for a cached object.
2pub trait CacheKey {
3    /// Get a byte representation of the object that
4    /// will be fed into the hash.
5    fn hash_bytes(&self) -> &[u8];
6}
7
8impl CacheKey for u32 {
9    fn hash_bytes(&self) -> &[u8] {
10        &bytemuck::bytes_of(&*self)
11    }
12}
13
14impl CacheKey for i32 {
15    fn hash_bytes(&self) -> &[u8] {
16        &bytemuck::bytes_of(&*self)
17    }
18}
19
20impl CacheKey for &[u8] {
21    fn hash_bytes(&self) -> &[u8] {
22        self
23    }
24}
25
26impl CacheKey for Vec<u8> {
27    fn hash_bytes(&self) -> &[u8] {
28        &self
29    }
30}
31
32impl CacheKey for Vec<u32> {
33    fn hash_bytes(&self) -> &[u8] {
34        bytemuck::cast_slice(&self)
35    }
36}
37
38impl CacheKey for &str {
39    fn hash_bytes(&self) -> &[u8] {
40        // need to be explicit
41        self.as_bytes()
42    }
43}