1+ use crate :: ascii:: Char as AsciiChar ;
12use crate :: convert:: TryFrom ;
23use crate :: mem;
34use crate :: num:: NonZeroUsize ;
@@ -14,7 +15,7 @@ macro_rules! unsafe_impl_trusted_step {
1415 unsafe impl TrustedStep for $type { }
1516 ) * } ;
1617}
17- unsafe_impl_trusted_step ! [ char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize ] ;
18+ unsafe_impl_trusted_step ! [ AsciiChar char i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize ] ;
1819
1920/// Objects that have a notion of *successor* and *predecessor* operations.
2021///
@@ -484,6 +485,48 @@ impl Step for char {
484485 }
485486}
486487
488+ #[ unstable( feature = "step_trait" , reason = "recently redesigned" , issue = "42168" ) ]
489+ impl Step for AsciiChar {
490+ #[ inline]
491+ fn steps_between ( & start: & AsciiChar , & end: & AsciiChar ) -> Option < usize > {
492+ Step :: steps_between ( & start. to_u8 ( ) , & end. to_u8 ( ) )
493+ }
494+
495+ #[ inline]
496+ fn forward_checked ( start : AsciiChar , count : usize ) -> Option < AsciiChar > {
497+ let end = Step :: forward_checked ( start. to_u8 ( ) , count) ?;
498+ AsciiChar :: from_u8 ( end)
499+ }
500+
501+ #[ inline]
502+ fn backward_checked ( start : AsciiChar , count : usize ) -> Option < AsciiChar > {
503+ let end = Step :: backward_checked ( start. to_u8 ( ) , count) ?;
504+
505+ // SAFETY: Values below that of a valid ASCII character are also valid ASCII
506+ Some ( unsafe { AsciiChar :: from_u8_unchecked ( end) } )
507+ }
508+
509+ #[ inline]
510+ unsafe fn forward_unchecked ( start : AsciiChar , count : usize ) -> AsciiChar {
511+ // SAFETY: Caller asserts that result is a valid ASCII character,
512+ // and therefore it is a valid u8.
513+ let end = unsafe { Step :: forward_unchecked ( start. to_u8 ( ) , count) } ;
514+
515+ // SAFETY: Caller asserts that result is a valid ASCII character.
516+ unsafe { AsciiChar :: from_u8_unchecked ( end) }
517+ }
518+
519+ #[ inline]
520+ unsafe fn backward_unchecked ( start : AsciiChar , count : usize ) -> AsciiChar {
521+ // SAFETY: Caller asserts that result is a valid ASCII character,
522+ // and therefore it is a valid u8.
523+ let end = unsafe { Step :: backward_unchecked ( start. to_u8 ( ) , count) } ;
524+
525+ // SAFETY: Caller asserts that result is a valid ASCII character.
526+ unsafe { AsciiChar :: from_u8_unchecked ( end) }
527+ }
528+ }
529+
487530macro_rules! range_exact_iter_impl {
488531 ( $( $t: ty) * ) => ( $(
489532 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
0 commit comments