File tree 3 files changed +18
-0
lines changed
3 files changed +18
-0
lines changed Original file line number Diff line number Diff line change @@ -1212,6 +1212,12 @@ impl char {
1212
1212
#[ unstable( feature = "ascii_char" , issue = "110998" ) ]
1213
1213
#[ inline]
1214
1214
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
+
1215
1221
// SAFETY: the caller promised that this char is ASCII.
1216
1222
unsafe { ascii:: Char :: from_u8_unchecked ( * self as u8 ) }
1217
1223
}
Original file line number Diff line number Diff line change @@ -492,6 +492,12 @@ impl u8 {
492
492
#[ unstable( feature = "ascii_char" , issue = "110998" ) ]
493
493
#[ inline]
494
494
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
+
495
501
// SAFETY: the caller promised that this byte is ASCII.
496
502
unsafe { ascii:: Char :: from_u8_unchecked ( * self ) }
497
503
}
Original file line number Diff line number Diff line change @@ -2643,6 +2643,12 @@ impl str {
2643
2643
#[ must_use]
2644
2644
#[ inline]
2645
2645
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
+
2646
2652
// SAFETY: the caller promised that every byte of this string slice
2647
2653
// is ASCII.
2648
2654
unsafe { self . as_bytes ( ) . as_ascii_unchecked ( ) }
You can’t perform that action at this time.
0 commit comments