Skip to content

Commit 2b82841

Browse files
committed
Turn EwasmAPI into a trait
1 parent 7e7ac2a commit 2b82841

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
@@ -599,3 +599,68 @@ pub fn selfdestruct(address: &Address) -> ! {
599599
native::ethereum_selfDestruct(address.bytes.as_ptr() as *const u32);
600600
}
601601
}
602+
603+
pub trait EwasmAPI: Send + Sync {
604+
fn consume_gas(mut self, amount: u64);
605+
fn gas_left(self) -> u64;
606+
fn current_address() -> [u8; 20];
607+
}
608+
609+
#[derive(Debug)]
610+
pub struct NativeImpl;
611+
612+
#[derive(Debug)]
613+
pub struct TestImpl {
614+
gas: u64,
615+
}
616+
617+
impl Default for TestImpl {
618+
fn default() -> TestImpl {
619+
TestImpl { gas: 0 }
620+
}
621+
}
622+
623+
/*
624+
trait TestSetter {
625+
fn set_gas(mut self, amount: u64);
626+
}
627+
628+
impl TestSetter for TestImpl {
629+
fn set_gas(mut self, amount: u64) {
630+
self.gas = amount
631+
}
632+
}
633+
*/
634+
635+
impl EwasmAPI for NativeImpl {
636+
fn consume_gas(self, amount: u64) {}
637+
fn gas_left(self) -> u64 {
638+
0
639+
}
640+
fn current_address() -> [u8; 20] {
641+
[0u8; 20]
642+
}
643+
}
644+
645+
impl EwasmAPI for TestImpl {
646+
fn consume_gas(mut self, amount: u64) {
647+
self.gas
648+
}
649+
fn gas_left(self) -> u64 {
650+
self.gas
651+
}
652+
fn current_address() -> [u8; 20] {
653+
[0u8; 20]
654+
}
655+
}
656+
657+
#[cfg(test)]
658+
mod tests {
659+
use super::{EwasmAPI, TestImpl};
660+
661+
#[test]
662+
fn consume_gas_func() {
663+
assert_eq!(0, EwasmAPI::gas_left(<TestImpl>::default()));
664+
assert_eq!(TestImpl::default().gas_left(), 0);
665+
}
666+
}

0 commit comments

Comments
 (0)