From 2c061914b15d376802205d53f0a79ff554060a53 Mon Sep 17 00:00:00 2001 From: 404Setup <153366651+404Setup@users.noreply.github.com> Date: Mon, 4 May 2026 07:51:42 +0000 Subject: [PATCH] Replace unsafe unwrap() in DeflateEncoder::finish with io::Result error The `DeflateEncoder::finish` method used `.unwrap()` on an `Option` returned by `self.writer.take()`. This change replaces it with `.ok_or_else()`, returning a descriptive `std::io::Error` instead. This prevents potential panics and improves the overall robustness of the streaming API. --- src/stream.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/stream.rs b/src/stream.rs index 1d8b5ce..31c4dd9 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -199,7 +199,9 @@ impl DeflateEncoder { /// but will silently ignore any errors. pub fn finish(mut self) -> io::Result { self.flush_buffer(true)?; - Ok(self.writer.take().unwrap()) + self.writer + .take() + .ok_or_else(|| io::Error::new(io::ErrorKind::Other, "encoder already finished")) } }