Skip to content

Commit 3db6799

Browse files
author
sam skeoch
committed
Add assert_unsafe_precondition!()s to as_ascii_unchecked() methods
1 parent d5daab4 commit 3db6799

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

library/core/src/char/methods.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,6 +1212,12 @@ impl char {
12121212
#[unstable(feature = "ascii_char", issue = "110998")]
12131213
#[inline]
12141214
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
1215+
assert_unsafe_precondition!(
1216+
check_library_ub,
1217+
"as_ascii_unchecked requires that the char is valid ASCII",
1218+
(it: &char = self) => it.is_ascii()
1219+
);
1220+
12151221
// SAFETY: the caller promised that this char is ASCII.
12161222
unsafe { ascii::Char::from_u8_unchecked(*self as u8) }
12171223
}

library/core/src/num/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,12 @@ impl u8 {
492492
#[unstable(feature = "ascii_char", issue = "110998")]
493493
#[inline]
494494
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
495+
assert_unsafe_precondition!(
496+
check_library_ub,
497+
"as_ascii_unchecked requires that the byte is valid ASCII",
498+
(it: &u8 = self) => it.is_ascii()
499+
);
500+
495501
// SAFETY: the caller promised that this byte is ASCII.
496502
unsafe { ascii::Char::from_u8_unchecked(*self) }
497503
}

library/core/src/str/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2643,6 +2643,12 @@ impl str {
26432643
#[must_use]
26442644
#[inline]
26452645
pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char] {
2646+
assert_unsafe_precondition!(
2647+
check_library_ub,
2648+
"as_ascii_unchecked requires that the string is valid ASCII",
2649+
(it: &str = self) => it.is_ascii()
2650+
);
2651+
26462652
// SAFETY: the caller promised that every byte of this string slice
26472653
// is ASCII.
26482654
unsafe { self.as_bytes().as_ascii_unchecked() }

0 commit comments

Comments
 (0)