@@ -29,7 +29,7 @@ pub use self::internals::YearFlags as __BenchYearFlags;
2929
3030/// A week represented by a [`NaiveDate`] and a [`Weekday`] which is the first
3131/// day of the week.
32- #[ derive( Clone , Copy , Debug , Eq , Hash , PartialEq ) ]
32+ #[ derive( Clone , Copy , Debug , Eq ) ]
3333pub struct NaiveWeek {
3434 date : NaiveDate ,
3535 start : Weekday ,
@@ -206,6 +206,19 @@ impl NaiveWeek {
206206 }
207207}
208208
209+ impl PartialEq for NaiveWeek {
210+ fn eq ( & self , other : & Self ) -> bool {
211+ self . first_day ( ) == other. first_day ( )
212+ }
213+ }
214+
215+ use std:: 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+
209222/// A duration in calendar days.
210223///
211224/// This is useful because when using `TimeDelta` it is possible that adding `TimeDelta::days(1)`
@@ -235,7 +248,8 @@ pub mod serde {
235248
236249#[ cfg( test) ]
237250mod test {
238- use crate :: { NaiveDate , Weekday } ;
251+ use crate :: { NaiveDate , NaiveWeek , Weekday } ;
252+ use std:: hash:: { DefaultHasher , Hash , Hasher } ;
239253 #[ test]
240254 fn test_naiveweek ( ) {
241255 let date = NaiveDate :: from_ymd_opt ( 2022 , 5 , 18 ) . unwrap ( ) ;
@@ -278,4 +292,39 @@ mod test {
278292 let _ = date_min. week ( Weekday :: Mon ) . checked_days ( ) ;
279293 let _ = date_max. week ( Weekday :: Mon ) . checked_days ( ) ;
280294 }
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+ }
281330}
0 commit comments