always panic for invalid integer logarithm

This commit is contained in:
Lukas Markeffsky 2022-10-02 14:24:56 +02:00
parent c2590e6e89
commit 69cafc0699
2 changed files with 15 additions and 73 deletions

View File

@ -2279,9 +2279,8 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is negative, zero, or if the base is not at least 2; it /// This function will panic if `self` is less than or equal to zero,
/// panics in debug mode and the return value is 0 in release /// or if `base` is less then 2.
/// mode.
/// ///
/// # Examples /// # Examples
/// ///
@ -2297,24 +2296,15 @@ pub const fn checked_next_multiple_of(self, rhs: Self) -> Option<Self> {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog(self, base: Self) -> u32 { pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) { assert!(base >= 2, "base of integer logarithm must be at least 2");
Some(n) => n, self.checked_ilog(base).expect("argument of integer logarithm must be positive")
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the base 2 logarithm of the number, rounded down. /// Returns the base 2 logarithm of the number, rounded down.
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is negative or zero it panics in debug mode and the return value /// This function will panic if `self` is less than or equal to zero.
/// is 0 in release mode.
/// ///
/// # Examples /// # Examples
/// ///
@ -2330,24 +2320,14 @@ pub const fn ilog(self, base: Self) -> u32 {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog2(self) -> u32 { pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() { self.checked_ilog2().expect("argument of integer logarithm must be positive")
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the base 10 logarithm of the number, rounded down. /// Returns the base 10 logarithm of the number, rounded down.
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is negative or zero it panics in debug mode and the return value /// This function will panic if `self` is less than or equal to zero.
/// is 0 in release mode.
/// ///
/// # Example /// # Example
/// ///
@ -2363,16 +2343,7 @@ pub const fn ilog2(self) -> u32 {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog10(self) -> u32 { pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() { self.checked_ilog10().expect("argument of integer logarithm must be positive")
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the logarithm of the number with respect to an arbitrary base, /// Returns the logarithm of the number with respect to an arbitrary base,

View File

@ -692,8 +692,7 @@ pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is zero, or if the base is not at least 2; /// This function will panic if `self` is zero, or if `base` is less then 2.
/// it panics in debug mode and the return value is 0 in release mode.
/// ///
/// # Examples /// # Examples
/// ///
@ -709,24 +708,15 @@ pub const fn checked_rem_euclid(self, rhs: Self) -> Option<Self> {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog(self, base: Self) -> u32 { pub const fn ilog(self, base: Self) -> u32 {
match self.checked_ilog(base) { assert!(base >= 2, "base of integer logarithm must be at least 2");
Some(n) => n, self.checked_ilog(base).expect("argument of integer logarithm must be positive")
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the base 2 logarithm of the number, rounded down. /// Returns the base 2 logarithm of the number, rounded down.
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is zero it panics in debug mode and /// This function will panic if `self` is zero.
/// the return value is 0 in release mode.
/// ///
/// # Examples /// # Examples
/// ///
@ -742,24 +732,14 @@ pub const fn ilog(self, base: Self) -> u32 {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog2(self) -> u32 { pub const fn ilog2(self) -> u32 {
match self.checked_ilog2() { self.checked_ilog2().expect("argument of integer logarithm must be positive")
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the base 10 logarithm of the number, rounded down. /// Returns the base 10 logarithm of the number, rounded down.
/// ///
/// # Panics /// # Panics
/// ///
/// When the number is zero it panics in debug mode and the /// This function will panic if `self` is zero.
/// return value is 0 in release mode.
/// ///
/// # Example /// # Example
/// ///
@ -775,16 +755,7 @@ pub const fn ilog2(self) -> u32 {
#[rustc_inherit_overflow_checks] #[rustc_inherit_overflow_checks]
#[allow(arithmetic_overflow)] #[allow(arithmetic_overflow)]
pub const fn ilog10(self) -> u32 { pub const fn ilog10(self) -> u32 {
match self.checked_ilog10() { self.checked_ilog10().expect("argument of integer logarithm must be positive")
Some(n) => n,
None => {
// In debug builds, trigger a panic on None.
// This should optimize completely out in release builds.
let _ = Self::MAX + 1;
0
},
}
} }
/// Returns the logarithm of the number with respect to an arbitrary base, /// Returns the logarithm of the number with respect to an arbitrary base,