Skip to content

Commit

Permalink
Use boxed slices instead of Vec in public 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.
  • Loading branch information
Ortham committed Jun 27, 2024
1 parent d75c76e commit 4b31303
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod plugin;
pub unsafe extern "C" fn esp_get_plugins_metadata(
plugins: *const *const Plugin,
plugins_len: size_t,
plugins_metadata: *mut *mut Vec<PluginMetadata>,
plugins_metadata: *mut *mut Box<[PluginMetadata]>,
) -> u32 {
panic::catch_unwind(|| {
if plugins.is_null() || plugins_metadata.is_null() {
Expand Down
4 changes: 2 additions & 2 deletions 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: *mut Box<[PluginMetadata]>,
) -> u32 {
panic::catch_unwind(|| {
if plugin_ptr.is_null() {
Expand All @@ -131,7 +131,7 @@ pub unsafe extern "C" fn esp_plugin_resolve_record_ids(
let plugins_metadata = if plugins_metadata.is_null() {
&[]
} else {
(*plugins_metadata).as_slice()
(*plugins_metadata).as_ref()
};

match plugin.resolve_record_ids(plugins_metadata) {
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.to_vec().into(),
ParsingErrorKind::GenericParserError(err.code.description().to_string()),
),
}
Expand Down
24 changes: 12 additions & 12 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 @@ -542,7 +542,7 @@ pub struct PluginMetadata {
}

// Get PluginMetadata objects for a collection of loaded plugins.
pub fn plugins_metadata(plugins: &[&Plugin]) -> Result<Vec<PluginMetadata>, Error> {
pub fn plugins_metadata(plugins: &[&Plugin]) -> Result<Box<[PluginMetadata]>, Error> {
let mut vec = Vec::new();

for plugin in plugins {
Expand All @@ -562,13 +562,13 @@ pub fn plugins_metadata(plugins: &[&Plugin]) -> Result<Vec<PluginMetadata>, Erro
let metadata = PluginMetadata {
filename,
scale: plugin.scale(),
record_ids: record_ids.into_boxed_slice(),
record_ids: record_ids.into(),
};

vec.push(metadata);
}

Ok(vec)
Ok(vec.into())
}

fn sorted_slices_intersect<T: PartialOrd>(left: &[T], right: &[T]) -> bool {
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 Expand Up @@ -2447,7 +2447,7 @@ mod tests {
let metadata = plugins_metadata(&[&plugin1, &plugin2, &plugin3]).unwrap();

assert_eq!(
vec![
[
PluginMetadata {
filename: "Blank.full.esm".to_string(),
scale: PluginScale::Full,
Expand All @@ -2463,8 +2463,8 @@ mod tests {
scale: PluginScale::Small,
record_ids: Box::new([]),
},
],
metadata
].as_slice(),
metadata.as_ref()
);
}

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 4b31303

Please sign in to comment.