Skip to content

Commit

Permalink
fn len_and_friends: return ControlFlow instead of Option
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Dec 9, 2024
1 parent 2593449 commit ad248e5
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions zlib-rs/src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ReturnCode> {
fn len_and_friends(&mut self) -> ControlFlow<ReturnCode, ()> {
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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
};
}

Expand All @@ -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);
};
}

Expand All @@ -640,7 +641,7 @@ impl State<'_> {
mode = Mode::Type;

restore!();
return None;
return ControlFlow::Continue(());
} else if here.op & 64 != 0 {
mode = Mode::Bad;
{
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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,
};
Expand Down Expand Up @@ -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);
};
}

Expand All @@ -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);
};
}

Expand All @@ -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;
Expand All @@ -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,
};
Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -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
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit ad248e5

Please sign in to comment.