Skip to content

Commit 84fe70b

Browse files
committed
Turn EwasmAPI into a trait
1 parent 2fff902 commit 84fe70b

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

0 commit comments

Comments
 (0)