Skip to content

Commit 82f93a2

Browse files
author
sam skeoch
committed
Add as_ascii_unchecked() method to primitive type u8
1 parent 46d4f99 commit 82f93a2

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/core/src/num/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,20 @@ impl u8 {
482482
ascii::Char::from_u8(*self)
483483
}
484484

485+
/// Converts this byte to an [ASCII character](ascii::Char), without
486+
/// checking whether or not it's valid.
487+
///
488+
/// # Safety
489+
///
490+
/// This byte must be valid ASCII, or else this is UB.
491+
#[must_use]
492+
#[unstable(feature = "ascii_char", issue = "110998")]
493+
#[inline]
494+
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
495+
// SAFETY: the caller promised that this byte is ASCII.
496+
unsafe { ascii::Char::from_u8_unchecked(*self) }
497+
}
498+
485499
/// Makes a copy of the value in its ASCII upper case equivalent.
486500
///
487501
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',

0 commit comments

Comments
 (0)