Skip to content

Commit

Permalink
All internals should have be named "unchecked"
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 18, 2019
1 parent 17c1a2e commit 7528763
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
24 changes: 12 additions & 12 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub const fn days_in_year(year: i32) -> u16 {
/// ```
#[inline(always)]
pub fn weeks_in_year(year: i32) -> u8 {
let weekday = internals::Date::from_yo(year, 1).weekday();
let weekday = internals::Date::from_yo_unchecked(year, 1).weekday();

if (weekday == Thursday) || (weekday == Wednesday && is_leap_year(year)) {
53
Expand Down Expand Up @@ -126,7 +126,7 @@ impl Date {
assert_value_in_range!(month in 1 => 12);
assert_value_in_range!(day in 1 => days_in_year_month(year, month), given year, month);

internals::Date::from_ymd(year, month, day)
internals::Date::from_ymd_unchecked(year, month, day)
}

/// Attempt to create a `Date` from the year, month, and day.
Expand All @@ -148,7 +148,7 @@ impl Date {
ensure_value_in_range!(month in 1 => 12);
ensure_value_in_range!(day in 1 => days_in_year_month(year, month), given year, month);

Ok(internals::Date::from_ymd(year, month, day))
Ok(internals::Date::from_ymd_unchecked(year, month, day))
}

/// Create a `Date` from the year and ordinal day number.
Expand Down Expand Up @@ -222,7 +222,7 @@ impl Date {
#[cfg_attr(doc, doc(cfg(feature = "panicking-api")))]
pub fn from_iso_ywd(year: i32, week: u8, weekday: Weekday) -> Self {
assert_value_in_range!(week in 1 => weeks_in_year(year), given year);
internals::Date::from_iso_ywd(year, week, weekday)
internals::Date::from_iso_ywd_unchecked(year, week, weekday)
}

/// Attempt to create a `Date` from the ISO year, week, and weekday.
Expand All @@ -247,7 +247,7 @@ impl Date {
weekday: Weekday,
) -> Result<Self, ComponentRangeError> {
ensure_value_in_range!(week in 1 => weeks_in_year(year), given year);
Ok(internals::Date::from_iso_ywd(year, week, weekday))
Ok(internals::Date::from_iso_ywd_unchecked(year, week, weekday))
}

/// Create a `Date` representing the current date.
Expand Down Expand Up @@ -687,7 +687,7 @@ impl Date {

// TODO Seek out a formal proof that this always results in a valid value.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
internals::Date::from_ymd(year as i32, month as u8, day as u8)
internals::Date::from_ymd_unchecked(year as i32, month as u8, day as u8)
}
}

Expand Down Expand Up @@ -977,7 +977,7 @@ impl Date {
/// Monday-based week numbering.
#[inline(always)]
fn adjustment(year: i32) -> i16 {
match internals::Date::from_yo(year, 1).weekday() {
match internals::Date::from_yo_unchecked(year, 1).weekday() {
Monday => 7,
Tuesday => 1,
Wednesday => 2,
Expand All @@ -990,14 +990,14 @@ impl Date {

// Verification for all components is done at parse time.
match items {
items!(year, month, day) => Ok(internals::Date::from_ymd(year, month.get(), day.get())),
items!(year, ordinal_day) => Ok(internals::Date::from_yo(year, ordinal_day.get())),
items!(week_based_year, iso_week, weekday) => Ok(internals::Date::from_iso_ywd(
items!(year, month, day) => Ok(internals::Date::from_ymd_unchecked(year, month.get(), day.get())),
items!(year, ordinal_day) => Ok(internals::Date::from_yo_unchecked(year, ordinal_day.get())),
items!(week_based_year, iso_week, weekday) => Ok(internals::Date::from_iso_ywd_unchecked(
week_based_year,
iso_week.get(),
weekday,
)),
items!(year, sunday_week, weekday) => Ok(internals::Date::from_yo(
items!(year, sunday_week, weekday) => Ok(internals::Date::from_yo_unchecked(
year,
#[allow(clippy::cast_sign_loss)]
{
Expand All @@ -1006,7 +1006,7 @@ impl Date {
+ 1) as u16
},
)),
items!(year, monday_week, weekday) => Ok(internals::Date::from_yo(
items!(year, monday_week, weekday) => Ok(internals::Date::from_yo_unchecked(
year,
#[allow(clippy::cast_sign_loss)]
{
Expand Down
14 changes: 7 additions & 7 deletions src/internals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ pub struct Date;
impl Date {
// macros
#[inline(always)]
pub const fn from_yo(year: i32, ordinal: u16) -> crate::Date {
pub const fn from_yo_unchecked(year: i32, ordinal: u16) -> crate::Date {
crate::Date { year, ordinal }
}

// reduce duplication
#[inline]
pub(crate) const fn from_ymd(year: i32, month: u8, day: u8) -> crate::Date {
pub(crate) const fn from_ymd_unchecked(year: i32, month: u8, day: u8) -> crate::Date {
/// Cumulative days through the beginning of a month in both common and
/// leap years.
const DAYS_CUMULATIVE_COMMON_LEAP: [[u16; 12]; 2] = [
Expand All @@ -62,19 +62,19 @@ impl Date {

// reduce duplication
#[inline]
pub(crate) fn from_iso_ywd(year: i32, week: u8, weekday: Weekday) -> crate::Date {
pub(crate) fn from_iso_ywd_unchecked(year: i32, week: u8, weekday: Weekday) -> crate::Date {
let ordinal = week as u16 * 7 + weekday.iso_weekday_number() as u16
- (Self::from_yo(year, 4).weekday().iso_weekday_number() as u16 + 3);
- (Self::from_yo_unchecked(year, 4).weekday().iso_weekday_number() as u16 + 3);

if ordinal < 1 {
return Self::from_yo(year - 1, ordinal + days_in_year(year - 1));
return Self::from_yo_unchecked(year - 1, ordinal + days_in_year(year - 1));
}

let days_in_cur_year = days_in_year(year);
if ordinal > days_in_cur_year {
Self::from_yo(year + 1, ordinal - days_in_cur_year)
Self::from_yo_unchecked(year + 1, ordinal - days_in_cur_year)
} else {
Self::from_yo(year, ordinal)
Self::from_yo_unchecked(year, ordinal)
}
}
}

0 comments on commit 7528763

Please sign in to comment.