@@ -299,10 +299,7 @@ where
299299 Ok ( ( prefix, node_hash, node) ) => {
300300 // Skip inline nodes, as they cannot contain hash references to other nodes by
301301 // assumption.
302- let node_hash = match node_hash {
303- Some ( node_hash) => node_hash,
304- None => continue ,
305- } ;
302+ let Some ( node_hash) = node_hash else { continue } ;
306303
307304 if let Some ( seen_hashes) = seen_hashes. as_deref_mut ( ) {
308305 // A subtree whose root was already emitted is skipped entirely; the parent's
@@ -428,10 +425,10 @@ impl<'a, C: NodeCodec> DecoderStackEntry<'a, C> {
428425 match child {
429426 NodeHandle :: Inline ( data) if data. is_empty ( ) => return Ok ( false ) ,
430427 _ => {
431- let child_ref = child. try_into ( ) . map_err ( |hash| {
428+ let child_ref: ChildReference < _ > = child. try_into ( ) . map_err ( |hash| {
432429 Box :: new ( TrieError :: InvalidHash ( C :: HashOut :: default ( ) , hash) )
433430 } ) ?;
434- if let ChildReference :: Hash ( _ ) = child_ref {
431+ if child_ref . is_hash ( ) {
435432 self . hash_ref_children |= 1u16 << self . child_index ;
436433 }
437434 self . children [ self . child_index ] = Some ( child_ref) ;
@@ -444,10 +441,11 @@ impl<'a, C: NodeCodec> DecoderStackEntry<'a, C> {
444441 match children[ self . child_index ] {
445442 Some ( NodeHandle :: Inline ( data) ) if data. is_empty ( ) => return Ok ( false ) ,
446443 Some ( child) => {
447- let child_ref = child. try_into ( ) . map_err ( |hash| {
448- Box :: new ( TrieError :: InvalidHash ( C :: HashOut :: default ( ) , hash) )
449- } ) ?;
450- if let ChildReference :: Hash ( _) = child_ref {
444+ let child_ref: ChildReference < _ > =
445+ child. try_into ( ) . map_err ( |hash| {
446+ Box :: new ( TrieError :: InvalidHash ( C :: HashOut :: default ( ) , hash) )
447+ } ) ?;
448+ if child_ref. is_hash ( ) {
451449 self . hash_ref_children |= 1u16 << self . child_index ;
452450 }
453451 self . children [ self . child_index ] = Some ( child_ref) ;
@@ -697,6 +695,9 @@ where
697695/// Decoding work is proportional to the *un-deduplicated* encoding: re-inserting subtrees at every
698696/// occurrence can touch far more positions than there are items in `encoded`, so callers decoding
699697/// untrusted input should bound the logical size, not the encoded size.
698+ ///
699+ /// Returns the root hash of the reconstructed partial trie and the number of items consumed from
700+ /// `encoded`.
700701pub fn decode_compact_from_iter_with_known_items < ' a , L , DB , I > (
701702 db : & mut DB ,
702703 encoded : I ,
@@ -714,12 +715,8 @@ where
714715 // The prefix of the next item to be read from the slice of encoded items.
715716 let mut prefix = NibbleVec :: new ( ) ;
716717
717- // The number of items consumed so far, including attached values.
718- let mut used;
719-
720718 let mut iter = encoded. into_iter ( ) . enumerate ( ) ;
721719 while let Some ( ( i, encoded_node) ) = iter. next ( ) {
722- used = i + 1 ;
723720 let mut attached_node = 0 ;
724721 if let Some ( header) = L :: Codec :: ESCAPE_HEADER {
725722 if encoded_node. starts_with ( & [ header] ) {
@@ -745,8 +742,7 @@ where
745742
746743 if attached_node > 0 {
747744 // Read value
748- if let Some ( ( i, fetched_value) ) = iter. next ( ) {
749- used = i + 1 ;
745+ if let Some ( ( _, fetched_value) ) = iter. next ( ) {
750746 last_entry. attached_value = Some ( fetched_value) ;
751747 // Record immediately: a node deduplicated against this one can complete before
752748 // this node does, e.g. a leaf below a branch carrying the value.
@@ -818,7 +814,7 @@ where
818814 last_entry. children [ last_entry. child_index ] = Some ( ChildReference :: Hash ( node_hash) ) ;
819815 last_entry. child_index += 1 ;
820816 } else {
821- return Ok ( ( node_hash, used ) )
817+ return Ok ( ( node_hash, i + 1 + attached_node ) )
822818 }
823819 }
824820 }
0 commit comments