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

This commit is contained in:
Tristan Guichaoua
2025-11-22 10:15:59 +01:00
committed by GitHub
parent 012c8e1286
commit ead6e2ccf7

View File

@@ -24,19 +24,13 @@ pub struct Day(u8);
impl Day { impl Day {
/// Creates a [`Day`] from the provided value if it's in the valid range, /// Creates a [`Day`] from the provided value if it's in the valid range,
/// returns [`None`] otherwise. /// returns [`None`] otherwise.
pub fn new(day: u8) -> Option<Self> { pub const fn new(day: u8) -> Option<Self> {
if day == 0 || day > 25 { if day == 0 || day > 25 {
return None; return None;
} }
Some(Self(day)) 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`]. /// Converts the [`Day`] into an [`u8`].
pub fn into_inner(self) -> u8 { pub fn into_inner(self) -> u8 {
self.0 self.0
@@ -137,17 +131,12 @@ impl Iterator for AllDays {
/// Creates a [`Day`] value in a const context. /// Creates a [`Day`] value in a const context.
#[macro_export] #[macro_export]
macro_rules! day { macro_rules! day {
($day:expr) => {{ ($day:expr) => {
const _ASSERT: () = assert!( const {
$day != 0 && $day <= 25, $crate::template::Day::new($day)
concat!( .expect("invalid day number, expecting a value between 1 and 25")
"invalid day number `", }
$day, };
"`, expecting a value between 1 and 25"
),
);
$crate::template::Day::__new_unchecked($day)
}};
} }
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */