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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/// Aborts processing of this action and unwinds all pending changes if the test
/// condition is true
#[inline]
pub fn check(pred: bool, msg: &str) {
    if !pred {
        let msg_ptr = msg.as_ptr();
        #[allow(clippy::cast_possible_truncation)]
        let msg_len = msg.len() as u32;
        unsafe { eosio_cdt_sys::eosio_assert_message(0, msg_ptr, msg_len) }
    }
}

/// Aborts processing of this action and unwinds all pending changes if the test
/// condition is true
#[inline]
pub fn check_code<C>(pred: bool, code: C)
where
    C: Into<u64>,
{
    if !pred {
        unsafe { eosio_cdt_sys::eosio_assert_code(0, code.into()) }
    }
}

pub trait Check {
    type Output;
    fn check(self, msg: &str) -> Self::Output;
}

impl<T, E> Check for Result<T, E> {
    type Output = T;

    #[inline]
    fn check(self, msg: &str) -> Self::Output {
        if let Ok(t) = self {
            t
        } else {
            check(false, msg);
            unreachable!();
        }
    }
}

impl<T> Check for Option<T> {
    type Output = T;

    #[inline]
    fn check(self, msg: &str) -> Self::Output {
        if let Some(t) = self {
            t
        } else {
            check(false, msg);
            unreachable!();
        }
    }
}

impl Check for bool {
    type Output = Self;

    #[inline]
    fn check(self, msg: &str) -> Self::Output {
        check(self, msg);
        self
    }
}