xrpl_hooks/api/control.rs
1use crate::_c;
2
3/// Guard function
4///
5/// Each time a loop appears in your code a call to this must be
6/// the first branch instruction in wasm binary after the beginning of the loop.
7/// In order to achieve this in Rust use the `while` loop with expression block.
8///
9/// # Example
10///
11/// ```no_run
12/// let mut i = 0;
13/// while {
14/// _g(UNIQUE_GUARD_ID, MAXITER + 1);
15/// i < MAXITER
16/// } {
17/// // your code
18/// i += 1;
19/// }
20/// ```
21#[cfg(not(doctest))]
22#[inline(always)]
23pub fn _g(id: u32, maxiter: u32) {
24 unsafe {
25 _c::_g(id, maxiter);
26 }
27}
28
29/// Accept the originating transaction and commit any changes the hook made
30#[inline(always)]
31pub fn accept(msg: &[u8], error_code: i64) -> ! {
32 unsafe {
33 _c::accept(msg.as_ptr() as u32, msg.len() as u32, error_code);
34 core::hint::unreachable_unchecked()
35 }
36}
37
38/// Reject the originating transaction and discard any changes the hook made
39#[inline(always)]
40pub fn rollback(msg: &[u8], error_code: i64) -> ! {
41 unsafe {
42 _c::rollback(msg.as_ptr() as u32, msg.len() as u32, error_code);
43 core::hint::unreachable_unchecked()
44 }
45}