tuwunel_core/utils/result/expect_unchecked.rs
1use std::fmt::Debug;
2
3use super::Result;
4
5/// Extracts successful values through an unchecked result assertion.
6///
7/// Debug builds retain an assertion for the error branch. Release builds rely
8/// entirely on the caller's safety guarantee.
9pub trait ExpectUnchecked<T> {
10 /// Returns the contained `Ok` value without a release-mode branch check.
11 ///
12 /// Debug builds use `msg` when asserting that the result is successful.
13 /// Release builds treat an `Err` value as unreachable.
14 ///
15 /// # Panics
16 ///
17 /// Panics in debug builds when the result is `Err`.
18 ///
19 /// # Safety
20 ///
21 /// The caller must guarantee the result is not `Err`; violating this in
22 /// release builds causes undefined behavior.
23 unsafe fn expect_unchecked(self, msg: &str) -> T;
24}
25
26impl<T, E> ExpectUnchecked<T> for Result<T, E>
27where
28 E: Debug,
29{
30 #[inline]
31 unsafe fn expect_unchecked(self, msg: &str) -> T {
32 if cfg!(debug_assertions) {
33 self.expect(msg)
34 } else {
35 // SAFETY: The caller guarantees the Result is not Err.
36 unsafe { self.unwrap_unchecked() }
37 }
38 }
39}