Shrink the PngError type

This brings `PngError` from 40 to 32 bytes on a 64-bit system, and
should only have a cost when a a `ReadFailed` or `WriteFailed` is
created
This commit is contained in:
Luracasmus 2025-12-09 11:23:12 +01:00 committed by andrews05
parent 0b68c08852
commit e90248898c
3 changed files with 10 additions and 6 deletions

View file

@ -15,9 +15,9 @@ pub enum PngError {
InvalidData,
InvalidDepthForType(BitDepth, ColorType),
NotPNG,
ReadFailed(String, std::io::Error),
ReadFailed(Box<str>, std::io::Error),
TruncatedData,
WriteFailed(String, std::io::Error),
WriteFailed(Box<str>, std::io::Error),
Other(Box<str>),
}

View file

@ -257,8 +257,9 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
let output_path = path
.as_ref()
.map_or_else(|| input.path().unwrap(), PathBuf::as_path);
let out_file = File::create(output_path)
.map_err(|err| PngError::WriteFailed(output_path.display().to_string(), err))?;
let out_file = File::create(output_path).map_err(|err| {
PngError::WriteFailed(output_path.display().to_string().into_boxed_str(), err)
})?;
if let Some(metadata_input) = &opt_metadata_preserved {
copy_permissions(metadata_input, &out_file)?;
}
@ -268,7 +269,9 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
.write_all(&optimized_output)
// flush BufWriter so IO errors don't get swallowed silently on close() by drop!
.and_then(|()| buffer.flush())
.map_err(|e| PngError::WriteFailed(output_path.display().to_string(), e))?;
.map_err(|e| {
PngError::WriteFailed(output_path.display().to_string().into_boxed_str(), e)
})?;
// force drop and thereby closing of file handle before modifying any timestamp
std::mem::drop(buffer);
if let Some(metadata_input) = &opt_metadata_preserved {

View file

@ -52,7 +52,8 @@ impl PngData {
}
pub fn read_file(filepath: &Path) -> Result<Vec<u8>, PngError> {
fs::read(filepath).map_err(|e| PngError::ReadFailed(filepath.display().to_string(), e))
fs::read(filepath)
.map_err(|e| PngError::ReadFailed(filepath.display().to_string().into_boxed_str(), e))
}
/// Create a new `PngData` struct by reading a slice