Skip to content

Commit eac15a2

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

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/lib.rs

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

0 commit comments

Comments
 (0)