From d75c76e959d625d502a9c10916ca26d980742a5a Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Thu, 27 Jun 2024 17:34:22 +0100 Subject: [PATCH] Use boxed slices instead of Vec in FFI conversions Since they're not meaningfully growable. --- ffi/src/helpers.rs | 16 ++++++++++++---- ffi/src/lib.rs | 8 ++------ ffi/src/plugin.rs | 26 +++++++++++--------------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/ffi/src/helpers.rs b/ffi/src/helpers.rs index b721bff..77cfa0b 100644 --- a/ffi/src/helpers.rs +++ b/ffi/src/helpers.rs @@ -2,7 +2,7 @@ use std::ffi::{CStr, CString}; use libc::{c_char, size_t}; -use esplugin::GameId; +use esplugin::{GameId, Plugin}; use crate::{constants::*, error::error}; @@ -43,9 +43,7 @@ pub fn to_c_string_array(strings: &[String]) -> Result<(*mut *mut c_char, size_t let mut c_strings = strings .iter() .map(|s| to_c_string(s)) - .collect::, u32>>()?; - - c_strings.shrink_to_fit(); + .collect::, u32>>()?; let pointer = c_strings.as_mut_ptr(); let size = c_strings.len(); @@ -54,6 +52,16 @@ pub fn to_c_string_array(strings: &[String]) -> Result<(*mut *mut c_char, size_t Ok((pointer, size)) } +pub unsafe fn to_plugin_refs_slice<'a>( + array: *const *const Plugin, + array_size: usize, +) -> Option> { + std::slice::from_raw_parts(array, array_size) + .iter() + .map(|pointer| pointer.as_ref()) + .collect() +} + pub fn map_game_id(game_id: u32) -> Result { match game_id { x if x == ESP_GAME_OBLIVION => Ok(GameId::Oblivion), diff --git a/ffi/src/lib.rs b/ffi/src/lib.rs index 40ea8fe..d5e2b03 100644 --- a/ffi/src/lib.rs +++ b/ffi/src/lib.rs @@ -18,6 +18,7 @@ use self::error::error; use error::handle_error; use esplugin::Plugin; use esplugin::PluginMetadata; +use helpers::to_plugin_refs_slice; use libc::size_t; pub use self::common::*; @@ -40,12 +41,7 @@ pub unsafe extern "C" fn esp_get_plugins_metadata( if plugins.is_null() || plugins_metadata.is_null() { error(ESP_ERROR_NULL_POINTER, "Null pointer passed") } else { - let plugins: Option> = std::slice::from_raw_parts(plugins, plugins_len) - .iter() - .map(|pointer| pointer.as_ref()) - .collect(); - - let plugins = match plugins { + let plugins = match to_plugin_refs_slice(plugins, plugins_len) { Some(p) => p, None => { return error( diff --git a/ffi/src/plugin.rs b/ffi/src/plugin.rs index e835c25..fffbaa1 100644 --- a/ffi/src/plugin.rs +++ b/ffi/src/plugin.rs @@ -489,21 +489,17 @@ pub unsafe extern "C" fn esp_plugin_records_overlap_size( error(ESP_ERROR_NULL_POINTER, "Null pointer passed") } else { let plugin = &*plugin_ptr; - let other_plugins: Option> = - std::slice::from_raw_parts(other_plugins_ptr, other_plugins_ptr_count) - .iter() - .map(|pointer| pointer.as_ref()) - .collect(); - - let other_plugins = match other_plugins { - Some(p) => p, - None => { - return error( - ESP_ERROR_NULL_POINTER, - "Null pointer passed in plugins array", - ) - } - }; + + let other_plugins = + match to_plugin_refs_slice(other_plugins_ptr, other_plugins_ptr_count) { + Some(p) => p, + None => { + return error( + ESP_ERROR_NULL_POINTER, + "Null pointer passed in plugins array", + ) + } + }; match plugin.overlap_size(&other_plugins) { Ok(x) => {