librashader_cache/
key.rs

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