1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//! Helpers for assertions due to prior validation of a WASM program.

use core::fmt::Debug;

pub(crate) trait UnwrapValidatedExt<T> {
    fn unwrap_validated(self) -> T;
}

impl<T> UnwrapValidatedExt<T> for Option<T> {
    /// Indicate that we can assume this Option to be Some(_) due to prior validation
    fn unwrap_validated(self) -> T {
        self.expect("Validation guarantees this to be `Some(_)`, but it is `None`")
    }
}

impl<T, E: Debug> UnwrapValidatedExt<T> for Result<T, E> {
    /// Indicate that we can assume this Result to be Ok(_) due to prior validation
    fn unwrap_validated(self) -> T {
        self.unwrap_or_else(|e| {
            panic!("Validation guarantees this to be `Ok(_)`, but it is `Err({e:?})`");
        })
    }
}

#[macro_export]
macro_rules! unreachable_validated {
    () => {
        unreachable!("because of prior validation")
    };
}