File tree Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Expand file tree Collapse file tree 3 files changed +18
-0
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff 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 ( ) }
You can’t perform that action at this time.
0 commit comments