-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
724 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use crate::ascii; | ||
|
||
#[cfg(not(test))] | ||
impl<const N: usize> [u8; N] { | ||
/// Converts this array of bytes into a array of ASCII characters, | ||
/// or returns `None` if any of the characters is non-ASCII. | ||
#[unstable(feature = "ascii_char", issue = "110998")] | ||
#[must_use] | ||
#[inline] | ||
pub fn as_ascii(&self) -> Option<&[ascii::Char; N]> { | ||
if self.is_ascii() { | ||
// SAFETY: Just checked that it's ASCII | ||
Some(unsafe { self.as_ascii_unchecked() }) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
/// Converts this array of bytes into a array of ASCII characters, | ||
/// without checking whether they're valid. | ||
/// | ||
/// # Safety | ||
/// | ||
/// Every byte in the array must be in `0..=127`, or else this is UB. | ||
#[unstable(feature = "ascii_char", issue = "110998")] | ||
#[must_use] | ||
#[inline] | ||
pub const unsafe fn as_ascii_unchecked(&self) -> &[ascii::Char; N] { | ||
let byte_ptr: *const [u8; N] = self; | ||
let ascii_ptr = byte_ptr as *const [ascii::Char; N]; | ||
// SAFETY: The caller promised all the bytes are ASCII | ||
unsafe { &*ascii_ptr } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.