13
13
14
14
use ich:: StableHashingContext ;
15
15
16
+ use std:: cmp;
16
17
use std:: hash as std_hash;
17
18
use std:: mem;
18
19
@@ -29,6 +30,30 @@ use rustc_data_structures::stable_hasher::{HashStable, ToStableHashKey,
29
30
StableHasher , StableHasherResult } ;
30
31
use rustc_data_structures:: accumulate_vec:: AccumulateVec ;
31
32
33
+ /// InternedString is not compared lexicographically but by pointer value. So,
34
+ /// in order to use it as a stable key we introduce a wrapper type with
35
+ /// the correct Ord, Eq, and Hash implementations.
36
+ #[ derive( Eq , Ord , Clone , Copy ) ]
37
+ pub struct InternedStringSortStable ( InternedString ) ;
38
+
39
+ impl std_hash:: Hash for InternedStringSortStable {
40
+ fn hash < H : std_hash:: Hasher > ( & self , state : & mut H ) {
41
+ std_hash:: Hash :: hash ( & self . 0 [ ..] , state) ;
42
+ }
43
+ }
44
+
45
+ impl cmp:: PartialOrd for InternedStringSortStable {
46
+ fn partial_cmp ( & self , other : & InternedStringSortStable ) -> Option < cmp:: Ordering > {
47
+ ( & self . 0 [ ..] ) . partial_cmp ( & other. 0 [ ..] )
48
+ }
49
+ }
50
+
51
+ impl cmp:: PartialEq for InternedStringSortStable {
52
+ fn eq ( & self , other : & InternedStringSortStable ) -> bool {
53
+ ( & self . 0 [ ..] ) . eq ( & other. 0 [ ..] )
54
+ }
55
+ }
56
+
32
57
impl < ' a > HashStable < StableHashingContext < ' a > > for InternedString {
33
58
#[ inline]
34
59
fn hash_stable < W : StableHasherResult > ( & self ,
@@ -39,14 +64,16 @@ impl<'a> HashStable<StableHashingContext<'a>> for InternedString {
39
64
}
40
65
}
41
66
67
+ impl_stable_hash_for ! ( tuple_struct self :: InternedStringSortStable { inner } ) ;
68
+
42
69
impl < ' a > ToStableHashKey < StableHashingContext < ' a > > for InternedString {
43
- type KeyType = InternedString ;
70
+ type KeyType = InternedStringSortStable ;
44
71
45
72
#[ inline]
46
73
fn to_stable_hash_key ( & self ,
47
74
_: & StableHashingContext < ' a > )
48
- -> InternedString {
49
- self . clone ( )
75
+ -> InternedStringSortStable {
76
+ InternedStringSortStable ( self . clone ( ) )
50
77
}
51
78
}
52
79
@@ -60,13 +87,13 @@ impl<'a> HashStable<StableHashingContext<'a>> for ast::Name {
60
87
}
61
88
62
89
impl < ' a > ToStableHashKey < StableHashingContext < ' a > > for ast:: Name {
63
- type KeyType = InternedString ;
90
+ type KeyType = InternedStringSortStable ;
64
91
65
92
#[ inline]
66
93
fn to_stable_hash_key ( & self ,
67
94
_: & StableHashingContext < ' a > )
68
- -> InternedString {
69
- self . as_str ( )
95
+ -> InternedStringSortStable {
96
+ InternedStringSortStable ( self . as_str ( ) )
70
97
}
71
98
}
72
99
0 commit comments