diff --git a/zlib-rs/src/inflate.rs b/zlib-rs/src/inflate.rs index bcba4db..cde1fe7 100644 --- a/zlib-rs/src/inflate.rs +++ b/zlib-rs/src/inflate.rs @@ -4,6 +4,7 @@ use core::ffi::{c_char, c_int, c_long, c_ulong}; use core::marker::PhantomData; use core::mem::MaybeUninit; +use core::ops::ControlFlow; mod bitreader; mod inffixed_tbl; @@ -526,13 +527,13 @@ impl State<'_> { // // It unfortunately does duplicate the code for some of the states; deduplicating it by having // more of the states call this function is slower. - fn len_and_friends(&mut self) -> Option { + fn len_and_friends(&mut self) -> ControlFlow { let avail_in = self.bit_reader.bytes_remaining(); let avail_out = self.writer.remaining(); if avail_in >= INFLATE_FAST_MIN_HAVE && avail_out >= INFLATE_FAST_MIN_LEFT { inflate_fast_help(self, 0); - return None; + return ControlFlow::Continue(()); } let mut mode; @@ -584,7 +585,7 @@ impl State<'_> { if avail_in >= INFLATE_FAST_MIN_HAVE && avail_out >= INFLATE_FAST_MIN_LEFT { restore!(); inflate_fast_help(self, 0); - return None; + return ControlFlow::Continue(()); } self.back = 0; @@ -601,7 +602,7 @@ impl State<'_> { if let Err(return_code) = bit_reader.pull_byte() { restore!(); - return Some(return_code); + return ControlFlow::Break(return_code); }; } @@ -616,7 +617,7 @@ impl State<'_> { if let Err(return_code) = bit_reader.pull_byte() { restore!(); - return Some(return_code); + return ControlFlow::Break(return_code); }; } @@ -640,7 +641,7 @@ impl State<'_> { mode = Mode::Type; restore!(); - return None; + return ControlFlow::Continue(()); } else if here.op & 64 != 0 { mode = Mode::Bad; { @@ -650,7 +651,7 @@ impl State<'_> { #[cfg(all(feature = "std", test))] dbg!(msg); this.error_message = Some(msg); - return Some(this.inflate_leave(ReturnCode::DataError)); + return ControlFlow::Break(ReturnCode::DataError); } } else { // length code @@ -665,7 +666,7 @@ impl State<'_> { restore!(); #[cfg(all(test, feature = "std"))] eprintln!("Ok: writer is full ({} bytes)", self.writer.capacity()); - return Some(self.inflate_leave(ReturnCode::Ok)); + return ControlFlow::Break(ReturnCode::Ok); } writer.push(self.length as u8); @@ -683,7 +684,7 @@ impl State<'_> { match bit_reader.need_bits(extra) { Err(return_code) => { restore!(); - return Some(self.inflate_leave(return_code)); + return ControlFlow::Break(return_code); } Ok(v) => v, }; @@ -713,7 +714,7 @@ impl State<'_> { if let Err(return_code) = bit_reader.pull_byte() { restore!(); - return Some(return_code); + return ControlFlow::Break(return_code); }; } @@ -730,7 +731,7 @@ impl State<'_> { if let Err(return_code) = bit_reader.pull_byte() { restore!(); - return Some(return_code); + return ControlFlow::Break(return_code); }; } @@ -743,7 +744,7 @@ impl State<'_> { if here.op & 64 != 0 { restore!(); self.mode = Mode::Bad; - return Some(self.bad("invalid distance code\0")); + return ControlFlow::Break(self.bad("invalid distance code\0")); } self.offset = here.val as usize; @@ -761,7 +762,7 @@ impl State<'_> { match bit_reader.need_bits(extra) { Err(return_code) => { restore!(); - return Some(self.inflate_leave(return_code)); + return ControlFlow::Break(return_code); } Ok(v) => v, }; @@ -773,7 +774,9 @@ impl State<'_> { if INFLATE_STRICT && self.offset > self.dmax { restore!(); self.mode = Mode::Bad; - return Some(self.bad("invalid distance code too far back\0")); + return ControlFlow::Break( + self.bad("invalid distance code too far back\0"), + ); } // eprintln!("inflate: distance {}", state.offset); @@ -791,7 +794,7 @@ impl State<'_> { "BufError: writer is full ({} bytes)", self.writer.capacity() ); - return Some(self.inflate_leave(ReturnCode::Ok)); + return ControlFlow::Break(ReturnCode::Ok); } let left = writer.remaining(); @@ -806,7 +809,9 @@ impl State<'_> { if self.flags.contains(Flags::SANE) { restore!(); self.mode = Mode::Bad; - return Some(self.bad("invalid distance too far back\0")); + return ControlFlow::Break( + self.bad("invalid distance too far back\0"), + ); } // TODO INFLATE_ALLOW_INVALID_DISTANCE_TOOFAR_ARRR @@ -1395,8 +1400,8 @@ impl State<'_> { continue 'label; } Mode::Len => match self.len_and_friends() { - Some(return_code) => break 'label return_code, - None => continue 'label, + ControlFlow::Break(return_code) => break 'label return_code, + ControlFlow::Continue(()) => continue 'label, }, Mode::LenExt => { // NOTE: this branch must be kept in sync with its counterpart in `len_and_friends`