@@ -576,6 +576,43 @@ pub trait AsMutAsciiStr {
576
576
fn as_mut_ascii_str ( & mut self ) -> Result < & mut AsciiStr , AsAsciiStrError > ;
577
577
}
578
578
579
+ // These generic implementations mirror the generic implementations for AsRef<T> in core.
580
+ impl < ' a , T > AsAsciiStr for & ' a T where T : AsAsciiStr + ?Sized {
581
+ #[ inline]
582
+ fn as_ascii_str ( & self ) -> Result < & AsciiStr , AsAsciiStrError > {
583
+ <T as AsAsciiStr >:: as_ascii_str ( * self )
584
+ }
585
+
586
+ #[ inline]
587
+ unsafe fn as_ascii_str_unchecked ( & self ) -> & AsciiStr {
588
+ <T as AsAsciiStr >:: as_ascii_str_unchecked ( * self )
589
+ }
590
+ }
591
+
592
+ impl < ' a , T > AsAsciiStr for & ' a mut T where T : AsAsciiStr + ?Sized {
593
+ #[ inline]
594
+ fn as_ascii_str ( & self ) -> Result < & AsciiStr , AsAsciiStrError > {
595
+ <T as AsAsciiStr >:: as_ascii_str ( * self )
596
+ }
597
+
598
+ #[ inline]
599
+ unsafe fn as_ascii_str_unchecked ( & self ) -> & AsciiStr {
600
+ <T as AsAsciiStr >:: as_ascii_str_unchecked ( * self )
601
+ }
602
+ }
603
+
604
+ impl < ' a , T > AsMutAsciiStr for & ' a mut T where T : AsMutAsciiStr + ?Sized {
605
+ #[ inline]
606
+ fn as_mut_ascii_str ( & mut self ) -> Result < & mut AsciiStr , AsAsciiStrError > {
607
+ <T as AsMutAsciiStr >:: as_mut_ascii_str ( * self )
608
+ }
609
+
610
+ #[ inline]
611
+ unsafe fn as_mut_ascii_str_unchecked ( & mut self ) -> & mut AsciiStr {
612
+ <T as AsMutAsciiStr >:: as_mut_ascii_str_unchecked ( * self )
613
+ }
614
+ }
615
+
579
616
impl AsAsciiStr for AsciiStr {
580
617
#[ inline]
581
618
fn as_ascii_str ( & self ) -> Result < & AsciiStr , AsAsciiStrError > {
@@ -686,6 +723,26 @@ mod tests {
686
723
assert_eq ! ( generic( "A" ) , Ok ( ascii_str) ) ;
687
724
assert_eq ! ( generic( & b"A" [ ..] ) , Ok ( ascii_str) ) ;
688
725
assert_eq ! ( generic( ascii_str) , Ok ( ascii_str) ) ;
726
+ assert_eq ! ( generic( & "A" ) , Ok ( ascii_str) ) ;
727
+ assert_eq ! ( generic( & ascii_str) , Ok ( ascii_str) ) ;
728
+ assert_eq ! ( generic( & mut "A" ) , Ok ( ascii_str) ) ;
729
+ }
730
+
731
+ #[ test]
732
+ fn generic_as_mut_ascii_str ( ) {
733
+ fn generic_mut < C : AsMutAsciiStr + ?Sized > (
734
+ c : & mut C ,
735
+ ) -> Result < & mut AsciiStr , AsAsciiStrError > {
736
+ c. as_mut_ascii_str ( )
737
+ }
738
+
739
+ let mut arr_mut = [ AsciiChar :: B ] ;
740
+ let mut ascii_str_mut: & mut AsciiStr = arr_mut. as_mut ( ) . into ( ) ;
741
+ // Need a second reference to prevent overlapping mutable borrows
742
+ let mut arr_mut_2 = [ AsciiChar :: B ] ;
743
+ let mut ascii_str_mut_2: & mut AsciiStr = arr_mut_2. as_mut ( ) . into ( ) ;
744
+ assert_eq ! ( generic_mut( & mut ascii_str_mut) , Ok ( & mut * ascii_str_mut_2) ) ;
745
+ assert_eq ! ( generic_mut( ascii_str_mut) , Ok ( & mut * ascii_str_mut_2) ) ;
689
746
}
690
747
691
748
#[ test]
0 commit comments