Skip to content

Commit

Permalink
Turn EwasmAPI into a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
axic committed May 28, 2019
1 parent 2fff902 commit d21dcb6
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,3 +604,67 @@ pub fn selfdestruct(address: &Address) -> ! {
native::ethereum_selfDestruct(address.bytes.as_ptr() as *const u32);
}
}

pub trait EwasmAPI: Send + Sync {
fn consume_gas(&self, amount: u64);
fn gas_left(&self) -> u64;
fn current_address(&self) -> [u8; 20];
}

#[derive(Debug)]
pub struct NativeImpl;

#[derive(Debug)]
pub struct TestImpl {
gas: u64,
}

impl Default for TestImpl {
fn default() -> TestImpl {
TestImpl { gas: 0 }
}
}

/*
trait TestSetter {
fn set_gas(mut self, amount: u64);
}
impl TestSetter for TestImpl {
fn set_gas(mut self, amount: u64) {
self.gas = amount
}
}
*/

impl EwasmAPI for NativeImpl {
fn consume_gas(&self, amount: u64) {}
fn gas_left(&self) -> u64 {
gas_left()
}
fn current_address(&self) -> [u8; 20] {
[0u8; 20]
}
}

impl EwasmAPI for TestImpl {
fn consume_gas(&self, amount: u64) {
}
fn gas_left(&self) -> u64 {
self.gas
}
fn current_address(&self) -> [u8; 20] {
[0u8; 20]
}
}

#[cfg(test)]
mod tests {
use super::{EwasmAPI, TestImpl};

#[test]
fn consume_gas_func() {
assert_eq!(0, EwasmAPI::gas_left(&<TestImpl>::default()));
assert_eq!(TestImpl::default().gas_left(), 0);
}
}

0 comments on commit d21dcb6

Please sign in to comment.