junobuild_shared/ic/
trap.rs

1use ic_cdk::trap;
2use std::fmt::Display;
3
4/// Unwraps a `Result<T, String>` or traps with the error message.
5pub trait UnwrapOrTrap<T> {
6    /// Returns the contained `Ok(T)` value, or traps with the contained error message.
7    ///
8    /// # Panics
9    /// Traps the canister execution with the error string.
10    fn unwrap_or_trap(self) -> T;
11}
12
13impl<T, E: Display> UnwrapOrTrap<T> for Result<T, E> {
14    #[inline]
15    fn unwrap_or_trap(self) -> T {
16        self.unwrap_or_else(|e| trap(e.to_string()))
17    }
18}