@@ -29,7 +29,7 @@ pub use self::internals::YearFlags as __BenchYearFlags;
29
29
30
30
/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first
31
31
/// day of the week.
32
- #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
32
+ #[ derive( Clone , Copy , Debug , Eq ) ]
33
33
pub struct NaiveWeek {
34
34
date : NaiveDate ,
35
35
start : Weekday ,
@@ -206,6 +206,19 @@ impl NaiveWeek {
206
206
}
207
207
}
208
208
209
+ impl PartialEq for NaiveWeek {
210
+ fn eq ( & self , other : & Self ) -> bool {
211
+ self . first_day ( ) == other. first_day ( )
212
+ }
213
+ }
214
+
215
+ use core:: hash:: { Hash , Hasher } ;
216
+ impl Hash for NaiveWeek {
217
+ fn hash < H : Hasher > ( & self , state : & mut H ) {
218
+ self . first_day ( ) . hash ( state) ;
219
+ }
220
+ }
221
+
209
222
/// A duration in calendar days.
210
223
///
211
224
/// This is useful because when using `TimeDelta` it is possible that adding `TimeDelta::days(1)`
@@ -235,7 +248,8 @@ pub mod serde {
235
248
236
249
#[ cfg( test) ]
237
250
mod test {
238
- use crate :: { NaiveDate , Weekday } ;
251
+ use crate :: { NaiveDate , NaiveWeek , Weekday } ;
252
+ use std:: hash:: { DefaultHasher , Hash , Hasher } ;
239
253
#[ test]
240
254
fn test_naiveweek ( ) {
241
255
let date = NaiveDate :: from_ymd_opt ( 2022 , 5 , 18 ) . unwrap ( ) ;
@@ -278,4 +292,39 @@ mod test {
278
292
let _ = date_min. week ( Weekday :: Mon ) . checked_days ( ) ;
279
293
let _ = date_max. week ( Weekday :: Mon ) . checked_days ( ) ;
280
294
}
295
+
296
+ #[ test]
297
+ fn test_naiveweek_eq ( ) {
298
+ let a =
299
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 3 ) . unwrap ( ) , start : Weekday :: Mon } ;
300
+ let b =
301
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 4 ) . unwrap ( ) , start : Weekday :: Mon } ;
302
+ assert_eq ! ( a, b) ;
303
+ let c =
304
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 3 ) . unwrap ( ) , start : Weekday :: Sun } ;
305
+ assert_ne ! ( a, c) ;
306
+ assert_ne ! ( b, c) ;
307
+ }
308
+
309
+ #[ test]
310
+ fn test_naiveweek_hash ( ) {
311
+ let mut hasher = DefaultHasher :: default ( ) ;
312
+ let a =
313
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 3 ) . unwrap ( ) , start : Weekday :: Mon } ;
314
+ let b =
315
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 4 ) . unwrap ( ) , start : Weekday :: Mon } ;
316
+ let c =
317
+ NaiveWeek { date : NaiveDate :: from_ymd_opt ( 2025 , 4 , 3 ) . unwrap ( ) , start : Weekday :: Sun } ;
318
+ a. hash ( & mut hasher) ;
319
+ let a_hash = hasher. finish ( ) ;
320
+ hasher = DefaultHasher :: default ( ) ;
321
+ b. hash ( & mut hasher) ;
322
+ let b_hash = hasher. finish ( ) ;
323
+ hasher = DefaultHasher :: default ( ) ;
324
+ c. hash ( & mut hasher) ;
325
+ let c_hash = hasher. finish ( ) ;
326
+ assert_eq ! ( a_hash, b_hash) ;
327
+ assert_ne ! ( b_hash, c_hash) ;
328
+ assert_ne ! ( a_hash, c_hash) ;
329
+ }
281
330
}
0 commit comments