1
1
//! rustc encodes a lot of hashes. If hashes are stored as `u64` or `u128`, a `derive(Encodable)`
2
2
//! will apply varint encoding to the hashes, which is less efficient than directly encoding the 8
3
- //! or 16 bytes of the hash.
3
+ //! or 16 bytes of the hash. And if that hash depends on the `StableCrateHash` (which most in rustc
4
+ //! do), the varint encoding will make the number of bytes encoded fluctuate between compiler
5
+ //! versions.
4
6
//!
5
7
//! The types in this module represent 64-bit or 128-bit hashes produced by a `StableHasher`.
6
8
//! `Hash64` and `Hash128` expose some utility functions to encourage users to not extract the inner
14
16
use std:: fmt;
15
17
use std:: ops:: BitXorAssign ;
16
18
17
- use rustc_serialize:: { Decodable , Decoder , Encodable , Encoder } ;
18
-
19
- use crate :: stable_hasher:: { FromStableHash , StableHasherHash } ;
19
+ use rustc_stable_hash:: { FromStableHash , SipHasher128Hash as StableHasherHash } ;
20
20
21
+ /// A `u64` but encoded with a fixed size; for hashes this encoding is more compact than `u64`.
21
22
#[ derive( Clone , Copy , PartialEq , Eq , Hash , PartialOrd , Ord , Default ) ]
22
23
pub struct Hash64 {
23
24
inner : u64 ,
@@ -49,20 +50,6 @@ impl BitXorAssign<u64> for Hash64 {
49
50
}
50
51
}
51
52
52
- impl < S : Encoder > Encodable < S > for Hash64 {
53
- #[ inline]
54
- fn encode ( & self , s : & mut S ) {
55
- s. emit_raw_bytes ( & self . inner . to_le_bytes ( ) ) ;
56
- }
57
- }
58
-
59
- impl < D : Decoder > Decodable < D > for Hash64 {
60
- #[ inline]
61
- fn decode ( d : & mut D ) -> Self {
62
- Self { inner : u64:: from_le_bytes ( d. read_raw_bytes ( 8 ) . try_into ( ) . unwrap ( ) ) }
63
- }
64
- }
65
-
66
53
impl FromStableHash for Hash64 {
67
54
type Hash = StableHasherHash ;
68
55
@@ -84,6 +71,7 @@ impl fmt::LowerHex for Hash64 {
84
71
}
85
72
}
86
73
74
+ /// A `u128` but encoded with a fixed size; for hashes this encoding is more compact than `u128`.
87
75
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Default ) ]
88
76
pub struct Hash128 {
89
77
inner : u128 ,
@@ -100,6 +88,11 @@ impl std::hash::Hash for Hash128 {
100
88
}
101
89
102
90
impl Hash128 {
91
+ #[ inline]
92
+ pub fn new ( n : u128 ) -> Self {
93
+ Self { inner : n }
94
+ }
95
+
103
96
#[ inline]
104
97
pub fn truncate ( self ) -> Hash64 {
105
98
Hash64 { inner : self . inner as u64 }
@@ -116,20 +109,6 @@ impl Hash128 {
116
109
}
117
110
}
118
111
119
- impl < S : Encoder > Encodable < S > for Hash128 {
120
- #[ inline]
121
- fn encode ( & self , s : & mut S ) {
122
- s. emit_raw_bytes ( & self . inner . to_le_bytes ( ) ) ;
123
- }
124
- }
125
-
126
- impl < D : Decoder > Decodable < D > for Hash128 {
127
- #[ inline]
128
- fn decode ( d : & mut D ) -> Self {
129
- Self { inner : u128:: from_le_bytes ( d. read_raw_bytes ( 16 ) . try_into ( ) . unwrap ( ) ) }
130
- }
131
- }
132
-
133
112
impl FromStableHash for Hash128 {
134
113
type Hash = StableHasherHash ;
135
114
0 commit comments