Created
January 31, 2018 15:42
-
-
Save masonforest/3276f962b72b7529414a1fb9819681a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#![feature(proc_macro)] | |
extern crate mock_derive; | |
#[allow(dead_code)] | |
#[mock] | |
extern "C" { | |
pub fn _read(x: Vec<u8>) -> Vec<u8>; | |
} | |
use mock_derive::mock; | |
extern crate rmp; | |
extern crate rmp_serialize; | |
extern crate rustc_serialize; | |
use rustc_serialize::{Encodable, Decodable}; | |
use rmp_serialize::{Encoder, Decoder}; | |
macro_rules! function { | |
() => (); | |
($func_name:ident($buffer:ident: &[u8]) -> $t:ident $body:block) => ( | |
pub fn $func_name($buffer: &[u8]) -> [u8; 3]{ | |
unsafe { | |
let run = || $body; | |
let result = run(); | |
let mut buf2 = [0u8; 3]; | |
result.encode(&mut Encoder::new(&mut &mut buf2[..])); | |
buf2 | |
} | |
} | |
); | |
($func_name:ident($buffer:ident: &[u8], $v1:ident: address) -> $t:ident $body:block) => ( | |
function!($func_name($buffer: &[u8]) -> $t { | |
let mut decoder = Decoder::new(&$buffer[..]); | |
let $v1: [u8; 3] = Decodable::decode(&mut decoder).unwrap(); | |
$body | |
}); | |
); | |
($func_name:ident($buffer:ident: &[u8], $($v1:ident: $t1:ident, $v2:ident: address),+) -> $t:ident $body:block) => ( | |
function!($func_name($buffer: &[u8], $($v1: $t1)*) -> $t { | |
let mut decoder = Decoder::new(&$buffer[..]); | |
let $($v2)*: [u8; 3] = Decodable::decode(&mut decoder).unwrap(); | |
$body | |
}); | |
); | |
($func_name:ident($($v1:ident: $t1:ident, $v2:ident: $t2:ident),+) -> $t:ident $body:block) => ( | |
function!($func_name(buf: &[u8], $($v1: $t1, $v2: $t2)*) -> $t { | |
$body | |
}); | |
); | |
} | |
fn read(key: [u8; 3]) -> u32 { | |
unsafe { | |
_read(key.to_vec()); | |
} | |
} | |
// Below here is the acutal contract | |
function![balance_of(address: address) -> u32 { | |
read(address) | |
}]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment