forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#111009 - scottmcm:ascii-char, r=BurntSushi
Add `ascii::Char` (ACP#179) ACP second: rust-lang/libs-team#179 (comment) New tracking issue: rust-lang#110998 For now this is an `enum` as `@kupiakos` [suggested](rust-lang/libs-team#179 (comment)), with the variants under a different feature flag. There's lots more things that could be added here, and place for further doc updates, but this seems like a plausible starting point PR. I've gone through and put an `as_ascii` next to every `is_ascii`: on `u8`, `char`, `[u8]`, and `str`. As a demonstration, made a commit updating some formatting code to use this: scottmcm@ascii-char-in-fmt (I don't want to include that in this PR, though, because that brings in perf questions that don't exist if this is just adding new unstable APIs.)
- 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.