Skip to content

Commit

Permalink
Use boxed slices instead of Vec in FFI conversions
Browse files Browse the repository at this point in the history
Since they're not meaningfully growable.
  • Loading branch information
Ortham committed Jun 27, 2024
1 parent 168bf70 commit d75c76e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
16 changes: 12 additions & 4 deletions ffi/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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::<Result<Vec<*mut c_char>, u32>>()?;

c_strings.shrink_to_fit();
.collect::<Result<Box<[*mut c_char]>, u32>>()?;

let pointer = c_strings.as_mut_ptr();
let size = c_strings.len();
Expand All @@ -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<Box<[&'a Plugin]>> {
std::slice::from_raw_parts(array, array_size)
.iter()
.map(|pointer| pointer.as_ref())
.collect()
}

pub fn map_game_id(game_id: u32) -> Result<GameId, u32> {
match game_id {
x if x == ESP_GAME_OBLIVION => Ok(GameId::Oblivion),
Expand Down
8 changes: 2 additions & 6 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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<Vec<&Plugin>> = 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(
Expand Down
26 changes: 11 additions & 15 deletions ffi/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<&Plugin>> =
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) => {
Expand Down

0 comments on commit d75c76e

Please sign in to comment.