Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
chalharu committed Dec 27, 2018
1 parent be5efff commit e795266
Show file tree
Hide file tree
Showing 21 changed files with 53 additions and 76 deletions.
6 changes: 3 additions & 3 deletions src/bitset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl FromIterator<bool> for BitArray {
T: IntoIterator<Item = bool>,
{
let mut s = 0;
let mut l = 0;
let mut l: usize = 0;
let it = iter.into_iter();
let mut data = if let (_, Some(sh)) = it.size_hint() {
Vec::with_capacity((sh + 63) >> 6)
Expand All @@ -97,12 +97,12 @@ impl FromIterator<bool> for BitArray {
s |= 1 << (l & 63);
}
l += 1;
if l & 63 == 0 {
if l.trailing_zeros() >= 6 {
data.push(s);
s = 0;
}
}
if l & 63 != 0 {
if l.trailing_zeros() < 6 {
data.push(s);
}
BitArray {
Expand Down
6 changes: 3 additions & 3 deletions src/bzip2/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl BZip2Encoder {
fn next_bits<I: Iterator<Item = u8>>(
&mut self,
iter: &mut I,
action: &Action,
action: Action,
) -> Option<Result<SmallBitVec<u32>, CompressionError>> {
while self.queue.is_empty() {
match iter.next() {
Expand All @@ -84,7 +84,7 @@ impl BZip2Encoder {
self.finished = false;
return None;
} else {
match *action {
match action {
Action::Flush => {
if let Err(e) =
self.inner.flush(&mut self.queue)
Expand Down Expand Up @@ -117,7 +117,7 @@ impl Encoder for BZip2Encoder {
action: &Action,
) -> Option<Result<u8, CompressionError>> {
while self.bitbuflen == 0 {
let s = match self.next_bits(iter, action) {
let s = match self.next_bits(iter, *action) {
Some(Err(e)) => return Some(Err(e)),
Some(Ok(ref s)) => self.writer.write_bits(s),
None => {
Expand Down
10 changes: 5 additions & 5 deletions src/bzip2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mod tests {
fn test_unit() {
setup();
let ret = b"a\n"
.into_iter()
.iter()
.cloned()
.encode(&mut BZip2Encoder::new(9), Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand Down Expand Up @@ -82,7 +82,7 @@ mod tests {

let mut encoder = BZip2Encoder::new(1);
let ret = include_bytes!("../../data/sample1.ref")
.into_iter()
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>()
Expand All @@ -101,7 +101,7 @@ mod tests {

let mut encoder = BZip2Encoder::new(2);
let ret = include_bytes!("../../data/sample2.ref")
.into_iter()
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>()
Expand All @@ -120,7 +120,7 @@ mod tests {

let mut encoder = BZip2Encoder::new(3);
let ret = include_bytes!("../../data/sample3.ref")
.into_iter()
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>()
Expand All @@ -145,7 +145,7 @@ mod tests {
#[test]
fn test_long() {
setup();
let data = b"a".into_iter()
let data = b"a".iter()
.cycle()
.take(1000)
.cloned()
Expand Down
4 changes: 2 additions & 2 deletions src/bzip2/mtf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub(crate) struct MtfPosition {
impl MtfPosition {
pub fn new(count: usize) -> Self {
Self {
data: (0..count).into_iter().collect::<Vec<_>>(),
data: (0..count).collect::<Vec<_>>(),
}
}
pub fn pop(&mut self, value: usize) -> usize {
Expand Down Expand Up @@ -45,7 +45,7 @@ pub(crate) struct MtfPositionDecoder {
impl MtfPositionDecoder {
pub fn new(count: usize) -> Self {
Self {
data: (0..count).into_iter().collect::<Vec<_>>(),
data: (0..count).collect::<Vec<_>>(),
}
}
pub fn pop(&mut self, value: usize) -> usize {
Expand Down
2 changes: 1 addition & 1 deletion src/deflate/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl DeflaterInner {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
];
let mut len_list = vec![0; 19];
for &i in len_index.into_iter().take(hclen as usize) {
for &i in len_index.iter().take(hclen as usize) {
len_list[i] = *try!(
reader
.read_bits(3)
Expand Down
18 changes: 9 additions & 9 deletions src/deflate/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ impl Inflater {
fn next_bits<I: Iterator<Item = u8>>(
&mut self,
iter: &mut I,
action: &Action,
action: Action,
) -> Option<Result<InflateBitVec, CompressionError>> {
while self.queue.is_empty() {
match self.lzss.next(iter, action) {
match self.lzss.next(iter, &action) {
Some(ref s) => {
if let Err(e) = self.inner.next(s, &mut self.queue) {
return Some(Err(e));
Expand All @@ -167,7 +167,7 @@ impl Inflater {
self.finished = false;
return None;
} else {
match *action {
match action {
Action::Flush => {
if let Err(e) =
self.inner.flush(&mut self.queue)
Expand Down Expand Up @@ -201,7 +201,7 @@ impl Encoder for Inflater {
action: &Action,
) -> Option<Result<u8, CompressionError>> {
while self.bitbuflen == 0 {
let s = match self.next_bits(iter, action) {
let s = match self.next_bits(iter, *action) {
Some(Err(e)) => return Some(Err(e)),
Some(Ok(InflateBitVec::BitVec(ref s))) => {
self.writer.write_bits(s)
Expand Down Expand Up @@ -475,7 +475,7 @@ impl InflaterInner {
self.decompress_len as u16 ^ 0xFFFF,
16,
)));
for i in 1..(self.decompress_len + 1) {
for i in 1..=self.decompress_len {
let d = self.nocomp_buf[self.decompress_len - i];
queue.push_back(InflateBitVec::Byte(d));
}
Expand Down Expand Up @@ -652,7 +652,7 @@ mod tests {
#[test]
fn test_empty() {
let mut encoder = Inflater::new();
let ret = [].into_iter()
let ret = [].iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand All @@ -669,7 +669,7 @@ mod tests {
#[test]
fn test_unit() {
let mut encoder = Inflater::new();
let ret = b"a".into_iter()
let ret = b"a".iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand All @@ -688,7 +688,7 @@ mod tests {
fn test_arr() {
let mut encoder = Inflater::new();
let ret = b"aaaaaaaaaaa"
.into_iter()
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand All @@ -712,7 +712,7 @@ mod tests {
fn test_arr2() {
let mut encoder = Inflater::new();
let a = b"aabbaabbaaabbbaaabbbaabbaabb"
.into_iter()
.iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand Down
2 changes: 1 addition & 1 deletion src/deflate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mod tests {
#[test]
fn test_long() {
check(
&(b"a".into_iter()
&(b"a".iter()
.cycle()
.take(260)
.cloned()
Expand Down
4 changes: 2 additions & 2 deletions src/gzip/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ where
let fname_len = if (flg & 0b1000) != 0 {
// NAME
if let Some(l) = (&self.header)
.into_iter()
.iter()
.enumerate()
.skip(10 + xlen)
.skip_while(|x| *x.1 != 0)
Expand All @@ -146,7 +146,7 @@ where
let fcomment_len = if (flg & 0b1_0000) != 0 {
// COMMENT
if let Some(l) = (&self.header)
.into_iter()
.iter()
.enumerate()
.skip(10 + xlen + fname_len)
.skip_while(|x| *x.1 != 0)
Expand Down
2 changes: 1 addition & 1 deletion src/gzip/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ mod tests {
#[test]
fn test_unit() {
let mut encoder = GZipEncoder::new();
let ret = b"a".into_iter()
let ret = b"a".iter()
.cloned()
.encode(&mut encoder, Action::Finish)
.collect::<Result<Vec<_>, _>>();
Expand Down
2 changes: 1 addition & 1 deletion src/gzip/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ mod tests {
#[test]
fn test_long() {
check(
&(b"a".into_iter()
&(b"a".iter()
.cycle()
.take(260)
.cloned()
Expand Down
2 changes: 1 addition & 1 deletion src/huffman/cano_huff_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub fn make_tab_with_fn<F: Fn(usize, usize) -> usize>(
if freq.is_empty() {
Vec::new()
} else {
let (s, l): (Vec<_>, Vec<_>) = freq.into_iter()
let (s, l): (Vec<_>, Vec<_>) = freq.iter()
.enumerate()
.filter_map(|(i, &t)| if t != 0 { Some((i, t)) } else { None })
.unzip();
Expand Down
2 changes: 1 addition & 1 deletion src/huffman/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ enum SymbolTableItem {
impl<D: Direction> HuffmanDecoder<D> {
pub fn new(symb_len: &[u8], mut stab_bits: usize) -> Result<Self, String> {
let max_len = symb_len
.into_iter()
.iter()
.cloned()
.max()
.unwrap_or_else(|| 0) as usize;
Expand Down
2 changes: 1 addition & 1 deletion src/huffman/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ mod tests {

assert_eq!(
b"abcdefgh"
.into_iter()
.iter()
.map(|x| x - 0x60)
.map(|x| hencoder.enc(&x).unwrap())
.collect::<Vec<_>>(),
Expand Down
4 changes: 2 additions & 2 deletions src/huffman/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ where
SmallBitVec<T>: SmallBitVecReverse,
{
let symbs = symb_len
.into_iter()
.iter()
.enumerate()
.filter(|&(_, &t)| t != 0)
.collect::<Vec<_>>();
Expand Down Expand Up @@ -93,7 +93,7 @@ mod tests {

let mut writer = BitWriter::<D>::new();
let vec = testarray
.into_iter()
.iter()
.map(|c| hencoder.enc(c).unwrap())
.to_bytes(&mut writer, Action::Flush)
.collect::<Vec<_>>();
Expand Down
4 changes: 1 addition & 3 deletions src/lzss/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,8 @@ mod tests {
fn test() {
let testvec = b"aabbaabbaaabbbaaabbbaabbaabb";
let mut encoder = LzssEncoder::new(comparison, 0x1_0000, 256, 3, 3);
let mut iter = testvec.into_iter().cloned();
let mut iter = testvec.iter().cloned();
let enc_ret = (0..)
.into_iter()
.scan((), |_, _| {
encoder.next(&mut iter, &Action::Flush)
})
Expand All @@ -80,7 +79,6 @@ mod tests {
let mut decoder = LzssDecoder::new(0x1_0000);
let mut dec_iter = enc_ret.into_iter().map::<Result<_, ()>, _>(Ok);
let ret = (0..)
.into_iter()
.scan((), |_, _| decoder.next(&mut dec_iter).unwrap())
.collect::<Vec<_>>();

Expand Down
Loading

0 comments on commit e795266

Please sign in to comment.