@@ -376,17 +376,13 @@ impl Error for IntoAsciiStrError {
376
376
pub trait IntoAsciiStr : AsciiExt {
377
377
/// Convert to `AsciiStr`, not doing any range asserts.
378
378
unsafe fn into_ascii_unchecked ( & self ) -> & AsciiStr ;
379
- /// Convert the portion from start of self that is valid ASCII to `AsciiStr`.
380
- fn ascii_part ( & self ) -> & AsciiStr ;
381
379
/// Convert to `AsciiStr`.
382
380
fn into_ascii ( & self ) -> Result < & AsciiStr , IntoAsciiStrError > ;
383
381
}
384
382
/// Trait for converting various slices into `AsciiStr`.
385
383
pub trait IntoMutAsciiStr : AsciiExt {
386
384
/// Convert to `AsciiStr`, not doing any range asserts.
387
385
unsafe fn into_ascii_mut_unchecked ( & mut self ) -> & mut AsciiStr ;
388
- /// Convert the portion from start of self that is valid ASCII to `AsciiStr`.
389
- fn ascii_part_mut ( & mut self ) -> & mut AsciiStr ;
390
386
/// Convert to `AsciiStr`.
391
387
fn into_ascii_mut ( & mut self ) -> Result < & mut AsciiStr , IntoAsciiStrError > ;
392
388
}
@@ -396,9 +392,6 @@ impl IntoAsciiStr for AsciiStr {
396
392
fn into_ascii ( & self ) -> Result < & AsciiStr , IntoAsciiStrError > {
397
393
Ok ( self )
398
394
}
399
- fn ascii_part ( & self ) -> & AsciiStr {
400
- self
401
- }
402
395
unsafe fn into_ascii_unchecked ( & self ) -> & AsciiStr {
403
396
self
404
397
}
@@ -408,9 +401,6 @@ impl IntoMutAsciiStr for AsciiStr {
408
401
fn into_ascii_mut ( & mut self ) -> Result < & mut AsciiStr , IntoAsciiStrError > {
409
402
Ok ( self )
410
403
}
411
- fn ascii_part_mut ( & mut self ) -> & mut AsciiStr {
412
- self
413
- }
414
404
unsafe fn into_ascii_mut_unchecked ( & mut self ) -> & mut AsciiStr {
415
405
self
416
406
}
@@ -426,12 +416,6 @@ impl IntoAsciiStr for [u8] {
426
416
None => unsafe { Ok ( self . into_ascii_unchecked ( ) ) } ,
427
417
}
428
418
}
429
- fn ascii_part ( & self ) -> & AsciiStr {
430
- let ascii = self . iter ( ) . position ( |b| * b > 127 )
431
- . map ( |l| & self [ ..l] )
432
- . unwrap_or ( self ) ;
433
- unsafe { ascii. into_ascii_unchecked ( ) }
434
- }
435
419
unsafe fn into_ascii_unchecked ( & self ) -> & AsciiStr {
436
420
AsciiStr :: from_bytes_unchecked ( self )
437
421
}
@@ -446,32 +430,17 @@ impl IntoMutAsciiStr for [u8] {
446
430
None => unsafe { Ok ( self . into_ascii_mut_unchecked ( ) ) } ,
447
431
}
448
432
}
449
- fn ascii_part_mut ( & mut self ) -> & mut AsciiStr {
450
- let ascii = match self . iter_mut ( ) . position ( |b| * b > 127 ) {
451
- Some ( l) => & mut self [ ..l] ,
452
- None => self ,
453
- } ;
454
- unsafe { ascii. into_ascii_mut_unchecked ( ) }
455
- }
456
433
unsafe fn into_ascii_mut_unchecked ( & mut self ) -> & mut AsciiStr {
457
434
mem:: transmute ( self )
458
435
}
459
436
}
460
437
461
438
impl IntoAsciiStr for str {
462
439
fn into_ascii ( & self ) -> Result < & AsciiStr , IntoAsciiStrError > {
463
- let s = self . ascii_part ( ) ;
464
- if s. len ( ) == self . len ( ) {
465
- Ok ( s)
466
- } else {
467
- Err ( IntoAsciiStrError {
468
- index : s. len ( ) ,
469
- not_ascii : self [ s. len ( ) ..] . chars ( ) . next ( ) . unwrap ( ) ,
470
- } )
471
- }
472
- }
473
- fn ascii_part ( & self ) -> & AsciiStr {
474
- self . as_bytes ( ) . ascii_part ( )
440
+ self . as_bytes ( ) . into_ascii ( ) . map_err ( |err| IntoAsciiStrError {
441
+ not_ascii : self [ err. index ..] . chars ( ) . next ( ) . unwrap ( ) ,
442
+ index : err. index ,
443
+ } )
475
444
}
476
445
unsafe fn into_ascii_unchecked ( & self ) -> & AsciiStr {
477
446
mem:: transmute ( self )
@@ -487,12 +456,6 @@ impl IntoMutAsciiStr for str {
487
456
None => unsafe { Ok ( self . into_ascii_mut_unchecked ( ) ) } ,
488
457
}
489
458
}
490
- fn ascii_part_mut ( & mut self ) -> & mut AsciiStr {
491
- unsafe { match self . bytes ( ) . position ( |b| b > 127 ) {
492
- Some ( index) => self . slice_mut_unchecked ( 0 , index) . into_ascii_mut_unchecked ( ) ,
493
- None => self . into_ascii_mut_unchecked ( ) ,
494
- } }
495
- }
496
459
unsafe fn into_ascii_mut_unchecked ( & mut self ) -> & mut AsciiStr {
497
460
mem:: transmute ( self )
498
461
}
0 commit comments