You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In terms of the API of this crate, one of the simplest use cases (writing a zip file) with (admittedly not well-formatted but easily imaginable) code like this has a footgun:
fnzip_files(paths:&[PathBuf]) -> Result<Vec<u8>,ZipError>{letmut io_cursor = Cursor::new(Vec::<u8>::new());letmut zipwriter = zip::ZipWriter::new(&mut io_cursor);let options = FileOptions::default().compression_method(zip::CompressionMethod::Stored);for path in paths {letmut buffer = vec![];
zipwriter.start_file(path.to_str().unwrap(), options)?;letmut filehandle = std::fs::File::open(path)?;
filehandle.read_to_end(&mut buffer)?;
zipwriter.write_all(&buffer)?;}
zipwriter.finish()?;// Code does not compile without this// drop(zipwriter);Ok(io_cursor.into_inner())}
Without an active drop(), this code does not compile, as the zipwriter keeps the exclusive reference even though it's finished.
This seems unnecessary, since using the writer after completing the zip file is explicitly not recommended behaviour anyway.
This would not be a problem if zipwriter.finish() simply consumed self, which could also drop it after finish() is completed.
This could be solved with an own scope too, but that is obviously somewhat equivalent to the active drop() invocation.
Obviously this would be a breaking change, but I imagine this could be a worthwhile improvement to the API of ZipWriter
The text was updated successfully, but these errors were encountered:
I'm not madly in love with the overall ZipWriter API. Having start_xxx() and finish_xxx() pairs smells like it should be a separate struct. Having start_xxx() consume self and return an io::Write impl, which in turn has a finish() method consuming self and returning the original ZipWriter would categorically prevent logic bugs where you interleave writes to different files. And it would be immediately obvious from the API that you can only write at most one file at a time.
In terms of the API of this crate, one of the simplest use cases (writing a zip file) with (admittedly not well-formatted but easily imaginable) code like this has a footgun:
Without an active drop(), this code does not compile, as the zipwriter keeps the exclusive reference even though it's finished.
This seems unnecessary, since using the writer after completing the zip file is explicitly not recommended behaviour anyway.
This would not be a problem if zipwriter.finish() simply consumed self, which could also drop it after finish() is completed.
This could be solved with an own scope too, but that is obviously somewhat equivalent to the active drop() invocation.
Obviously this would be a breaking change, but I imagine this could be a worthwhile improvement to the API of ZipWriter
The text was updated successfully, but these errors were encountered: