File size: 6,332 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use std::mem::Discriminant;

pub use turbo_tasks_macros::DeterministicHash;

macro_rules! deterministic_hash_number {
    ($(($ty:ident, $meth:ident),)*) => {$(
        impl DeterministicHash for $ty {
            fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
                state.$meth(*self);
            }
        }
    )*}
}

macro_rules! impl_write_number {
    ($(($ty:ident, $meth:ident),)*) => {$(
        /// Writes a single `$ty` to this hasher
        #[inline]
        fn $meth(&mut self, i: $ty) {
            // Apple silicon and Intel chips both use little endian, so this should be fast.
            let little_endian = i.to_le_bytes();
            self.write_bytes(&little_endian);
        }
    )*}
}

/// Signals the implementor can safely be hashed in a replicatable way across platforms and process
/// runs.
///
/// Note that the default [`std::hash::Hash`] trait used by Rust allows for hashing that differs
/// across process runs, so it is not suitable for persistent caching with turbo-tasks.
///
/// It's very important that `Vc`s never implement this, since they cannot be deterministic. The
/// value that they wrap, however, can implement the trait.
pub trait DeterministicHash {
    /// Adds `self`'s bytes to the [`DeterministicHasher`]'s state, in a way that is replicatable on
    /// any platform or process run.
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H);
}

/// Signals the implementor can safely hash in a replicatable way across platforms and process runs.
///
/// Note that the default [`std::hash::Hash`] trait used by Rust allows for hashing that differs
/// across process runs, so it is not suitable for persistent caching with turbo-tasks.
pub trait DeterministicHasher {
    fn finish(&self) -> u64;
    fn write_bytes(&mut self, bytes: &[u8]);

    /// Writes a single `u8` to this hasher
    #[inline]
    fn write_u8(&mut self, i: u8) {
        self.write_bytes(&[i]);
    }

    /// Writes a single `usize` to this hasher
    #[inline]
    fn write_usize(&mut self, i: usize) {
        // usize can be 4 or 8 bytes, standardize on the larger.
        // As long as the original value is smaller than 4 bytes, the two will hash
        // equivalently.
        self.write_u64(i as u64);
    }

    /// Writes a single `isize` to this hasher
    #[inline]
    fn write_isize(&mut self, i: isize) {
        // isize can be 4 or 8 bytes, standardize on the larger.
        // As long as the original value is smaller than 4 bytes, the two will hash
        // equivalently.
        self.write_i64(i as i64);
    }

    impl_write_number! {
        (u16, write_u16),
        (u32, write_u32),
        (u64, write_u64),
        (i8, write_i8),
        (i16, write_i16),
        (i32, write_i32),
        (i64, write_i64),
        (u128, write_u128),
        (i128, write_i128),
    }
}

deterministic_hash_number! {
    (u8, write_u8),
    (u16, write_u16),
    (u32, write_u32),
    (u64, write_u64),
    (usize, write_usize),
    (i8, write_i8),
    (i16, write_i16),
    (i32, write_i32),
    (i64, write_i64),
    (isize, write_isize),
    (u128, write_u128),
    (i128, write_i128),
}

impl<T: ?Sized + DeterministicHash> DeterministicHash for &T {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        (**self).deterministic_hash(state);
    }
}

impl DeterministicHash for [u8] {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        state.write_bytes(self);
    }
}

impl DeterministicHash for String {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        state.write_bytes(self.as_bytes());
    }
}

impl DeterministicHash for &str {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        state.write_bytes(self.as_bytes());
    }
}

impl DeterministicHash for bool {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_u8(*self as u8);
    }
}

impl<T: DeterministicHash> DeterministicHash for Option<T> {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        match self {
            None => state.write_u8(0),
            Some(v) => {
                state.write_u8(1);
                v.deterministic_hash(state);
            }
        }
    }
}

impl<T: DeterministicHash> DeterministicHash for Vec<T> {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        state.write_usize(self.len());
        for v in self {
            v.deterministic_hash(state);
        }
    }
}

macro_rules! tuple_impls {
    ( $( $name:ident )+ ) => {
        impl<$($name: DeterministicHash),+> DeterministicHash for ($($name,)+)
        {
            #[allow(non_snake_case)]
            fn deterministic_hash<Hasher: DeterministicHasher>(&self, state: &mut Hasher) {
                let ($(ref $name,)+) = *self;
                $($name.deterministic_hash(state);)+
            }
        }
    };
}

// Implement `DeterministicHash` for all tuples of 1 to 12 elements.
tuple_impls! { A }
tuple_impls! { A B }
tuple_impls! { A B C }
tuple_impls! { A B C D }
tuple_impls! { A B C D E }
tuple_impls! { A B C D E F }
tuple_impls! { A B C D E F G }
tuple_impls! { A B C D E F G H }
tuple_impls! { A B C D E F G H I }
tuple_impls! { A B C D E F G H I J }
tuple_impls! { A B C D E F G H I J K }
tuple_impls! { A B C D E F G H I J K L }

/// HasherWrapper allows the DeterministicHasher to be used as a Hasher, for
/// standard types that do not allow us to directly access their internals.
struct HasherWrapper<'a, D: DeterministicHasher>(&'a mut D);
impl<D: DeterministicHasher> std::hash::Hasher for HasherWrapper<'_, D> {
    fn write(&mut self, bytes: &[u8]) {
        self.0.write_bytes(bytes);
    }

    fn finish(&self) -> u64 {
        unimplemented!();
    }
}

impl<T> DeterministicHash for Discriminant<T> {
    fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
        // The Discriminant does not allow us to access its internal state, but does
        // allow us to Hash it.
        let mut wrapper = HasherWrapper(state);
        std::hash::Hash::hash(self, &mut wrapper);
    }
}