tuwunel_core/utils/result/unwrap_infallible.rs
1use std::convert::Infallible;
2
3use super::{DebugInspect, Result};
4use crate::error;
5
6/// Extracts the value from a result whose error type is uninhabited.
7///
8/// Only the `Ok` branch can be constructed for `Result<T, Infallible>`. The
9/// implementation uses an unchecked unwrap after a debug assertion.
10pub trait UnwrapInfallible<T> {
11 /// Returns the only constructible value from the result.
12 ///
13 /// The uninhabited error type makes the `Err` branch unreachable. Consuming
14 /// the result requires no fallback value or closure.
15 fn unwrap_infallible(self) -> T;
16}
17
18impl<T> UnwrapInfallible<T> for Result<T, Infallible> {
19 #[inline]
20 fn unwrap_infallible(self) -> T {
21 // SAFETY: Branchless unwrap for errors that can never happen. In debug
22 // mode this is asserted.
23 unsafe {
24 self.debug_inspect_err(error::infallible)
25 .unwrap_unchecked()
26 }
27 }
28}