diff options
| author | Matthias Krüger <matthias.krueger@famsik.de> | 2023-05-04 19:18:21 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-04 19:18:21 +0200 |
| commit | ea0b6504faa95dd4bfb0666cbcb5dfcfe0591313 (patch) | |
| tree | fa4ad464202284442a0b227f262f6a048b006040 /library/core/src/array/ascii.rs | |
| parent | 8d66f01ab50870c63f85c3c7395347e5b82caa88 (diff) | |
| parent | 8c781b0906209e81f3540d1495becddae9894a25 (diff) | |
| download | rust-ea0b6504faa95dd4bfb0666cbcb5dfcfe0591313.tar.gz rust-ea0b6504faa95dd4bfb0666cbcb5dfcfe0591313.zip | |
Rollup merge of #111009 - scottmcm:ascii-char, r=BurntSushi
Add `ascii::Char` (ACP#179) ACP second: https://github.com/rust-lang/libs-team/issues/179#issuecomment-1527900570 New tracking issue: https://github.com/rust-lang/rust/issues/110998 For now this is an `enum` as `@kupiakos` [suggested](https://github.com/rust-lang/libs-team/issues/179#issuecomment-1527959724), 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: https://github.com/scottmcm/rust/commit/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.)
Diffstat (limited to 'library/core/src/array/ascii.rs')
| -rw-r--r-- | library/core/src/array/ascii.rs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/library/core/src/array/ascii.rs b/library/core/src/array/ascii.rs new file mode 100644 index 00000000000..6750d7c0711 --- /dev/null +++ b/library/core/src/array/ascii.rs @@ -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 } + } +} |
