Skip to content

Commit

Permalink
Fix constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jakelang committed Mar 13, 2019
1 parent e635949 commit 92b5eae
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions bindings/rust/evmc-vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,45 @@ pub use evmc_sys as ffi;
pub struct EvmcResult(Box<ffi::evmc_result>);

impl EvmcResult {
pub fn new(
status: ffi::evmc_status_code,
gasleft: i64,
output: &[u8],
) -> Self {
pub fn new(status: ffi::evmc_status_code, gasleft: i64, output: Option<&[u8]>) -> Self {
EvmcResult(Box::new(ffi::evmc_result {
status_code: status,
gas_left: gasleft,
output_data: output.as_ptr(),
output_size: output.len(),
output_data: if let Some(buf) = output {
buf.clone().as_ptr()
} else {
std::ptr::null()
},
output_size: if let Some(buf) = output { buf.len() } else { 0 },
release: Some(release_result),
create_address: ffi::evmc_address { bytes: [0u8; 20] },
padding: [0u8; 4],
}))
}

pub fn gasleft(&self) -> i64 {
self.0.gas_left
}

pub fn create_addr(&self) -> &ffi::evmc_address {
&self.0.create_address
}

pub fn output(&self) -> Option<&[u8]> {
if !self.0.output_data.is_null() && self.0.output_size != 0 {
Some(unsafe { std::slice::from_raw_parts::<u8>(self.0.output_data, self.0.output_size) })
} else {
None
}
}

pub fn status(&self) -> ffi::evmc_status_code {
self.0.status_code
}
}

#[no_mangle]
pub extern "C" fn release_result(result: *const ffi::evmc_result) {
extern "C" fn release_result(result: *const ffi::evmc_result) {
unsafe {
Box::from_raw(result as *mut ffi::evmc_result);
}
Expand Down

0 comments on commit 92b5eae

Please sign in to comment.