Skip to content

Commit 4142537

Browse files
committed
Turn EwasmAPI into a trait
1 parent baa76bb commit 4142537

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

src/lib.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,3 +587,68 @@ pub fn selfdestruct(address: &Address) -> ! {
587587
native::ethereum_selfDestruct(address.bytes.as_ptr() as *const u32);
588588
}
589589
}
590+
591+
pub trait EwasmAPI: Send + Sync {
592+
fn consume_gas(mut self, amount: u64);
593+
fn gas_left(self) -> u64;
594+
fn current_address() -> [u8; 20];
595+
}
596+
597+
#[derive(Debug)]
598+
pub struct NativeImpl;
599+
600+
#[derive(Debug)]
601+
pub struct TestImpl {
602+
gas: u64,
603+
}
604+
605+
impl Default for TestImpl {
606+
fn default() -> TestImpl {
607+
TestImpl { gas: 0 }
608+
}
609+
}
610+
611+
/*
612+
trait TestSetter {
613+
fn set_gas(mut self, amount: u64);
614+
}
615+
616+
impl TestSetter for TestImpl {
617+
fn set_gas(mut self, amount: u64) {
618+
self.gas = amount
619+
}
620+
}
621+
*/
622+
623+
impl EwasmAPI for NativeImpl {
624+
fn consume_gas(self, amount: u64) {}
625+
fn gas_left(self) -> u64 {
626+
0
627+
}
628+
fn current_address() -> [u8; 20] {
629+
[0u8; 20]
630+
}
631+
}
632+
633+
impl EwasmAPI for TestImpl {
634+
fn consume_gas(mut self, amount: u64) {
635+
self.gas
636+
}
637+
fn gas_left(self) -> u64 {
638+
self.gas
639+
}
640+
fn current_address() -> [u8; 20] {
641+
[0u8; 20]
642+
}
643+
}
644+
645+
#[cfg(test)]
646+
mod tests {
647+
use super::{EwasmAPI, TestImpl};
648+
649+
#[test]
650+
fn consume_gas_func() {
651+
assert_eq!(0, EwasmAPI::gas_left(<TestImpl>::default()));
652+
assert_eq!(TestImpl::default().gas_left(), 0);
653+
}
654+
}

0 commit comments

Comments
 (0)