Use assert_unsafe_precondition! in AsciiChar::digit_unchecked

This commit is contained in:
Chai T. Rex 2024-08-21 15:59:14 -04:00
parent 982c6f8721
commit 191862d701

View File

@ -3,8 +3,8 @@
//! suggestions from rustc if you get anything slightly wrong in here, and overall //! suggestions from rustc if you get anything slightly wrong in here, and overall
//! helps with clarity as we're also referring to `char` intentionally in here. //! helps with clarity as we're also referring to `char` intentionally in here.
use crate::fmt;
use crate::mem::transmute; use crate::mem::transmute;
use crate::{assert_unsafe_precondition, fmt};
/// One of the 128 Unicode characters from U+0000 through U+007F, /// One of the 128 Unicode characters from U+0000 through U+007F,
/// often known as the [ASCII] subset. /// often known as the [ASCII] subset.
@ -497,14 +497,18 @@ pub const fn digit(d: u8) -> Option<Self> {
/// Notably, it should not be expected to return hex digits, or any other /// Notably, it should not be expected to return hex digits, or any other
/// reasonable extension of the decimal digits. /// reasonable extension of the decimal digits.
/// ///
/// (This lose safety condition is intended to simplify soundness proofs /// (This loose safety condition is intended to simplify soundness proofs
/// when writing code using this method, since the implementation doesn't /// when writing code using this method, since the implementation doesn't
/// need something really specific, not to make those other arguments do /// need something really specific, not to make those other arguments do
/// something useful. It might be tightened before stabilization.) /// something useful. It might be tightened before stabilization.)
#[unstable(feature = "ascii_char", issue = "110998")] #[unstable(feature = "ascii_char", issue = "110998")]
#[inline] #[inline]
pub const unsafe fn digit_unchecked(d: u8) -> Self { pub const unsafe fn digit_unchecked(d: u8) -> Self {
debug_assert!(d < 10); assert_unsafe_precondition!(
check_language_ub,
"`AsciiChar::digit_unchecked` input cannot exceed 9.",
(d: u8 = d) => d < 10
);
// SAFETY: `'0'` through `'9'` are U+00030 through U+0039, // SAFETY: `'0'` through `'9'` are U+00030 through U+0039,
// so because `d` must be 64 or less the addition can return at most // so because `d` must be 64 or less the addition can return at most