Skip to content

Commit

Permalink
Merge pull request #392 from ethereum/phantom-data
Browse files Browse the repository at this point in the history
rust: properly bind lifetimes in EvmcVm
  • Loading branch information
axic authored Aug 9, 2019
2 parents 598432e + f9c05b5 commit 8634f0b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
13 changes: 10 additions & 3 deletions bindings/rust/evmc-vm/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ use crate::EvmcVm;
use std::ops::Deref;

/// Container struct for EVMC instances and user-defined data.
pub struct EvmcContainer<T: EvmcVm + Sized> {
pub struct EvmcContainer<T>
where
T: EvmcVm + Sized,
{
#[allow(dead_code)]
instance: ::evmc_sys::evmc_instance,
vm: T,
}

impl<T: EvmcVm + Sized> EvmcContainer<T> {
impl<T> EvmcContainer<T>
where
T: EvmcVm + Sized,
{
/// Basic constructor.
pub fn new(_instance: ::evmc_sys::evmc_instance) -> Self {
Self {
Expand Down Expand Up @@ -126,8 +132,8 @@ mod tests {
emit_log: None,
};
let mut backing_context = ::evmc_sys::evmc_context { host: &host };
let mut context = ExecutionContext::new(&mut backing_context);

let mut context = ExecutionContext::new(&mut backing_context);
let container = EvmcContainer::<TestVm>::new(instance);
assert_eq!(
container
Expand All @@ -143,6 +149,7 @@ mod tests {

let ptr = unsafe { EvmcContainer::into_ffi_pointer(Box::new(container)) };

let mut context = ExecutionContext::new(&mut backing_context);
let container = unsafe { EvmcContainer::<TestVm>::from_ffi_pointer(ptr) };
assert_eq!(
container
Expand Down
10 changes: 5 additions & 5 deletions bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ pub use types::*;
/// Trait EVMC VMs have to implement.
pub trait EvmcVm {
fn init() -> Self;
fn execute(
fn execute<'a>(
&self,
revision: ffi::evmc_revision,
code: &[u8],
message: &ExecutionMessage,
context: &mut ExecutionContext,
code: &'a [u8],
message: &'a ExecutionMessage,
context: &'a mut ExecutionContext<'a>,
) -> ExecutionResult;
}

Expand Down Expand Up @@ -806,7 +806,7 @@ mod tests {
// sanitizers to complain
let mut context_raw_copy = context_raw.clone();

let mut exe_context = ExecutionContext::new(&mut context_raw);
let exe_context = ExecutionContext::new(&mut context_raw);
let a = exe_context.get_tx_context();
let b = unsafe { get_dummy_tx_context(&mut context_raw_copy as *mut ffi::evmc_context) };

Expand Down
8 changes: 4 additions & 4 deletions examples/example-rust-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ impl EvmcVm for ExampleRustVM {
ExampleRustVM {}
}

fn execute(
fn execute<'a>(
&self,
_revision: evmc_sys::evmc_revision,
_code: &[u8],
message: &ExecutionMessage,
_context: &mut ExecutionContext,
_code: &'a [u8],
message: &'a ExecutionMessage,
_context: &'a mut ExecutionContext<'a>,
) -> ExecutionResult {
if message.kind() != evmc_sys::evmc_call_kind::EVMC_CALL {
return ExecutionResult::failure();
Expand Down

0 comments on commit 8634f0b

Please sign in to comment.