4
4
// which would call the inherent methods if AsciiExt wasn't in scope.
5
5
#![ cfg_attr( feature = "std" , allow( deprecated) ) ]
6
6
7
- use core:: { fmt, mem } ;
7
+ use core:: fmt;
8
8
use core:: ops:: { Index , IndexMut , Range , RangeTo , RangeFrom , RangeFull } ;
9
9
use core:: slice:: { Iter , IterMut } ;
10
10
#[ cfg( feature = "std" ) ]
@@ -36,15 +36,13 @@ impl AsciiStr {
36
36
/// Converts `&self` to a `&str` slice.
37
37
#[ inline]
38
38
pub fn as_str ( & self ) -> & str {
39
- let ptr = self as * const AsciiStr as * const str ;
40
- unsafe { & * ptr }
39
+ From :: from ( self )
41
40
}
42
41
43
42
/// Converts `&self` into a byte slice.
44
43
#[ inline]
45
44
pub fn as_bytes ( & self ) -> & [ u8 ] {
46
- let ptr = self as * const AsciiStr as * const [ u8 ] ;
47
- unsafe { & * ptr }
45
+ From :: from ( self )
48
46
}
49
47
50
48
/// Returns the entire string as slice of `AsciiChar`s.
@@ -342,7 +340,7 @@ impl AsMut<[AsciiChar]> for AsciiStr {
342
340
impl Default for & ' static AsciiStr {
343
341
#[ inline]
344
342
fn default ( ) -> & ' static AsciiStr {
345
- unsafe { "" . as_ascii_str_unchecked ( ) }
343
+ From :: from ( & [ ] as & [ AsciiChar ] )
346
344
}
347
345
}
348
346
impl < ' a > From < & ' a [ AsciiChar ] > for & ' a AsciiStr {
@@ -432,16 +430,14 @@ macro_rules! impl_index {
432
430
433
431
#[ inline]
434
432
fn index( & self , index: $idx) -> & AsciiStr {
435
- let ptr = & self . slice[ index] as * const [ AsciiChar ] as * const AsciiStr ;
436
- unsafe { & * ptr }
433
+ self . slice[ index] . as_ref( )
437
434
}
438
435
}
439
436
440
437
impl IndexMut <$idx> for AsciiStr {
441
438
#[ inline]
442
439
fn index_mut( & mut self , index: $idx) -> & mut AsciiStr {
443
- let ptr = & mut self . slice[ index] as * mut [ AsciiChar ] as * mut AsciiStr ;
444
- unsafe { & mut * ptr }
440
+ self . slice[ index] . as_mut( )
445
441
}
446
442
}
447
443
}
@@ -457,14 +453,14 @@ impl Index<usize> for AsciiStr {
457
453
458
454
#[ inline]
459
455
fn index ( & self , index : usize ) -> & AsciiChar {
460
- unsafe { mem :: transmute ( & self . slice [ index] ) }
456
+ & self . slice [ index]
461
457
}
462
458
}
463
459
464
460
impl IndexMut < usize > for AsciiStr {
465
461
#[ inline]
466
462
fn index_mut ( & mut self , index : usize ) -> & mut AsciiChar {
467
- unsafe { mem :: transmute ( & mut self . slice [ index] ) }
463
+ & mut self . slice [ index]
468
464
}
469
465
}
470
466
0 commit comments