Merge pull request #330 from ethereum/rust-container-deref

rust: implement Deref trait for EvmcContainer
This commit is contained in:
Alex Beregszaszi 2019-06-25 18:48:53 +01:00 committed by GitHub
commit e6eab307fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View File

@ -334,6 +334,8 @@ fn build_execute_fn(names: &VMNameSet) -> proc_macro2::TokenStream {
code_size: usize code_size: usize
) -> ::evmc_vm::ffi::evmc_result ) -> ::evmc_vm::ffi::evmc_result
{ {
use evmc_vm::EvmcVm;
assert!(!msg.is_null()); assert!(!msg.is_null());
assert!(!context.is_null()); assert!(!context.is_null());
assert!(!instance.is_null()); assert!(!instance.is_null());

View File

@ -4,8 +4,8 @@
*/ */
use crate::EvmcVm; use crate::EvmcVm;
use crate::ExecutionContext;
use crate::ExecutionResult; use std::ops::Deref;
/// Container struct for EVMC instances and user-defined data. /// Container struct for EVMC instances and user-defined data.
pub struct EvmcContainer<T: EvmcVm + Sized> { pub struct EvmcContainer<T: EvmcVm + Sized> {
@ -33,9 +33,15 @@ impl<T: EvmcVm + Sized> EvmcContainer<T> {
pub unsafe fn into_ffi_pointer(boxed: Box<Self>) -> *const ::evmc_sys::evmc_instance { pub unsafe fn into_ffi_pointer(boxed: Box<Self>) -> *const ::evmc_sys::evmc_instance {
Box::into_raw(boxed) as *const ::evmc_sys::evmc_instance Box::into_raw(boxed) as *const ::evmc_sys::evmc_instance
} }
}
// TODO: Maybe this can just be done with the Deref<Target = T> trait. impl<T> Deref for EvmcContainer<T>
pub fn execute(&self, code: &[u8], context: &ExecutionContext) -> ExecutionResult { where
self.vm.execute(code, context) T: EvmcVm,
{
type Target = T;
fn deref(&self) -> &Self::Target {
&self.vm
} }
} }