@@ -9,16 +9,19 @@ struct MyHasher {
99 hash : u64 ,
1010}
1111
12- impl Default for MyHasher {
12+ impl const Default for MyHasher {
1313 fn default ( ) -> MyHasher {
1414 MyHasher { hash : 0 }
1515 }
1616}
1717
18- impl Hasher for MyHasher {
18+ impl const Hasher for MyHasher {
1919 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 ;
2225 }
2326 }
2427 fn write_str ( & mut self , s : & str ) {
@@ -32,12 +35,25 @@ impl Hasher for MyHasher {
3235
3336#[ test]
3437fn test_writer_hasher ( ) {
35- fn hash < T : Hash > ( t : & T ) -> u64 {
38+ const fn hash < T : ~ const Hash > ( t : & T ) -> u64 {
3639 let mut s = MyHasher { hash : 0 } ;
3740 t. hash ( & mut s) ;
3841 s. finish ( )
3942 }
4043
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+
4157 assert_eq ! ( hash( & ( ) ) , 0 ) ;
4258
4359 assert_eq ! ( hash( & 5_u8 ) , 5 ) ;
@@ -97,7 +113,7 @@ struct CustomHasher {
97113 output : u64 ,
98114}
99115
100- impl Hasher for CustomHasher {
116+ impl const Hasher for CustomHasher {
101117 fn finish ( & self ) -> u64 {
102118 self . output
103119 }
@@ -109,27 +125,29 @@ impl Hasher for CustomHasher {
109125 }
110126}
111127
112- impl Default for CustomHasher {
128+ impl const Default for CustomHasher {
113129 fn default ( ) -> CustomHasher {
114130 CustomHasher { output : 0 }
115131 }
116132}
117133
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 ) {
120136 state. write_u64 ( self . hash ) ;
121137 }
122138}
123139
124140#[ test]
125141fn test_custom_state ( ) {
126- fn hash < T : Hash > ( t : & T ) -> u64 {
142+ const fn hash < T : ~ const Hash > ( t : & T ) -> u64 {
127143 let mut c = CustomHasher { output : 0 } ;
128144 t. hash ( & mut c) ;
129145 c. finish ( )
130146 }
131147
132148 assert_eq ! ( hash( & Custom { hash: 5 } ) , 5 ) ;
149+
150+ const { assert ! ( hash( & Custom { hash: 6 } ) == 6 ) } ;
133151}
134152
135153// FIXME: Instantiated functions with i128 in the signature is not supported in Emscripten.
0 commit comments