zink/
asm.rs

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
//! Assembly trait implementation.

use crate::ffi;
use paste::paste;

/// Types implemented this trait are able to be pushed on stack.
pub trait Asm: Copy {
    /// Push self on the stack.
    fn push(self);

    #[cfg(not(target_family = "wasm"))]
    fn bytes32(&self) -> [u8; 32];
}

macro_rules! impl_asm {
    ($ty:ident) => {
        impl Asm for $ty {
            fn push(self) {
                unsafe {
                    paste! { ffi::asm::[<push_ $ty>](self); }
                }
            }

            #[cfg(not(target_family = "wasm"))]
            fn bytes32(&self) -> [u8; 32] {
                crate::to_bytes32(&self.to_le_bytes())
            }
        }
    };
    ($($ty:tt),+) => {
        $(impl_asm!($ty);)+
    };
}

impl_asm!(i8, u8, i16, u16, i32, u32, i64, u64);