Skip to content

Commit d848104

Browse files
committed
Constify methods that previously "dereferenced" raw pointers
* AsciiStr::as_str() * AsciiStr::as_bytes() Dereferencing raw pointers in `const fn` requires Rust 1.58, while transmute only requires Rust 1.56.
1 parent 18d5b4d commit d848104

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/ascii_str.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,17 @@ impl AsciiStr {
4848
/// Converts `&self` to a `&str` slice.
4949
#[inline]
5050
#[must_use]
51-
pub fn as_str(&self) -> &str {
51+
pub const fn as_str(&self) -> &str {
5252
// SAFETY: All variants of `AsciiChar` are valid bytes for a `str`.
53-
unsafe { &*(self as *const AsciiStr as *const str) }
53+
unsafe { mem::transmute(self) }
5454
}
5555

5656
/// Converts `&self` into a byte slice.
5757
#[inline]
5858
#[must_use]
59-
pub fn as_bytes(&self) -> &[u8] {
59+
pub const fn as_bytes(&self) -> &[u8] {
6060
// SAFETY: All variants of `AsciiChar` are valid `u8`, given they're `repr(u8)`.
61-
unsafe { &*(self as *const AsciiStr as *const [u8]) }
61+
unsafe { mem::transmute(self) }
6262
}
6363

6464
/// Returns the entire string as slice of `AsciiChar`s.

0 commit comments

Comments
 (0)