diff options
| author | bors <bors@rust-lang.org> | 2023-05-04 17:56:17 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2023-05-04 17:56:17 +0000 |
| commit | eb7a7434215dd9d4b7cc18746ad1d0e531c25325 (patch) | |
| tree | 0287e377a5dbb3e130b3eb2265703697cfd50ff0 /library/core/src/array/ascii.rs | |
| parent | eac35583d2ffb5ed9e564dee0822c9a244058ee0 (diff) | |
| parent | 75e8f87673905d58618773bec92eff6446c0d579 (diff) | |
| download | rust-eb7a7434215dd9d4b7cc18746ad1d0e531c25325.tar.gz rust-eb7a7434215dd9d4b7cc18746ad1d0e531c25325.zip | |
Auto merge of #111210 - matthiaskrgr:rollup-doquh2n, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #108865 (Add a `sysroot` crate to represent the standard library crates) - #110651 (libtest: include test output in junit xml reports) - #110826 (Make PlaceMention a non-mutating use.) - #110982 (Do not recurse into const generic args when resolving self lifetime elision.) - #111009 (Add `ascii::Char` (ACP#179)) - #111100 (check array type of repeat exprs is wf) - #111186 (Add `is_positive` method for signed non-zero integers.) - #111201 (bootstrap: add .gitmodules to the sources) Failed merges: - #110954 (Reject borrows of projections in ConstProp.) r? `@ghost` `@rustbot` modify labels: rollup
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 } + } +} |
