
Many tiny changes to stdlib doc comments to make them consistent (for example "Returns foo", rather than "Return foo", per RFC1574), adding missing periods, paragraph breaks, backticks for monospace style, and other minor nits. https://github.com/rust-lang/rfcs/blob/master/text/1574-more-api-documentation-conventions.md#appendix-a-full-conventions-text
47 lines
1.5 KiB
Rust
47 lines
1.5 KiB
Rust
use crate::ascii;
|
|
|
|
#[cfg(not(test))]
|
|
impl<const N: usize> [u8; N] {
|
|
/// Converts this array of bytes into an array of ASCII characters,
|
|
/// or returns `None` if any of the characters is non-ASCII.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// #![feature(ascii_char)]
|
|
/// #![feature(const_option)]
|
|
///
|
|
/// const HEX_DIGITS: [std::ascii::Char; 16] =
|
|
/// *b"0123456789abcdef".as_ascii().unwrap();
|
|
///
|
|
/// assert_eq!(HEX_DIGITS[1].as_str(), "1");
|
|
/// assert_eq!(HEX_DIGITS[10].as_str(), "a");
|
|
/// ```
|
|
#[unstable(feature = "ascii_char", issue = "110998")]
|
|
#[must_use]
|
|
#[inline]
|
|
pub const 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 an 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 }
|
|
}
|
|
}
|