Skip to content

Commit

Permalink
Use boxed slices instead of Vec in public Rust API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Ortham committed Jun 27, 2024
1 parent 723bdbc commit c8cfd8e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion ffi/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PluginMetadata>,
plugins_metadata: *const Vec<PluginMetadata>,
) -> u32 {
panic::catch_unwind(|| {
if plugin_ptr.is_null() {
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub enum Error {
IoError(io::Error),
NoFilename(PathBuf),
ParsingIncomplete(MoreDataNeeded),
ParsingError(Vec<u8>, ParsingErrorKind),
DecodeError(Vec<u8>),
ParsingError(Box<[u8]>, ParsingErrorKind),
DecodeError(Box<[u8]>),
UnresolvedRecordIds(PathBuf),
PluginMetadataNotFound(String),
}
Expand All @@ -46,7 +46,7 @@ impl From<Err<nom::error::Error<&[u8]>>> 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()),
),
}
Expand Down
12 changes: 6 additions & 6 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ impl Plugin {
.map(std::string::ToString::to_string)
}

pub fn masters(&self) -> Result<Vec<String>, Error> {
pub fn masters(&self) -> Result<Box<[String]>, Error> {
masters(&self.data.header_record)
}

Expand Down Expand Up @@ -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),
));
}
Expand All @@ -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()));
}
}

Expand Down Expand Up @@ -719,7 +719,7 @@ fn hashed_masters_for_starfield(
Ok(hashed_masters)
}

fn masters(header_record: &Record) -> Result<Vec<String>, Error> {
fn masters(header_record: &Record) -> Result<Box<[String]>, Error> {
header_record
.subrecords()
.iter()
Expand All @@ -729,9 +729,9 @@ fn masters(header_record: &Record) -> Result<Vec<String>, 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::<Result<Vec<String>, Error>>()
.collect()
}

fn read_form_ids<R: BufRead + Seek>(reader: &mut R, game_id: GameId) -> Result<Vec<u32>, Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
));
}
Expand Down

0 comments on commit c8cfd8e

Please sign in to comment.