@@ -8,8 +8,6 @@ use std::{
8
8
use opentelemetry:: { Array , Key , KeyValue , Value } ;
9
9
use ordered_float:: OrderedFloat ;
10
10
11
- use crate :: Resource ;
12
-
13
11
#[ derive( Clone , Debug ) ]
14
12
struct HashKeyValue ( KeyValue ) ;
15
13
@@ -87,17 +85,6 @@ impl From<&[KeyValue]> for AttributeSet {
87
85
}
88
86
}
89
87
90
- impl From < & Resource > for AttributeSet {
91
- fn from ( values : & Resource ) -> Self {
92
- let vec = values
93
- . iter ( )
94
- . map ( |( key, value) | HashKeyValue ( KeyValue :: new ( key. clone ( ) , value. clone ( ) ) ) )
95
- . collect :: < Vec < _ > > ( ) ;
96
-
97
- AttributeSet :: new ( vec)
98
- }
99
- }
100
-
101
88
fn calculate_hash ( values : & [ HashKeyValue ] ) -> u64 {
102
89
let mut hasher = DefaultHasher :: new ( ) ;
103
90
values. iter ( ) . fold ( & mut hasher, |mut hasher, item| {
@@ -146,3 +133,52 @@ impl Hash for AttributeSet {
146
133
state. write_u64 ( self . 1 )
147
134
}
148
135
}
136
+
137
+ #[ cfg( test) ]
138
+ mod tests {
139
+ use std:: hash:: DefaultHasher ;
140
+ use std:: hash:: { Hash , Hasher } ;
141
+
142
+ use crate :: attributes:: set:: HashKeyValue ;
143
+ use opentelemetry:: KeyValue ;
144
+
145
+ #[ test]
146
+ fn equality_kv_float ( ) {
147
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
148
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
149
+ assert_eq ! ( kv1, kv2) ;
150
+
151
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
152
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.01 ) ) ;
153
+ assert_ne ! ( kv1, kv2) ;
154
+
155
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
156
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
157
+ assert_eq ! ( kv1, kv2) ;
158
+
159
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
160
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
161
+ assert_eq ! ( kv1, kv2) ;
162
+ }
163
+
164
+ #[ test]
165
+ fn hash_kv_float ( ) {
166
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
167
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , 1.0 ) ) ;
168
+ assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
169
+
170
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
171
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: NAN ) ) ;
172
+ assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
173
+
174
+ let kv1 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
175
+ let kv2 = HashKeyValue ( KeyValue :: new ( "key" , std:: f64:: INFINITY ) ) ;
176
+ assert_eq ! ( hash_helper( & kv1) , hash_helper( & kv2) ) ;
177
+ }
178
+
179
+ fn hash_helper < T : Hash > ( item : & T ) -> u64 {
180
+ let mut hasher = DefaultHasher :: new ( ) ;
181
+ item. hash ( & mut hasher) ;
182
+ hasher. finish ( )
183
+ }
184
+ }
0 commit comments