Skip to content
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

Make Day::new const and remove Day::__new_unchecked #73

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 7 additions & 18 deletions src/template/day.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ pub struct Day(u8);
impl Day {
/// Creates a [`Day`] from the provided value if it's in the valid range,
/// returns [`None`] otherwise.
pub fn new(day: u8) -> Option<Self> {
pub const fn new(day: u8) -> Option<Self> {
if day == 0 || day > 25 {
return None;
}
Some(Self(day))
}

// Not part of the public API
#[doc(hidden)]
pub const fn __new_unchecked(day: u8) -> Self {
Self(day)
}

/// Converts the [`Day`] into an [`u8`].
pub fn into_inner(self) -> u8 {
self.0
Expand Down Expand Up @@ -137,17 +131,12 @@ impl Iterator for AllDays {
/// Creates a [`Day`] value in a const context.
#[macro_export]
macro_rules! day {
($day:expr) => {{
const _ASSERT: () = assert!(
$day != 0 && $day <= 25,
concat!(
"invalid day number `",
$day,
"`, expecting a value between 1 and 25"
),
);
$crate::template::Day::__new_unchecked($day)
}};
($day:expr) => {
const {
$crate::template::Day::new($day)
.expect("invalid day number, expecting a value between 1 and 25")
}
};
}

/* -------------------------------------------------------------------------- */
Expand Down