@@ -675,12 +675,18 @@ impl ToAsciiChar for AsciiChar {
675
675
impl ToAsciiChar for u8 {
676
676
#[ inline]
677
677
fn to_ascii_char ( self ) -> Result < AsciiChar , ToAsciiCharError > {
678
- unsafe {
679
- if self <= 0x7F {
680
- return Ok ( self . to_ascii_char_unchecked ( ) ) ;
681
- }
682
- }
683
- Err ( ToAsciiCharError ( ( ) ) )
678
+ ( self as u32 ) . to_ascii_char ( )
679
+ }
680
+ #[ inline]
681
+ unsafe fn to_ascii_char_unchecked ( self ) -> AsciiChar {
682
+ mem:: transmute ( self )
683
+ }
684
+ }
685
+
686
+ impl ToAsciiChar for i8 {
687
+ #[ inline]
688
+ fn to_ascii_char ( self ) -> Result < AsciiChar , ToAsciiCharError > {
689
+ ( self as u32 ) . to_ascii_char ( )
684
690
}
685
691
#[ inline]
686
692
unsafe fn to_ascii_char_unchecked ( self ) -> AsciiChar {
@@ -690,13 +696,33 @@ impl ToAsciiChar for u8 {
690
696
691
697
impl ToAsciiChar for char {
692
698
#[ inline]
699
+ fn to_ascii_char ( self ) -> Result < AsciiChar , ToAsciiCharError > {
700
+ ( self as u32 ) . to_ascii_char ( )
701
+ }
702
+ #[ inline]
703
+ unsafe fn to_ascii_char_unchecked ( self ) -> AsciiChar {
704
+ ( self as u32 ) . to_ascii_char_unchecked ( )
705
+ }
706
+ }
707
+
708
+ impl ToAsciiChar for u32 {
693
709
fn to_ascii_char ( self ) -> Result < AsciiChar , ToAsciiCharError > {
694
710
unsafe {
695
- if self as u32 <= 0x7F {
696
- return Ok ( self . to_ascii_char_unchecked ( ) ) ;
711
+ match self {
712
+ 0 ..=127 => Ok ( self . to_ascii_char_unchecked ( ) ) ,
713
+ _ => Err ( ToAsciiCharError ( ( ) ) )
697
714
}
698
715
}
699
- Err ( ToAsciiCharError ( ( ) ) )
716
+ }
717
+ #[ inline]
718
+ unsafe fn to_ascii_char_unchecked ( self ) -> AsciiChar {
719
+ ( self as u8 ) . to_ascii_char_unchecked ( )
720
+ }
721
+ }
722
+
723
+ impl ToAsciiChar for u16 {
724
+ fn to_ascii_char ( self ) -> Result < AsciiChar , ToAsciiCharError > {
725
+ ( self as u32 ) . to_ascii_char ( )
700
726
}
701
727
#[ inline]
702
728
unsafe fn to_ascii_char_unchecked ( self ) -> AsciiChar {
@@ -758,7 +784,7 @@ mod tests {
758
784
assert_eq ! ( generic( A ) , Ok ( A ) ) ;
759
785
assert_eq ! ( generic( b'A' ) , Ok ( A ) ) ;
760
786
assert_eq ! ( generic( 'A' ) , Ok ( A ) ) ;
761
- assert ! ( generic( 200 ) . is_err( ) ) ;
787
+ assert ! ( generic( 200u16 ) . is_err( ) ) ;
762
788
assert ! ( generic( 'λ' ) . is_err( ) ) ;
763
789
}
764
790
0 commit comments