From 315137edd2e80bc11137749ee09c4c9abfe81f19 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Tue, 25 Jun 2019 19:07:57 +0100 Subject: [PATCH] rust: prefer getters as opposed to pub members on structs --- bindings/rust/evmc-vm/src/lib.rs | 62 +++++++++++++++++++++++------ examples/example-rust-vm/src/lib.rs | 2 +- 2 files changed, 51 insertions(+), 13 deletions(-) diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index 13b8a2f..84f87b6 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -23,15 +23,15 @@ pub struct ExecutionResult { /// EVMC execution message structure. pub struct ExecutionMessage { - pub kind: ffi::evmc_call_kind, - pub flags: u32, - pub depth: i32, - pub gas: i64, - pub destination: ffi::evmc_address, - pub sender: ffi::evmc_address, - pub input: Option>, - pub value: ffi::evmc_uint256be, - pub create2_salt: ffi::evmc_bytes32, + kind: ffi::evmc_call_kind, + flags: u32, + depth: i32, + gas: i64, + destination: ffi::evmc_address, + sender: ffi::evmc_address, + input: Option>, + value: ffi::evmc_uint256be, + create2_salt: ffi::evmc_bytes32, } /// EVMC context structure. Exposes the EVMC host functions, message data, and transaction context @@ -81,6 +81,44 @@ impl ExecutionResult { } } +impl ExecutionMessage { + pub fn kind(&self) -> ffi::evmc_call_kind { + self.kind + } + + pub fn flags(&self) -> u32 { + self.flags + } + + pub fn depth(&self) -> i32 { + self.depth + } + + pub fn gas(&self) -> i64 { + self.gas + } + + pub fn destination(&self) -> &ffi::evmc_address { + &self.destination + } + + pub fn sender(&self) -> &ffi::evmc_address { + &self.sender + } + + pub fn input(&self) -> Option<&Vec> { + self.input.as_ref() + } + + pub fn value(&self) -> &ffi::evmc_uint256be { + &self.value + } + + pub fn create2_salt(&self) -> &ffi::evmc_bytes32 { + &self.create2_salt + } +} + impl<'a> ExecutionContext<'a> { pub fn new(_message: &'a ffi::evmc_message, _context: &'a mut ffi::evmc_context) -> Self { let _tx_context = unsafe { @@ -609,10 +647,10 @@ mod tests { assert_eq!(c.depth, d.depth); assert_eq!(c.gas, d.gas); if d.input_data.is_null() { - assert!(c.input.is_none()); + assert!(c.input().is_none()); } else { - assert!(c.input.is_some()); - assert_eq!(c.input.clone().unwrap().len(), d.input_size); + assert!(c.input().is_some()); + assert_eq!(c.input().unwrap().len(), d.input_size); } dummy_context_dispose(context_raw); diff --git a/examples/example-rust-vm/src/lib.rs b/examples/example-rust-vm/src/lib.rs index a617dad..90a00ec 100644 --- a/examples/example-rust-vm/src/lib.rs +++ b/examples/example-rust-vm/src/lib.rs @@ -15,7 +15,7 @@ impl EvmcVm for ExampleRustVM { } fn execute(&self, _code: &[u8], context: &ExecutionContext) -> ExecutionResult { - let is_create = context.get_message().kind == evmc_sys::evmc_call_kind::EVMC_CREATE; + let is_create = context.get_message().kind() == evmc_sys::evmc_call_kind::EVMC_CREATE; if is_create { ExecutionResult::failure()