Skip to content

Commit

Permalink
add ReadBuf::len and use it
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Feb 12, 2024
1 parent e4458db commit 7b09ffd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
6 changes: 3 additions & 3 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ impl<'a> State<'a> {
"zng_tr_tally: bad literal"
);

self.sym_buf.filled().len() == self.sym_end
self.sym_buf.len() == self.sym_end
}

pub(crate) fn tally_dist(&mut self, mut dist: usize, len: usize) -> bool {
Expand All @@ -712,7 +712,7 @@ impl<'a> State<'a> {

*self.d_desc.dyn_tree[Self::d_code(dist) as usize].freq_mut() += 1;

self.sym_buf.filled().len() == self.sym_end
self.sym_buf.len() == self.sym_end
}

fn detect_data_type(dyn_tree: &[Value]) -> DataType {
Expand Down Expand Up @@ -790,7 +790,7 @@ impl<'a> State<'a> {
"pending_buf overflow"
);

if sx >= self.sym_buf.filled().len() {
if sx >= self.sym_buf.len() {
break;
}
}
Expand Down
6 changes: 3 additions & 3 deletions zlib-rs/src/inflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -857,7 +857,7 @@ impl<'a> State<'a> {
}

// this is not quite right. not sure when that matters
let out = self.writer.remaining() + self.writer.filled().len();
let out = self.writer.remaining() + self.writer.len();
let left = self.writer.remaining();

let copy = out - left;
Expand Down Expand Up @@ -1207,7 +1207,7 @@ fn inflate_fast_help(state: &mut State, _start: usize) -> ReturnCode {
bit_reader.drop_bits(op as usize);

// max distance in output
let written = writer.filled().len();
let written = writer.len();

if dist as usize > written {
// copy fropm the window
Expand Down Expand Up @@ -1483,7 +1483,7 @@ pub unsafe fn inflate(stream: &mut InflateStream, flush: Flush) -> ReturnCode {
stream.avail_in = state.bit_reader.bytes_remaining() as u32;
stream.next_in = state.bit_reader.as_ptr() as *mut u8;

stream.avail_out = (state.writer.capacity() - state.writer.filled().len()) as u32;
stream.avail_out = (state.writer.capacity() - state.writer.len()) as u32;
stream.next_out = state.writer.as_mut_ptr() as *mut u8;

stream.adler = state.checksum as u64;
Expand Down
8 changes: 7 additions & 1 deletion zlib-rs/src/read_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl<'a> ReadBuf<'a> {
self.buf.len()
}

/// Returns the length of the filled part of the buffer
#[inline]
pub fn len(&self) -> usize {
self.filled
}

/// Returns true if there are no bytes in this ReadBuf
#[inline]
pub fn is_empty(&self) -> bool {
Expand Down Expand Up @@ -132,7 +138,7 @@ impl<'a> ReadBuf<'a> {
/// Returns a mutable reference to the entire buffer, without ensuring that it has been fully
/// initialized.
///
/// The elements between 0 and `self.filled().len()` are filled, and those between 0 and
/// The elements between 0 and `self.len()` are filled, and those between 0 and
/// `self.initialized().len()` are initialized (and so can be converted to a `&mut [u8]`).
///
/// The caller of this method must ensure that these invariants are upheld. For example, if the
Expand Down

0 comments on commit 7b09ffd

Please sign in to comment.