diff --git a/src/serde/de.rs b/src/serde/de.rs index 7634c9fc..2d10afe1 100644 --- a/src/serde/de.rs +++ b/src/serde/de.rs @@ -19,12 +19,8 @@ pub fn node_from_stream(allocator: &mut Allocator, f: &mut Cursor<&[u8]>) -> io: let mut ops = vec![ParseOp::SExp]; let mut b = [0; 1]; - loop { - let op = ops.pop(); - if op.is_none() { - break; - } - match op.unwrap() { + while let Some(op) = ops.pop() { + match op { ParseOp::SExp => { f.read_exact(&mut b)?; if b[0] == CONS_BOX_MARKER { diff --git a/src/serde/de_br.rs b/src/serde/de_br.rs index cde718c4..22ce97ff 100644 --- a/src/serde/de_br.rs +++ b/src/serde/de_br.rs @@ -24,12 +24,8 @@ pub fn node_from_stream_backrefs( let mut ops = vec![ParseOp::SExp]; let mut b = [0; 1]; - loop { - let op = ops.pop(); - if op.is_none() { - break; - } - match op.unwrap() { + while let Some(op) = ops.pop() { + match op { ParseOp::SExp => { f.read_exact(&mut b)?; if b[0] == CONS_BOX_MARKER { diff --git a/src/serde/de_tree.rs b/src/serde/de_tree.rs index 36291d99..0056d7f9 100644 --- a/src/serde/de_tree.rs +++ b/src/serde/de_tree.rs @@ -125,97 +125,92 @@ pub fn parse_triples( let mut tree_hashes = Vec::new(); let mut op_stack = vec![ParseOpRef::ParseObj]; let mut cursor: u64 = 0; - loop { - match op_stack.pop() { - None => { - break; - } - Some(op) => match op { - ParseOpRef::ParseObj => { - let mut b: [u8; 1] = [0]; - f.read_exact(&mut b)?; - let start = cursor; - cursor += 1; - let b = b[0]; - if b == CONS_BOX_MARKER { - let index = r.len(); - let new_obj = ParsedTriple::Pair { - start, - end: 0, - right_index: 0, - }; - r.push(new_obj); - if calculate_tree_hashes { - tree_hashes.push([0; 32]) - } - op_stack.push(ParseOpRef::SaveEnd(index)); - op_stack.push(ParseOpRef::ParseObj); - op_stack.push(ParseOpRef::SaveRightIndex(index)); - op_stack.push(ParseOpRef::ParseObj); - } else { - let (start, end, atom_offset, tree_hash) = { - if b <= MAX_SINGLE_BYTE { - ( - start, - start + 1, - 0, - tree_hash_for_byte(b, calculate_tree_hashes), - ) - } else { - let (atom_offset, atom_size) = decode_size_with_offset(f, b)?; - let end = start + (atom_offset as u64) + atom_size; - let h = skip_or_sha_bytes(f, atom_size, calculate_tree_hashes)?; - (start, end, atom_offset as u32, h) - } - }; - if calculate_tree_hashes { - tree_hashes.push(tree_hash.expect("failed unwrap")) + while let Some(op) = op_stack.pop() { + match op { + ParseOpRef::ParseObj => { + let mut b: [u8; 1] = [0]; + f.read_exact(&mut b)?; + let start = cursor; + cursor += 1; + let b = b[0]; + if b == CONS_BOX_MARKER { + let index = r.len(); + let new_obj = ParsedTriple::Pair { + start, + end: 0, + right_index: 0, + }; + r.push(new_obj); + if calculate_tree_hashes { + tree_hashes.push([0; 32]) + } + op_stack.push(ParseOpRef::SaveEnd(index)); + op_stack.push(ParseOpRef::ParseObj); + op_stack.push(ParseOpRef::SaveRightIndex(index)); + op_stack.push(ParseOpRef::ParseObj); + } else { + let (start, end, atom_offset, tree_hash) = { + if b <= MAX_SINGLE_BYTE { + ( + start, + start + 1, + 0, + tree_hash_for_byte(b, calculate_tree_hashes), + ) + } else { + let (atom_offset, atom_size) = decode_size_with_offset(f, b)?; + let end = start + (atom_offset as u64) + atom_size; + let h = skip_or_sha_bytes(f, atom_size, calculate_tree_hashes)?; + (start, end, atom_offset as u32, h) } - let new_obj = ParsedTriple::Atom { - start, - end, - atom_offset, - }; - cursor = end; - r.push(new_obj); + }; + if calculate_tree_hashes { + tree_hashes.push(tree_hash.expect("failed unwrap")) + } + let new_obj = ParsedTriple::Atom { + start, + end, + atom_offset, + }; + cursor = end; + r.push(new_obj); + } + } + ParseOpRef::SaveEnd(index) => match &mut r[index] { + ParsedTriple::Pair { + start: _, + end, + right_index, + } => { + if calculate_tree_hashes { + let h = sha_blobs(&[ + &[2], + &tree_hashes[index + 1], + &tree_hashes[*right_index as usize], + ]); + tree_hashes[index] = h; } + *end = cursor; + } + _ => { + panic!("internal error: SaveEnd") } - ParseOpRef::SaveEnd(index) => match &mut r[index] { + }, + ParseOpRef::SaveRightIndex(index) => { + let new_index = r.len() as u32; + match &mut r[index] { ParsedTriple::Pair { start: _, - end, + end: _, right_index, } => { - if calculate_tree_hashes { - let h = sha_blobs(&[ - &[2], - &tree_hashes[index + 1], - &tree_hashes[*right_index as usize], - ]); - tree_hashes[index] = h; - } - *end = cursor; + *right_index = new_index; } _ => { - panic!("internal error: SaveEnd") - } - }, - ParseOpRef::SaveRightIndex(index) => { - let new_index = r.len() as u32; - match &mut r[index] { - ParsedTriple::Pair { - start: _, - end: _, - right_index, - } => { - *right_index = new_index; - } - _ => { - panic!("internal error: SaveRightIndex") - } + panic!("internal error: SaveRightIndex") } } - }, + } } } Ok(( diff --git a/src/serde/object_cache.rs b/src/serde/object_cache.rs index 0ee3bd5a..ed479259 100644 --- a/src/serde/object_cache.rs +++ b/src/serde/object_cache.rs @@ -78,30 +78,23 @@ impl<'a, T: Clone> ObjectCache<'a, T> { /// as necessary fn calculate(&mut self, root_node: &NodePtr) { let mut obj_list = vec![*root_node]; - loop { - match obj_list.pop() { - None => { - return; - } - Some(node) => { - let v = self.get_from_cache(&node); - match v { - Some(_) => {} - None => match (self.f)(self, self.allocator, node) { - None => match self.allocator.sexp(node) { - SExp::Pair(left, right) => { - obj_list.push(node); - obj_list.push(left); - obj_list.push(right); - } - _ => panic!("f returned `None` for atom"), - }, - Some(v) => { - self.set(&node, v); - } - }, + while let Some(node) = obj_list.pop() { + let v = self.get_from_cache(&node); + match v { + Some(_) => {} + None => match (self.f)(self, self.allocator, node) { + None => match self.allocator.sexp(node) { + SExp::Pair(left, right) => { + obj_list.push(node); + obj_list.push(left); + obj_list.push(right); + } + _ => panic!("f returned `None` for atom"), + }, + Some(v) => { + self.set(&node, v); } - } + }, } } } diff --git a/src/serde/read_cache_lookup.rs b/src/serde/read_cache_lookup.rs index 3a2ca395..3d1ca858 100644 --- a/src/serde/read_cache_lookup.rs +++ b/src/serde/read_cache_lookup.rs @@ -135,10 +135,7 @@ impl ReadCacheLookup { seen_ids.insert(id); let mut partial_paths = vec![(*id, vec![])]; - loop { - if partial_paths.is_empty() { - break; - } + while !partial_paths.is_empty() { let mut new_partial_paths = vec![]; for (node, path) in partial_paths.iter_mut() { if *node == self.root_hash { diff --git a/src/serde/tools.rs b/src/serde/tools.rs index b53a75dd..d2fcad88 100644 --- a/src/serde/tools.rs +++ b/src/serde/tools.rs @@ -74,12 +74,8 @@ pub fn tree_hash_from_stream(f: &mut Cursor<&[u8]>) -> io::Result<[u8; 32]> { let mut ops = vec![ParseOp::SExp]; let mut b = [0; 1]; - loop { - let op = ops.pop(); - if op.is_none() { - break; - } - match op.unwrap() { + while let Some(op) = ops.pop() { + match op { ParseOp::SExp => { f.read_exact(&mut b)?; if b[0] == CONS_BOX_MARKER {