@@ -9,16 +9,19 @@ struct MyHasher {
9
9
hash : u64 ,
10
10
}
11
11
12
- impl Default for MyHasher {
12
+ impl const Default for MyHasher {
13
13
fn default ( ) -> MyHasher {
14
14
MyHasher { hash : 0 }
15
15
}
16
16
}
17
17
18
- impl Hasher for MyHasher {
18
+ impl const Hasher for MyHasher {
19
19
fn write ( & mut self , buf : & [ u8 ] ) {
20
- for byte in buf {
21
- self . hash += * byte as u64 ;
20
+ // FIXME(const_trait_impl): change to for loop
21
+ let mut i = 0 ;
22
+ while i < buf. len ( ) {
23
+ self . hash += buf[ i] as u64 ;
24
+ i += 1 ;
22
25
}
23
26
}
24
27
fn write_str ( & mut self , s : & str ) {
@@ -32,12 +35,25 @@ impl Hasher for MyHasher {
32
35
33
36
#[ test]
34
37
fn test_writer_hasher ( ) {
35
- fn hash < T : Hash > ( t : & T ) -> u64 {
38
+ const fn hash < T : ~ const Hash > ( t : & T ) -> u64 {
36
39
let mut s = MyHasher { hash : 0 } ;
37
40
t. hash ( & mut s) ;
38
41
s. finish ( )
39
42
}
40
43
44
+ const {
45
+ // FIXME(fee1-dead): assert_eq
46
+ assert ! ( hash( & ( ) ) == 0 ) ;
47
+ assert ! ( hash( & 5_u8 ) == 5 ) ;
48
+ assert ! ( hash( & 5_u16 ) == 5 ) ;
49
+ assert ! ( hash( & 5_u32 ) == 5 ) ;
50
+
51
+ assert ! ( hash( & 'a' ) == 97 ) ;
52
+
53
+ let s: & str = "a" ;
54
+ assert ! ( hash( & s) == 97 + 0xFF ) ;
55
+ } ;
56
+
41
57
assert_eq ! ( hash( & ( ) ) , 0 ) ;
42
58
43
59
assert_eq ! ( hash( & 5_u8 ) , 5 ) ;
@@ -97,7 +113,7 @@ struct CustomHasher {
97
113
output : u64 ,
98
114
}
99
115
100
- impl Hasher for CustomHasher {
116
+ impl const Hasher for CustomHasher {
101
117
fn finish ( & self ) -> u64 {
102
118
self . output
103
119
}
@@ -109,27 +125,29 @@ impl Hasher for CustomHasher {
109
125
}
110
126
}
111
127
112
- impl Default for CustomHasher {
128
+ impl const Default for CustomHasher {
113
129
fn default ( ) -> CustomHasher {
114
130
CustomHasher { output : 0 }
115
131
}
116
132
}
117
133
118
- impl Hash for Custom {
119
- fn hash < H : Hasher > ( & self , state : & mut H ) {
134
+ impl const Hash for Custom {
135
+ fn hash < H : ~ const Hasher > ( & self , state : & mut H ) {
120
136
state. write_u64 ( self . hash ) ;
121
137
}
122
138
}
123
139
124
140
#[ test]
125
141
fn test_custom_state ( ) {
126
- fn hash < T : Hash > ( t : & T ) -> u64 {
142
+ const fn hash < T : ~ const Hash > ( t : & T ) -> u64 {
127
143
let mut c = CustomHasher { output : 0 } ;
128
144
t. hash ( & mut c) ;
129
145
c. finish ( )
130
146
}
131
147
132
148
assert_eq ! ( hash( & Custom { hash: 5 } ) , 5 ) ;
149
+
150
+ const { assert ! ( hash( & Custom { hash: 6 } ) == 6 ) } ;
133
151
}
134
152
135
153
// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
0 commit comments