Skip to content

Commit

Permalink
Rollup merge of #118979 - ChrisDenton:unwrap-const, r=Nilstrieb,dtolnay
Browse files Browse the repository at this point in the history
Use `assert_unsafe_precondition` for `char::from_u32_unchecked`

Use `assert_unsafe_precondition` in `char::from_u32_unchecked` so that it can be stabilized as `const`.
  • Loading branch information
matthiaskrgr committed Jan 9, 2024
2 parents 1974f5c + ab716d0 commit b0c492c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion library/core/src/char/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::char::TryFromCharError;
use crate::convert::TryFrom;
use crate::error::Error;
use crate::fmt;
use crate::intrinsics::assert_unsafe_precondition;
use crate::mem::transmute;
use crate::str::FromStr;

Expand All @@ -23,7 +24,13 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
#[must_use]
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
// SAFETY: the caller must guarantee that `i` is a valid char value.
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
unsafe {
assert_unsafe_precondition!(
"invalid value for `char`",
(i: u32) => char_try_from_u32(i).is_ok()
);
transmute(i)
}
}

#[stable(feature = "char_convert", since = "1.13.0")]
Expand Down

0 comments on commit b0c492c

Please sign in to comment.