File tree 12 files changed +704
-1
lines changed
12 files changed +704
-1
lines changed Original file line number Diff line number Diff line change 101
101
#![ feature( array_into_iter_constructors) ]
102
102
#![ feature( array_methods) ]
103
103
#![ feature( array_windows) ]
104
+ #![ feature( ascii_char) ]
104
105
#![ feature( assert_matches) ]
105
106
#![ feature( async_iterator) ]
106
107
#![ feature( coerce_unsized) ]
Original file line number Diff line number Diff line change @@ -2526,6 +2526,15 @@ impl<T: fmt::Display + ?Sized> ToString for T {
2526
2526
}
2527
2527
}
2528
2528
2529
+ #[ cfg( not( no_global_oom_handling) ) ]
2530
+ #[ unstable( feature = "ascii_char" , issue = "110998" ) ]
2531
+ impl ToString for core:: ascii:: Char {
2532
+ #[ inline]
2533
+ fn to_string ( & self ) -> String {
2534
+ self . as_str ( ) . to_owned ( )
2535
+ }
2536
+ }
2537
+
2529
2538
#[ cfg( not( no_global_oom_handling) ) ]
2530
2539
#[ stable( feature = "char_to_string_specialization" , since = "1.46.0" ) ]
2531
2540
impl ToString for char {
Original file line number Diff line number Diff line change
1
+ use crate :: ascii;
2
+
3
+ #[ cfg( not( test) ) ]
4
+ impl < const N : usize > [ u8 ; N ] {
5
+ /// Converts this array of bytes into a array of ASCII characters,
6
+ /// or returns `None` if any of the characters is non-ASCII.
7
+ #[ unstable( feature = "ascii_char" , issue = "110998" ) ]
8
+ #[ must_use]
9
+ #[ inline]
10
+ pub fn as_ascii ( & self ) -> Option < & [ ascii:: Char ; N ] > {
11
+ if self . is_ascii ( ) {
12
+ // SAFETY: Just checked that it's ASCII
13
+ Some ( unsafe { self . as_ascii_unchecked ( ) } )
14
+ } else {
15
+ None
16
+ }
17
+ }
18
+
19
+ /// Converts this array of bytes into a array of ASCII characters,
20
+ /// without checking whether they're valid.
21
+ ///
22
+ /// # Safety
23
+ ///
24
+ /// Every byte in the array must be in `0..=127`, or else this is UB.
25
+ #[ unstable( feature = "ascii_char" , issue = "110998" ) ]
26
+ #[ must_use]
27
+ #[ inline]
28
+ pub const unsafe fn as_ascii_unchecked ( & self ) -> & [ ascii:: Char ; N ] {
29
+ let byte_ptr: * const [ u8 ; N ] = self ;
30
+ let ascii_ptr = byte_ptr as * const [ ascii:: Char ; N ] ;
31
+ // SAFETY: The caller promised all the bytes are ASCII
32
+ unsafe { & * ascii_ptr }
33
+ }
34
+ }
Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ use crate::ops::{
17
17
} ;
18
18
use crate :: slice:: { Iter , IterMut } ;
19
19
20
+ mod ascii;
20
21
mod drain;
21
22
mod equality;
22
23
mod iter;
Original file line number Diff line number Diff line change @@ -14,6 +14,10 @@ use crate::iter::FusedIterator;
14
14
use crate :: ops:: Range ;
15
15
use crate :: str:: from_utf8_unchecked;
16
16
17
+ mod ascii_char;
18
+ #[ unstable( feature = "ascii_char" , issue = "110998" ) ]
19
+ pub use ascii_char:: AsciiChar as Char ;
20
+
17
21
/// An iterator over the escaped version of a byte.
18
22
///
19
23
/// This `struct` is created by the [`escape_default`] function. See its
You can’t perform that action at this time.
0 commit comments