Skip to content

Commit a207bf0

Browse files
committed
Use const_unwrap for char::from_u32_unchecked
1 parent 96df494 commit a207bf0

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

library/core/src/char/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(super) const fn from_u32(i: u32) -> Option<char> {
2323
#[must_use]
2424
pub(super) const unsafe fn from_u32_unchecked(i: u32) -> char {
2525
// SAFETY: the caller must guarantee that `i` is a valid char value.
26-
if cfg!(debug_assertions) { char::from_u32(i).unwrap() } else { unsafe { transmute(i) } }
26+
if cfg!(debug_assertions) { char::from_u32(i).const_unwrap() } else { unsafe { transmute(i) } }
2727
}
2828

2929
#[stable(feature = "char_convert", since = "1.13.0")]

library/core/src/option.rs

+12
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,18 @@ impl<T> Option<T> {
18111811
}
18121812
}
18131813
}
1814+
impl<T: Copy> Option<T> {
1815+
/// Unwrap `Copy` types in a const context. Other than the `Copy` bounds,
1816+
/// this is the same as [`unwrap`][Option::unwrap].
1817+
#[inline]
1818+
#[track_caller]
1819+
pub(crate) const fn const_unwrap(self) -> T {
1820+
match self {
1821+
Some(val) => val,
1822+
None => panic("called `Option::unwrap()` on a `None` value"),
1823+
}
1824+
}
1825+
}
18141826

18151827
impl<T, U> Option<(T, U)> {
18161828
/// Unzips an option containing a tuple of two options.

0 commit comments

Comments
 (0)