Skip to content

Commit

Permalink
Use cmp::Ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Aug 9, 2023
1 parent 3437655 commit 5d4a8b2
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions src/container.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Container for reader functions
use core::cmp::Ordering;

use acid_io::{self, Read};
use bitvec::prelude::*;

Expand Down Expand Up @@ -69,47 +71,51 @@ impl<R: Read> Container<R> {
}
let mut ret = BitVec::with_capacity(amt);

if amt == self.leftover.len() {
match amt.cmp(&self.leftover.len()) {
// exact match, just use leftover
ret = self.leftover.clone();
self.leftover.clear();
} else if amt < self.leftover.len() {
Ordering::Equal => {
ret = self.leftover.clone();
self.leftover.clear();
}
// The entire bits we need to return have been already read previously from bytes but
// not all were read, return required leftover bits
let used = self.leftover.split_off(amt);
ret.extend_from_bitslice(&self.leftover);
self.leftover = used;
} else {
// previous read was not enought to satisfy the amt requirement, return all previously
// read bits
ret.extend_from_bitslice(&self.leftover);

// calulcate the amount of bytes we need to read to read enough bits
let bits_left = amt - self.leftover.len();
let mut bytes_len = bits_left / 8;
if (bits_left % 8) != 0 {
bytes_len += 1;
}

// read in new bytes
let mut buf = alloc::vec![0; bytes_len];
if let Err(e) = self.inner.read_exact(&mut buf) {
if e.kind() == acid_io::ErrorKind::UnexpectedEof {
return Err(DekuError::Incomplete(NeedSize::new(amt)));
Ordering::Greater => {
// read bits
ret.extend_from_bitslice(&self.leftover);

// calulcate the amount of bytes we need to read to read enough bits
let bits_left = amt - self.leftover.len();
let mut bytes_len = bits_left / 8;
if (bits_left % 8) != 0 {
bytes_len += 1;
}

// TODO: other errors?
}
#[cfg(feature = "logging")]
log::trace!("read_bits: read() {buf:02x?}");
// read in new bytes
let mut buf = alloc::vec![0; bytes_len];
if let Err(e) = self.inner.read_exact(&mut buf) {
if e.kind() == acid_io::ErrorKind::UnexpectedEof {
return Err(DekuError::Incomplete(NeedSize::new(amt)));
}

// TODO: other errors?
}
#[cfg(feature = "logging")]
log::trace!("read_bits: read() {buf:02x?}");

// create bitslice and remove unused bits
let mut rest: BitVec<u8, Msb0> = BitVec::try_from_slice(&buf).unwrap();
let not_needed = rest.split_off(bits_left);
self.leftover = not_needed;
// create bitslice and remove unused bits
let mut rest: BitVec<u8, Msb0> = BitVec::try_from_slice(&buf).unwrap();
let not_needed = rest.split_off(bits_left);
self.leftover = not_needed;

// create return
ret.extend_from_bitslice(rest.as_bitslice());
// create return
ret.extend_from_bitslice(rest.as_bitslice());
}
// previous read was not enough to satisfy the amt requirement, return all previously
Ordering::Less => {
let used = self.leftover.split_off(amt);
ret.extend_from_bitslice(&self.leftover);
self.leftover = used;
}
}

self.bits_read += ret.len();
Expand Down

0 comments on commit 5d4a8b2

Please sign in to comment.