diff --git a/bindings/rust/evmc-vm/src/lib.rs b/bindings/rust/evmc-vm/src/lib.rs index 70c3910..6b35346 100644 --- a/bindings/rust/evmc-vm/src/lib.rs +++ b/bindings/rust/evmc-vm/src/lib.rs @@ -357,17 +357,7 @@ impl From for ExecutionResult { } else if result.output_size == 0 { None } else { - // Pre-allocate a vector. - let mut buf: Vec = Vec::with_capacity(result.output_size); - - unsafe { - // Set the len of the vec manually. - buf.set_len(result.output_size); - // Copy from the C struct's buffer to the vec's buffer. - std::ptr::copy(result.output_data, buf.as_mut_ptr(), result.output_size); - } - - Some(buf) + Some(from_buf_raw::(result.output_data, result.output_size)) }, // Consider it is always valid. create_address: Some(result.create_address), @@ -469,17 +459,7 @@ impl From<&ffi::evmc_message> for ExecutionMessage { } else if message.input_size == 0 { None } else { - // Pre-allocate a vector. - let mut buf: Vec = Vec::with_capacity(message.input_size); - - unsafe { - // Set the len of the vec manually. - buf.set_len(message.input_size); - // Copy from the C struct's buffer to the vec's buffer. - std::ptr::copy(message.input_data, buf.as_mut_ptr(), message.input_size); - } - - Some(buf) + Some(from_buf_raw::(message.input_data, message.input_size)) }, value: message.value, create2_salt: message.create2_salt, @@ -487,6 +467,18 @@ impl From<&ffi::evmc_message> for ExecutionMessage { } } +fn from_buf_raw(ptr: *const T, size: usize) -> Vec { + // Pre-allocate a vector. + let mut buf = Vec::with_capacity(size); + unsafe { + // Set the len of the vec manually. + buf.set_len(size); + // Copy from the C buffer to the vec's buffer. + std::ptr::copy(ptr, buf.as_mut_ptr(), size); + } + buf +} + #[cfg(test)] mod tests { use super::*;