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:
parent
0b68c08852
commit
e90248898c
3 changed files with 10 additions and 6 deletions
|
|
@ -15,9 +15,9 @@ pub enum PngError {
|
||||||
InvalidData,
|
InvalidData,
|
||||||
InvalidDepthForType(BitDepth, ColorType),
|
InvalidDepthForType(BitDepth, ColorType),
|
||||||
NotPNG,
|
NotPNG,
|
||||||
ReadFailed(String, std::io::Error),
|
ReadFailed(Box<str>, std::io::Error),
|
||||||
TruncatedData,
|
TruncatedData,
|
||||||
WriteFailed(String, std::io::Error),
|
WriteFailed(Box<str>, std::io::Error),
|
||||||
Other(Box<str>),
|
Other(Box<str>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -257,8 +257,9 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
|
||||||
let output_path = path
|
let output_path = path
|
||||||
.as_ref()
|
.as_ref()
|
||||||
.map_or_else(|| input.path().unwrap(), PathBuf::as_path);
|
.map_or_else(|| input.path().unwrap(), PathBuf::as_path);
|
||||||
let out_file = File::create(output_path)
|
let out_file = File::create(output_path).map_err(|err| {
|
||||||
.map_err(|err| PngError::WriteFailed(output_path.display().to_string(), err))?;
|
PngError::WriteFailed(output_path.display().to_string().into_boxed_str(), err)
|
||||||
|
})?;
|
||||||
if let Some(metadata_input) = &opt_metadata_preserved {
|
if let Some(metadata_input) = &opt_metadata_preserved {
|
||||||
copy_permissions(metadata_input, &out_file)?;
|
copy_permissions(metadata_input, &out_file)?;
|
||||||
}
|
}
|
||||||
|
|
@ -268,7 +269,9 @@ pub fn optimize(input: &InFile, output: &OutFile, opts: &Options) -> PngResult<(
|
||||||
.write_all(&optimized_output)
|
.write_all(&optimized_output)
|
||||||
// flush BufWriter so IO errors don't get swallowed silently on close() by drop!
|
// flush BufWriter so IO errors don't get swallowed silently on close() by drop!
|
||||||
.and_then(|()| buffer.flush())
|
.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
|
// force drop and thereby closing of file handle before modifying any timestamp
|
||||||
std::mem::drop(buffer);
|
std::mem::drop(buffer);
|
||||||
if let Some(metadata_input) = &opt_metadata_preserved {
|
if let Some(metadata_input) = &opt_metadata_preserved {
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,8 @@ impl PngData {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_file(filepath: &Path) -> Result<Vec<u8>, PngError> {
|
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
|
/// Create a new `PngData` struct by reading a slice
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue