Skip to content

Commit 5a2d04a

Browse files
committed
Add PartiaEq, remove equals method, and add docs
1 parent ae2e945 commit 5a2d04a

File tree

5 files changed

+121
-102
lines changed

5 files changed

+121
-102
lines changed

Diff for: src/builtins/core/date.rs

+59-45
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ macro_rules! impl_with_fallback_method {
127127

128128
/// The native Rust implementation of `Temporal.PlainDate`.
129129
#[non_exhaustive]
130-
#[derive(Debug, Default, Clone)]
130+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
131131
pub struct PlainDate {
132132
pub(crate) iso: IsoDate,
133133
calendar: Calendar,
@@ -435,15 +435,17 @@ impl PlainDate {
435435
other.iso.to_epoch_days() - self.iso.to_epoch_days()
436436
}
437437

438+
/// Compares one `PlainDate` to another `PlainDate` using their
439+
/// `IsoDate` representation.
440+
///
441+
/// # Note on Ordering.
442+
///
443+
/// `temporal_rs` does not implement `PartialOrd`/`Ord` as `PlainDate` does
444+
/// not fulfill all the conditions required to implement the traits. However,
445+
/// it is possible to compare `PlainDate`'s as their `IsoDate` representation.
438446
#[inline]
439447
#[must_use]
440-
pub fn equals(&self, other: &Self) -> bool {
441-
self.compare(other) == Ordering::Equal && self.calendar == other.calendar
442-
}
443-
444-
#[inline]
445-
#[must_use]
446-
pub fn compare(&self, other: &Self) -> Ordering {
448+
pub fn compare_iso(&self, other: &Self) -> Ordering {
447449
self.iso.cmp(&other.iso)
448450
}
449451

@@ -640,25 +642,31 @@ mod tests {
640642
assert!(err.is_err());
641643
let err = PlainDate::try_new(275_760, 9, 14, Calendar::default());
642644
assert!(err.is_err());
643-
let ok = PlainDate::try_new(-271_821, 4, 19, Calendar::default()).unwrap();
644-
assert!(ok.equals(&PlainDate {
645-
iso: IsoDate {
646-
year: -271_821,
647-
month: 4,
648-
day: 19,
649-
},
650-
calendar: Calendar::default(),
651-
}));
652-
653-
let ok = PlainDate::try_new(275_760, 9, 13, Calendar::default()).unwrap();
654-
assert!(ok.equals(&PlainDate {
655-
iso: IsoDate {
656-
year: 275760,
657-
month: 9,
658-
day: 13,
659-
},
660-
calendar: Calendar::default(),
661-
}));
645+
let ok = PlainDate::try_new(-271_821, 4, 19, Calendar::default());
646+
assert_eq!(
647+
ok,
648+
Ok(PlainDate {
649+
iso: IsoDate {
650+
year: -271_821,
651+
month: 4,
652+
day: 19,
653+
},
654+
calendar: Calendar::default(),
655+
})
656+
);
657+
658+
let ok = PlainDate::try_new(275_760, 9, 13, Calendar::default());
659+
assert_eq!(
660+
ok,
661+
Ok(PlainDate {
662+
iso: IsoDate {
663+
year: 275760,
664+
month: 9,
665+
day: 13,
666+
},
667+
calendar: Calendar::default(),
668+
})
669+
);
662670
}
663671

664672
#[test]
@@ -710,30 +718,36 @@ mod tests {
710718
assert!(result.is_err());
711719

712720
let max = PlainDate::try_new(275_760, 9, 12, Calendar::default()).unwrap();
713-
let result = max.add(&Duration::from_str("P1D").unwrap(), None).unwrap();
714-
assert!(result.equals(&PlainDate {
715-
iso: IsoDate {
716-
year: 275760,
717-
month: 9,
718-
day: 13
719-
},
720-
calendar: Calendar::default(),
721-
}));
721+
let result = max.add(&Duration::from_str("P1D").unwrap(), None);
722+
assert_eq!(
723+
result,
724+
Ok(PlainDate {
725+
iso: IsoDate {
726+
year: 275760,
727+
month: 9,
728+
day: 13
729+
},
730+
calendar: Calendar::default(),
731+
})
732+
);
722733

723734
let min = PlainDate::try_new(-271_821, 4, 19, Calendar::default()).unwrap();
724735
let result = min.add(&Duration::from_str("-P1D").unwrap(), None);
725736
assert!(result.is_err());
726737

727738
let min = PlainDate::try_new(-271_821, 4, 20, Calendar::default()).unwrap();
728-
let result = min.add(&Duration::from_str("-P1D").unwrap(), None).unwrap();
729-
assert!(result.equals(&PlainDate {
730-
iso: IsoDate {
731-
year: -271_821,
732-
month: 4,
733-
day: 19
734-
},
735-
calendar: Calendar::default(),
736-
}));
739+
let result = min.add(&Duration::from_str("-P1D").unwrap(), None);
740+
assert_eq!(
741+
result,
742+
Ok(PlainDate {
743+
iso: IsoDate {
744+
year: -271_821,
745+
month: 4,
746+
day: 19
747+
},
748+
calendar: Calendar::default(),
749+
})
750+
);
737751
}
738752

739753
#[test]

Diff for: src/builtins/core/datetime.rs

+39-31
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub struct PartialDateTime {
3030

3131
/// The native Rust implementation of `Temporal.PlainDateTime`
3232
#[non_exhaustive]
33-
#[derive(Debug, Default, Clone)]
33+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
3434
pub struct PlainDateTime {
3535
pub(crate) iso: IsoDateTime,
3636
calendar: Calendar,
@@ -563,15 +563,17 @@ impl PlainDateTime {
563563
}
564564

565565
impl PlainDateTime {
566+
/// Compares one `PlainDateTime` to another `PlainDateTime` using their
567+
/// `IsoDate` representation.
568+
///
569+
/// # Note on Ordering.
570+
///
571+
/// `temporal_rs` does not implement `PartialOrd`/`Ord` as `PlainDateTime` does
572+
/// not fulfill all the conditions required to implement the traits. However,
573+
/// it is possible to compare `PlainDate`'s as their `IsoDate` representation.
566574
#[inline]
567575
#[must_use]
568-
pub fn equals(&self, other: &Self) -> bool {
569-
self.compare(other) == Ordering::Equal && self.calendar == other.calendar
570-
}
571-
572-
#[inline]
573-
#[must_use]
574-
pub fn compare(&self, other: &Self) -> Ordering {
576+
pub fn compare_iso(&self, other: &Self) -> Ordering {
575577
self.iso.cmp(&other.iso)
576578
}
577579

@@ -735,31 +737,37 @@ mod tests {
735737
assert!(negative_limit.is_err());
736738
let positive_limit = pdt_from_date(275_760, 9, 14);
737739
assert!(positive_limit.is_err());
738-
let within_negative_limit = pdt_from_date(-271_821, 4, 20).unwrap();
739-
assert!(within_negative_limit.equals(&PlainDateTime {
740-
iso: IsoDateTime {
741-
date: IsoDate {
742-
year: -271_821,
743-
month: 4,
744-
day: 20,
740+
let within_negative_limit = pdt_from_date(-271_821, 4, 20);
741+
assert_eq!(
742+
within_negative_limit,
743+
Ok(PlainDateTime {
744+
iso: IsoDateTime {
745+
date: IsoDate {
746+
year: -271_821,
747+
month: 4,
748+
day: 20,
749+
},
750+
time: IsoTime::default(),
745751
},
746-
time: IsoTime::default(),
747-
},
748-
calendar: Calendar::default(),
749-
}));
750-
751-
let within_positive_limit = pdt_from_date(275_760, 9, 13).unwrap();
752-
assert!(within_positive_limit.equals(&PlainDateTime {
753-
iso: IsoDateTime {
754-
date: IsoDate {
755-
year: 275_760,
756-
month: 9,
757-
day: 13,
752+
calendar: Calendar::default(),
753+
})
754+
);
755+
756+
let within_positive_limit = pdt_from_date(275_760, 9, 13);
757+
assert_eq!(
758+
within_positive_limit,
759+
Ok(PlainDateTime {
760+
iso: IsoDateTime {
761+
date: IsoDate {
762+
year: 275_760,
763+
month: 9,
764+
day: 13,
765+
},
766+
time: IsoTime::default(),
758767
},
759-
time: IsoTime::default(),
760-
},
761-
calendar: Calendar::default(),
762-
}));
768+
calendar: Calendar::default(),
769+
})
770+
);
763771
}
764772

765773
#[test]

Diff for: src/builtins/core/month_day.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! This module implements `MonthDay` and any directly related algorithms.
22
33
use alloc::string::String;
4-
use core::{cmp::Ordering, str::FromStr};
4+
use core::str::FromStr;
55

66
use tinystr::TinyAsciiStr;
77

@@ -14,7 +14,7 @@ use crate::{
1414

1515
/// The native Rust implementation of `Temporal.PlainMonthDay`
1616
#[non_exhaustive]
17-
#[derive(Debug, Default, Clone)]
17+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
1818
pub struct PlainMonthDay {
1919
pub iso: IsoDate,
2020
calendar: Calendar,
@@ -90,11 +90,6 @@ impl PlainMonthDay {
9090
self.calendar.month_code(&self.iso)
9191
}
9292

93-
#[inline]
94-
pub fn equals(&self, other: &Self) -> bool {
95-
self.iso.cmp(&other.iso) == Ordering::Equal && self.calendar == other.calendar
96-
}
97-
9893
pub fn to_ixdtf_string(&self, display_calendar: DisplayCalendar) -> String {
9994
let ixdtf = FormattableMonthDay {
10095
date: FormattableDate(self.iso_year(), self.iso_month(), self.iso.day),

Diff for: src/builtins/core/year_month.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::{Duration, PartialDate};
1717

1818
/// The native Rust implementation of `Temporal.YearMonth`.
1919
#[non_exhaustive]
20-
#[derive(Debug, Default, Clone)]
20+
#[derive(Debug, Default, Clone, PartialEq, Eq)]
2121
pub struct PlainYearMonth {
2222
pub(crate) iso: IsoDate,
2323
calendar: Calendar,
@@ -137,15 +137,17 @@ impl PlainYearMonth {
137137
self.calendar.identifier()
138138
}
139139

140+
/// Compares one `PlainYearMonth` to another `PlainYearMonth` using their
141+
/// `IsoDate` representation.
142+
///
143+
/// # Note on Ordering.
144+
///
145+
/// `temporal_rs` does not implement `PartialOrd`/`Ord` as `PlainYearMonth` does
146+
/// not fulfill all the conditions required to implement the traits. However,
147+
/// it is possible to compare `PlainDate`'s as their `IsoDate` representation.
140148
#[inline]
141149
#[must_use]
142-
pub fn equals(&self, other: &Self) -> bool {
143-
self.compare(other) == Ordering::Equal && self.calendar == other.calendar
144-
}
145-
146-
#[inline]
147-
#[must_use]
148-
pub fn compare(&self, other: &Self) -> Ordering {
150+
pub fn compare_iso(&self, other: &Self) -> Ordering {
149151
self.iso.cmp(&other.iso)
150152
}
151153

Diff for: src/builtins/core/zoneddatetime.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::{
2929
};
3030

3131
/// A struct representing a partial `ZonedDateTime`.
32-
#[derive(Debug, Default, Clone)]
32+
#[derive(Debug, Default, Clone, PartialEq)]
3333
pub struct PartialZonedDateTime {
3434
/// The `PartialDate` portion of a `PartialZonedDateTime`
3535
pub date: PartialDate,
@@ -43,7 +43,7 @@ pub struct PartialZonedDateTime {
4343

4444
/// The native Rust implementation of `Temporal.ZonedDateTime`.
4545
#[non_exhaustive]
46-
#[derive(Debug, Clone, PartialEq)]
46+
#[derive(Debug, Clone, PartialEq, Eq)]
4747
pub struct ZonedDateTime {
4848
instant: Instant,
4949
calendar: Calendar,
@@ -421,17 +421,17 @@ impl ZonedDateTime {
421421
Self::try_new(self.epoch_nanoseconds(), calendar, self.tz.clone())
422422
}
423423

424+
/// Compares one `ZonedDateTime` to another `ZonedDateTime` using their
425+
/// `Instant` representation.
426+
///
427+
/// # Note on Ordering.
428+
///
429+
/// `temporal_rs` does not implement `PartialOrd`/`Ord` as `ZonedDateTime` does
430+
/// not fulfill all the conditions required to implement the traits. However,
431+
/// it is possible to compare `PlainDate`'s as their `IsoDate` representation.
424432
#[inline]
425433
#[must_use]
426-
pub fn equals(&self, other: &Self) -> bool {
427-
self.compare(other) == Ordering::Equal
428-
&& self.tz == other.tz
429-
&& self.calendar == other.calendar
430-
}
431-
432-
#[inline]
433-
#[must_use]
434-
pub fn compare(&self, other: &Self) -> Ordering {
434+
pub fn compare_instant(&self, other: &Self) -> Ordering {
435435
self.instant.cmp(&other.instant)
436436
}
437437
}

0 commit comments

Comments
 (0)