Skip to content

Commit c0034e2

Browse files
committed
Turn EwasmAPI into a trait
1 parent 07229ce commit c0034e2

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/lib.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,3 +636,60 @@ pub fn selfdestruct(address: &Address) -> ! {
636636
native::ethereum_selfDestruct(address.bytes.as_ptr() as *const u32);
637637
}
638638
}
639+
640+
pub trait EwasmAPI {
641+
fn consume_gas(&self, amount: u64);
642+
fn gas_left(&self) -> u64;
643+
fn current_address(&self) -> [u8; 20];
644+
}
645+
646+
#[derive(Debug)]
647+
pub struct NativeImpl;
648+
649+
#[derive(Debug, Default)]
650+
pub struct TestImpl {
651+
gas: u64,
652+
}
653+
654+
/*
655+
trait TestSetter {
656+
fn set_gas(mut self, amount: u64);
657+
}
658+
659+
impl TestSetter for TestImpl {
660+
fn set_gas(mut self, amount: u64) {
661+
self.gas = amount
662+
}
663+
}
664+
*/
665+
666+
impl EwasmAPI for NativeImpl {
667+
fn consume_gas(&self, amount: u64) {}
668+
fn gas_left(&self) -> u64 {
669+
gas_left()
670+
}
671+
fn current_address(&self) -> [u8; 20] {
672+
[0u8; 20]
673+
}
674+
}
675+
676+
impl EwasmAPI for TestImpl {
677+
fn consume_gas(&self, amount: u64) {}
678+
fn gas_left(&self) -> u64 {
679+
self.gas
680+
}
681+
fn current_address(&self) -> [u8; 20] {
682+
[0u8; 20]
683+
}
684+
}
685+
686+
#[cfg(test)]
687+
mod tests {
688+
use super::{EwasmAPI, TestImpl};
689+
690+
#[test]
691+
fn consume_gas_func() {
692+
assert_eq!(0, EwasmAPI::gas_left(&<TestImpl>::default()));
693+
assert_eq!(TestImpl::default().gas_left(), 0);
694+
}
695+
}

0 commit comments

Comments
 (0)