File tree Expand file tree Collapse file tree 12 files changed +724
-1
lines changed Expand file tree Collapse file tree 12 files changed +724
-1
lines changed Original file line number Diff line number Diff line change 101101#![ feature( array_into_iter_constructors) ]
102102#![ feature( array_methods) ]
103103#![ feature( array_windows) ]
104+ #![ feature( ascii_char) ]
104105#![ feature( assert_matches) ]
105106#![ feature( async_iterator) ]
106107#![ 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 {
25262526 }
25272527}
25282528
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+
25292538#[ cfg( not( no_global_oom_handling) ) ]
25302539#[ stable( feature = "char_to_string_specialization" , since = "1.46.0" ) ]
25312540impl 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::{
1717} ;
1818use crate :: slice:: { Iter , IterMut } ;
1919
20+ mod ascii;
2021mod drain;
2122mod equality;
2223mod iter;
Original file line number Diff line number Diff line change @@ -14,6 +14,10 @@ use crate::iter::FusedIterator;
1414use crate :: ops:: Range ;
1515use crate :: str:: from_utf8_unchecked;
1616
17+ mod ascii_char;
18+ #[ unstable( feature = "ascii_char" , issue = "110998" ) ]
19+ pub use ascii_char:: AsciiChar as Char ;
20+
1721/// An iterator over the escaped version of a byte.
1822///
1923/// This `struct` is created by the [`escape_default`] function. See its
You can’t perform that action at this time.
0 commit comments