Skip to content

Commit 55f8305

Browse files
burtonageoThomas Bahn
authored and
Thomas Bahn
committed
Remove more unnecessary unsafe statements
1 parent 46116e5 commit 55f8305

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

src/ascii_str.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// which would call the inherent methods if AsciiExt wasn't in scope.
55
#![cfg_attr(feature = "std", allow(deprecated))]
66

7-
use core::{fmt, mem};
7+
use core::fmt;
88
use core::ops::{Index, IndexMut, Range, RangeTo, RangeFrom, RangeFull};
99
use core::slice::{Iter, IterMut};
1010
#[cfg(feature = "std")]
@@ -36,15 +36,13 @@ impl AsciiStr {
3636
/// Converts `&self` to a `&str` slice.
3737
#[inline]
3838
pub fn as_str(&self) -> &str {
39-
let ptr = self as *const AsciiStr as *const str;
40-
unsafe { &*ptr }
39+
From::from(self)
4140
}
4241

4342
/// Converts `&self` into a byte slice.
4443
#[inline]
4544
pub fn as_bytes(&self) -> &[u8] {
46-
let ptr = self as *const AsciiStr as *const [u8];
47-
unsafe { &*ptr }
45+
From::from(self)
4846
}
4947

5048
/// Returns the entire string as slice of `AsciiChar`s.
@@ -342,7 +340,7 @@ impl AsMut<[AsciiChar]> for AsciiStr {
342340
impl Default for &'static AsciiStr {
343341
#[inline]
344342
fn default() -> &'static AsciiStr {
345-
unsafe { "".as_ascii_str_unchecked() }
343+
From::from(&[] as &[AsciiChar])
346344
}
347345
}
348346
impl<'a> From<&'a [AsciiChar]> for &'a AsciiStr {
@@ -432,16 +430,14 @@ macro_rules! impl_index {
432430

433431
#[inline]
434432
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()
437434
}
438435
}
439436

440437
impl IndexMut<$idx> for AsciiStr {
441438
#[inline]
442439
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()
445441
}
446442
}
447443
}
@@ -457,14 +453,14 @@ impl Index<usize> for AsciiStr {
457453

458454
#[inline]
459455
fn index(&self, index: usize) -> &AsciiChar {
460-
unsafe { mem::transmute(&self.slice[index]) }
456+
&self.slice[index]
461457
}
462458
}
463459

464460
impl IndexMut<usize> for AsciiStr {
465461
#[inline]
466462
fn index_mut(&mut self, index: usize) -> &mut AsciiChar {
467-
unsafe { mem::transmute(&mut self.slice[index]) }
463+
&mut self.slice[index]
468464
}
469465
}
470466

0 commit comments

Comments
 (0)