From ec137dd6ef58d2c84235705196be8d37cd64e5b0 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Fri, 6 Dec 2024 19:38:25 +0100 Subject: [PATCH 1/2] make `Day::new` const and remove `__new_unchecked` --- src/template/day.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/template/day.rs b/src/template/day.rs index 99b8280..a71c354 100644 --- a/src/template/day.rs +++ b/src/template/day.rs @@ -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 { + pub const fn new(day: u8) -> Option { 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 @@ -137,17 +131,15 @@ 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 { + let day: u8 = $day; + match $crate::template::Day::new(day) { + Some(day) => day, + None => panic!("invalid day number, expecting a value between 1 and 25"), + } + } + }; } /* -------------------------------------------------------------------------- */ From 850e90ec94b5567ca0bec01de2e50defa53874a3 Mon Sep 17 00:00:00 2001 From: Tristan Guichaoua Date: Fri, 6 Dec 2024 19:53:07 +0100 Subject: [PATCH 2/2] use `Option::expect` --- src/template/day.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/template/day.rs b/src/template/day.rs index a71c354..e7b7ab0 100644 --- a/src/template/day.rs +++ b/src/template/day.rs @@ -133,11 +133,8 @@ impl Iterator for AllDays { macro_rules! day { ($day:expr) => { const { - let day: u8 = $day; - match $crate::template::Day::new(day) { - Some(day) => day, - None => panic!("invalid day number, expecting a value between 1 and 25"), - } + $crate::template::Day::new($day) + .expect("invalid day number, expecting a value between 1 and 25") } }; }