@@ -60,8 +60,19 @@ impl FromStableHash for Hash64 {
60
60
type Hash = StableHasherHash ;
61
61
62
62
#[ inline]
63
- fn from ( StableHasherHash ( [ _0, __1] ) : Self :: Hash ) -> Self {
64
- Self { inner : _0 }
63
+ fn from ( StableHasherHash ( bytes) : Self :: Hash ) -> Self {
64
+ let p0 = u64:: from_le_bytes ( bytes[ 0 ..8 ] . try_into ( ) . unwrap ( ) ) ;
65
+ let p1 = u64:: from_le_bytes ( bytes[ 8 ..16 ] . try_into ( ) . unwrap ( ) ) ;
66
+ let p2 = u64:: from_le_bytes ( bytes[ 16 ..24 ] . try_into ( ) . unwrap ( ) ) ;
67
+ let p3 = u64:: from_le_bytes ( bytes[ 24 ..32 ] . try_into ( ) . unwrap ( ) ) ;
68
+
69
+ // See https://stackoverflow.com/a/27952689 on why this function is
70
+ // implemented this way.
71
+ let m0 = p0. wrapping_mul ( 3 ) . wrapping_add ( p1) ;
72
+ let m1 = p2. wrapping_mul ( 3 ) . wrapping_add ( p3) ;
73
+ let h = m0. wrapping_mul ( 3 ) . wrapping_add ( m1) ;
74
+
75
+ Self { inner : h }
65
76
}
66
77
}
67
78
@@ -127,8 +138,18 @@ impl FromStableHash for Hash128 {
127
138
type Hash = StableHasherHash ;
128
139
129
140
#[ inline]
130
- fn from ( StableHasherHash ( [ _0, _1] ) : Self :: Hash ) -> Self {
131
- Self { inner : u128:: from ( _0) | ( u128:: from ( _1) << 64 ) }
141
+ fn from ( StableHasherHash ( bytes) : Self :: Hash ) -> Self {
142
+ let p0 = u64:: from_le_bytes ( bytes[ 0 ..8 ] . try_into ( ) . unwrap ( ) ) ;
143
+ let p1 = u64:: from_le_bytes ( bytes[ 8 ..16 ] . try_into ( ) . unwrap ( ) ) ;
144
+ let p2 = u64:: from_le_bytes ( bytes[ 16 ..24 ] . try_into ( ) . unwrap ( ) ) ;
145
+ let p3 = u64:: from_le_bytes ( bytes[ 24 ..32 ] . try_into ( ) . unwrap ( ) ) ;
146
+
147
+ // See https://stackoverflow.com/a/27952689 on why this function is
148
+ // implemented this way.
149
+ let upper = p0. wrapping_mul ( 3 ) . wrapping_add ( p1) ;
150
+ let lower = p2. wrapping_mul ( 3 ) . wrapping_add ( p3) ;
151
+
152
+ Self { inner : u128:: from ( lower) | ( u128:: from ( upper) << 64 ) }
132
153
}
133
154
}
134
155
0 commit comments