from_u32_unchecked: check validity when debug assertions are enabled

This commit is contained in:
Ralf Jung 2020-05-28 10:31:38 +02:00
parent 4512721156
commit 52ed89ae8c

View file

@ -99,7 +99,7 @@ pub fn from_u32(i: u32) -> Option<char> {
#[inline]
#[stable(feature = "char_from_unchecked", since = "1.5.0")]
pub unsafe fn from_u32_unchecked(i: u32) -> char {
transmute(i)
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { transmute(i) }
}
#[stable(feature = "char_convert", since = "1.13.0")]
@ -218,7 +218,7 @@ impl TryFrom<u32> for char {
Err(CharTryFromError(()))
} else {
// SAFETY: checked that it's a legal unicode value
Ok(unsafe { from_u32_unchecked(i) })
Ok(unsafe { transmute(i) })
}
}
}