From 533c29457175c5d7d8c0a4150d00b59af64eeca6 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Thu, 27 Jun 2024 17:35:47 +0100 Subject: [PATCH] Use boxed slices instead of Vec in public Rust API None of the data returned is meaningfully extensible, so reflect that in the types used. plugins_metadata() returns a Vec because the FFI layer needs a Vec, as cbindgen doesn't support representing a Box<[T]> in a C header, so there's no point converting back and forth. --- ffi/src/plugin.rs | 2 +- src/error.rs | 6 +++--- src/plugin.rs | 12 ++++++------ src/record.rs | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/ffi/src/plugin.rs b/ffi/src/plugin.rs index fffbaa1..0a6c7a7 100644 --- a/ffi/src/plugin.rs +++ b/ffi/src/plugin.rs @@ -121,7 +121,7 @@ pub unsafe extern "C" fn esp_plugin_parse(plugin_ptr: *mut Plugin, load_header_o #[no_mangle] pub unsafe extern "C" fn esp_plugin_resolve_record_ids( plugin_ptr: *mut Plugin, - plugins_metadata: *mut Vec, + plugins_metadata: *const Vec, ) -> u32 { panic::catch_unwind(|| { if plugin_ptr.is_null() { diff --git a/src/error.rs b/src/error.rs index e9df333..67c0656 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,8 +30,8 @@ pub enum Error { IoError(io::Error), NoFilename(PathBuf), ParsingIncomplete(MoreDataNeeded), - ParsingError(Vec, ParsingErrorKind), - DecodeError(Vec), + ParsingError(Box<[u8]>, ParsingErrorKind), + DecodeError(Box<[u8]>), UnresolvedRecordIds(PathBuf), PluginMetadataNotFound(String), } @@ -46,7 +46,7 @@ impl From>> for Error { Error::ParsingIncomplete(MoreDataNeeded::Size(size)) } Err::Error(err) | Err::Failure(err) => Error::ParsingError( - err.input.to_vec(), + err.input.into(), ParsingErrorKind::GenericParserError(err.code.description().to_string()), ), } diff --git a/src/plugin.rs b/src/plugin.rs index 57c27a3..3ba6046 100644 --- a/src/plugin.rs +++ b/src/plugin.rs @@ -199,7 +199,7 @@ impl Plugin { .map(std::string::ToString::to_string) } - pub fn masters(&self) -> Result, Error> { + pub fn masters(&self) -> Result, Error> { masters(&self.data.header_record) } @@ -288,7 +288,7 @@ impl Plugin { if subrecord.subrecord_type() == target_subrecord_type { if subrecord.data().len() <= description_offset { return Err(Error::ParsingError( - subrecord.data().to_vec(), + subrecord.data().into(), ParsingErrorKind::SubrecordDataTooShort(description_offset), )); } @@ -298,7 +298,7 @@ impl Plugin { return WINDOWS_1252 .decode_without_bom_handling_and_without_replacement(data) .map(|s| Some(s.to_string())) - .ok_or(Error::DecodeError(data.to_vec())); + .ok_or(Error::DecodeError(data.into())); } } @@ -719,7 +719,7 @@ fn hashed_masters_for_starfield( Ok(hashed_masters) } -fn masters(header_record: &Record) -> Result, Error> { +fn masters(header_record: &Record) -> Result, Error> { header_record .subrecords() .iter() @@ -729,9 +729,9 @@ fn masters(header_record: &Record) -> Result, Error> { WINDOWS_1252 .decode_without_bom_handling_and_without_replacement(d) .map(|s| s.to_string()) - .ok_or(Error::DecodeError(d.to_vec())) + .ok_or(Error::DecodeError(d.into())) }) - .collect::, Error>>() + .collect() } fn read_form_ids(reader: &mut R, game_id: GameId) -> Result, Error> { diff --git a/src/record.rs b/src/record.rs index 41b87cb..b80b323 100644 --- a/src/record.rs +++ b/src/record.rs @@ -72,7 +72,7 @@ impl Record { if &header_bytes[0..4] != expected_type { // Take a copy of 16 bytes so the output includes the FormID. return Err(Error::ParsingError( - header_bytes[..16].to_vec(), + header_bytes[..16].into(), ParsingErrorKind::UnexpectedRecordType(expected_type.to_vec()), )); }