-
Notifications
You must be signed in to change notification settings - Fork 226
Optimize Date::to_calendar
#7089
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ use crate::cal::iso::IsoDateInner; | |
use crate::error::{DateError, DateFromFieldsError}; | ||
use crate::options::DateFromFieldsOptions; | ||
use crate::options::{DateAddOptions, DateDifferenceOptions}; | ||
use crate::types; | ||
use crate::{types, Iso}; | ||
use core::fmt; | ||
|
||
/// A calendar implementation | ||
|
@@ -56,11 +56,23 @@ pub trait Calendar: crate::cal::scaffold::UnstableSealed { | |
options: DateFromFieldsOptions, | ||
) -> Result<Self::DateInner, DateFromFieldsError>; | ||
|
||
/// Construct the date from an ISO date | ||
/// Whether `from_iso`/`to_iso` is more efficient | ||
/// than `from_rata_die`/`to_rata_die`. | ||
const HAS_CHEAP_ISO_CONVERSION: bool; | ||
|
||
/// Construct the date from an ISO date. | ||
/// | ||
/// Only called if `HAS_CHEAP_ISO_CONVERSION` is set. | ||
#[expect(clippy::wrong_self_convention)] | ||
fn from_iso(&self, iso: IsoDateInner) -> Self::DateInner; | ||
/// Obtain an ISO date from this date | ||
fn to_iso(&self, date: &Self::DateInner) -> IsoDateInner; | ||
fn from_iso(&self, iso: IsoDateInner) -> Self::DateInner { | ||
self.from_rata_die(Iso.to_rata_die(&iso)) | ||
} | ||
/// Obtain an ISO date from this date. | ||
/// | ||
/// Only called if `HAS_CHEAP_ISO_CONVERSION` is set. | ||
fn to_iso(&self, date: &Self::DateInner) -> IsoDateInner { | ||
Iso.from_rata_die(self.to_rata_die(date)) | ||
} | ||
Comment on lines
+67
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: If this code isn't called unless the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, the "Only called..." part is new, and we consider this callable-stable. I also really dislike debug asserts |
||
|
||
/// Construct the date from a [`RataDie`] | ||
#[expect(clippy::wrong_self_convention)] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Another design here would be
from_iso
would retain the default impl as you have here. Then the Date code would beAll of the
cheap_to_iso
impls should be marked as#[inline]
so that DCE works nicely and avoids the branch.I think all the pairs work:
cheap_to_iso
andfrom_iso
. OKto_rata_die
andfrom_rata_die
. OKcheap_to_iso
andfrom_iso
, wherefrom_iso
internally uses the default implto_rata_die
andfrom_rata_die
. OKto_rata_die
andfrom_rata_die
. OKThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the solution here is basically this.