@@ -359,7 +359,7 @@ impl<'a> AsciiCast<'a> for str {
359
359
360
360
361
361
/// Error returned by IntoAsciiStr
362
- #[ derive( PartialEq ) ] // allows assert_eq!
362
+ #[ derive( Clone , Copy ) ]
363
363
pub struct IntoAsciiStrError {
364
364
index : usize ,
365
365
/// If less than 128, it was a byte >= 128
@@ -498,11 +498,11 @@ impl IntoAsciiStr for str {
498
498
}
499
499
impl IntoMutAsciiStr for str {
500
500
fn into_ascii_mut ( & mut self ) -> Result < & mut AsciiStr , IntoAsciiStrError > {
501
- match self . bytes ( ) . enumerate ( ) . find ( | & ( _ , b ) | b > 127 ) {
502
- Some ( ( index, byte ) ) => Err ( IntoAsciiStrError {
503
- index : index,
504
- not_ascii : ( byte - 128 ) as char ,
505
- } ) ,
501
+ match self . bytes ( ) . position ( |b | b > 127 ) {
502
+ Some ( index) => Err ( IntoAsciiStrError {
503
+ index : index,
504
+ not_ascii : self [ index.. ] . chars ( ) . next ( ) . unwrap ( ) ,
505
+ } ) ,
506
506
None => unsafe { Ok ( self . into_ascii_mut_unchecked ( ) ) } ,
507
507
}
508
508
}
@@ -516,15 +516,38 @@ impl IntoMutAsciiStr for str {
516
516
#[ cfg( test) ]
517
517
mod tests {
518
518
use { AsciiCast , Ascii } ;
519
- use super :: { AsciiStr , IntoAsciiStr , IntoAsciiStrError } ;
519
+ use super :: { AsciiStr , IntoAsciiStr , IntoMutAsciiStr , IntoAsciiStrError } ;
520
+
521
+ pub fn tuplify < T > ( r : Result < T , IntoAsciiStrError > ) -> Result < T , ( usize , char ) > {
522
+ r. map_err ( |e| ( e. index , e. not_ascii ) )
523
+ }
520
524
521
525
#[ test]
522
- fn into_ascii ( ) {
526
+ fn generic_into_ascii ( ) {
523
527
fn generic < C : IntoAsciiStr +?Sized > ( c : & C ) -> Result < & AsciiStr , IntoAsciiStrError > {
524
528
c. into_ascii ( )
525
529
}
526
- assert_eq ! ( generic( "A" ) , Ok ( [ Ascii :: A ] . as_ref( ) . into( ) ) ) ;
527
- assert_eq ! ( generic( & b"A" [ ..] ) , Ok ( [ Ascii :: A ] . as_ref( ) . into( ) ) ) ;
530
+ let arr = [ Ascii :: A ] ;
531
+ let ascii_str = arr. as_ref ( ) . into ( ) ;
532
+ assert_eq ! ( tuplify( generic( "A" ) ) , Ok ( ascii_str) ) ;
533
+ assert_eq ! ( tuplify( generic( & b"A" [ ..] ) ) , Ok ( ascii_str) ) ;
534
+ //assert_eq!(generic(ascii_str), Ok(ascii_str));
535
+ }
536
+ #[ test]
537
+ fn into_ascii ( ) {
538
+ let mut s: String = "abčd" . to_string ( ) ;
539
+ let mut b: Vec < u8 > = s. clone ( ) . into ( ) ;
540
+ assert_eq ! ( tuplify( s. as_str( ) . into_ascii( ) ) , Err ( ( 2 , 'č' ) ) ) ;
541
+ assert_eq ! ( tuplify( s. as_mut_str( ) . into_ascii_mut( ) ) , Err ( ( 2 , 'č' ) ) ) ;
542
+ let c = ( b[ 2 ] -128 ) as char ;
543
+ assert_eq ! ( tuplify( b. as_slice( ) . into_ascii( ) ) , Err ( ( 2 , c) ) ) ;
544
+ assert_eq ! ( tuplify( b. as_mut_slice( ) . into_ascii_mut( ) ) , Err ( ( 2 , c) ) ) ;
545
+ let mut a = [ Ascii :: a, Ascii :: b] ;
546
+ assert_eq ! ( tuplify( ( & s[ ..2 ] ) . into_ascii( ) ) , Ok ( ( & a[ ..] ) . into( ) ) ) ;
547
+ assert_eq ! ( tuplify( ( & b[ ..2 ] ) . into_ascii( ) ) , Ok ( ( & a[ ..] ) . into( ) ) ) ;
548
+ let a = Ok ( ( & mut a[ ..] ) . into ( ) ) ;
549
+ assert_eq ! ( tuplify( ( & mut s[ ..2 ] ) . into_ascii_mut( ) ) , a) ;
550
+ assert_eq ! ( tuplify( ( & mut b[ ..2 ] ) . into_ascii_mut( ) ) , a) ;
528
551
}
529
552
530
553
#[ test]
0 commit comments