Skip to content

Commit 46d4f99

Browse files
author
sam skeoch
committed
Add as_ascii_unchecked() method to primitive type char
1 parent 15469f8 commit 46d4f99

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

library/core/src/char/methods.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,20 @@ impl char {
12021202
}
12031203
}
12041204

1205+
/// Converts this char into an [ASCII character](`ascii::Char`), without
1206+
/// checking whether it is valid.
1207+
///
1208+
/// # Safety
1209+
///
1210+
/// This char must be within the ASCII range, or else this is UB.
1211+
#[must_use]
1212+
#[unstable(feature = "ascii_char", issue = "110998")]
1213+
#[inline]
1214+
pub const unsafe fn as_ascii_unchecked(&self) -> ascii::Char {
1215+
// SAFETY: the caller promised that this char is ASCII.
1216+
unsafe { ascii::Char::from_u8_unchecked(*self as u8) }
1217+
}
1218+
12051219
/// Makes a copy of the value in its ASCII upper case equivalent.
12061220
///
12071221
/// ASCII letters 'a' to 'z' are mapped to 'A' to 'Z',

0 commit comments

Comments
 (0)