Coverage Report

Created: 2025-07-01 09:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/__w/smoldot/smoldot/repo/lib/src/trie/trie_structure.rs
Line
Count
Source
1
// Smoldot
2
// Copyright (C) 2019-2022  Parity Technologies (UK) Ltd.
3
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
4
5
// This program is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
10
// This program is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
15
// You should have received a copy of the GNU General Public License
16
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18
//! Manages the structure of a trie. Allows inserting and removing nodes, but does not store any
19
//! value. Only the structure is stored.
20
//!
21
//! See the [`TrieStructure`] struct.
22
23
// TODO: the API of `TrieStructure` is rather wonky and could be simplified
24
25
use super::nibble::{Nibble, bytes_to_nibbles};
26
27
use alloc::{borrow::ToOwned as _, vec, vec::Vec};
28
use core::{cmp, fmt, iter, mem, ops};
29
use either::Either;
30
use slab::Slab;
31
32
mod tests;
33
34
/// Stores the structure of a trie, including branch nodes that have no storage value.
35
///
36
/// The `TUd` parameter is a user data stored in each node.
37
///
38
/// This struct doesn't represent a complete trie. It only manages the structure of the trie, and
39
/// the storage values have to be maintained in parallel of this.
40
#[derive(Clone)]
41
pub struct TrieStructure<TUd> {
42
    /// List of nodes. Using a [`Slab`] guarantees that the node indices never change.
43
    nodes: Slab<Node<TUd>>,
44
    /// Index of the root node within [`TrieStructure::nodes`]. `None` if the trie is empty.
45
    root_index: Option<usize>,
46
}
47
48
/// Entry in the structure.
49
#[derive(Debug, Clone)]
50
struct Node<TUd> {
51
    /// Index of the parent within [`TrieStructure::nodes`]. `None` if this is the root.
52
    parent: Option<(usize, Nibble)>,
53
    /// Partial key of the node. Portion to add to the values in `parent` to obtain the full key.
54
    partial_key: Vec<Nibble>,
55
    /// Indices of the children within [`TrieStructure::nodes`].
56
    children: [Option<usize>; 16],
57
    /// If true, this node is a so-called "storage node" with a storage value associated to it. If
58
    /// false, then it is a so-called "branch node". Branch nodes are automatically removed from
59
    /// the trie if their number of children is inferior to 2.
60
    has_storage_value: bool,
61
    /// User data associated to the node.
62
    user_data: TUd,
63
}
64
65
impl<TUd> TrieStructure<TUd> {
66
    /// Builds a new empty trie.
67
    ///
68
    /// Equivalent to calling [`TrieStructure::with_capacity`] with a capacity of 0.
69
    ///
70
    /// # Examples
71
    ///
72
    /// ```
73
    /// use smoldot::trie::trie_structure;
74
    ///
75
    /// let trie = trie_structure::TrieStructure::<()>::new();
76
    /// assert!(trie.is_empty());
77
    /// assert_eq!(trie.capacity(), 0);
78
    /// ```
79
45.5k
    pub fn new() -> Self {
80
45.5k
        TrieStructure {
81
45.5k
            nodes: Slab::new(),
82
45.5k
            root_index: None,
83
45.5k
        }
84
45.5k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE3newB6_
Line
Count
Source
79
32.7k
    pub fn new() -> Self {
80
32.7k
        TrieStructure {
81
32.7k
            nodes: Slab::new(),
82
32.7k
            root_index: None,
83
32.7k
        }
84
32.7k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE3newB6_
Line
Count
Source
79
1.02k
    pub fn new() -> Self {
80
1.02k
        TrieStructure {
81
1.02k
            nodes: Slab::new(),
82
1.02k
            root_index: None,
83
1.02k
        }
84
1.02k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE3newB6_
Line
Count
Source
79
11.7k
    pub fn new() -> Self {
80
11.7k
        TrieStructure {
81
11.7k
            nodes: Slab::new(),
82
11.7k
            root_index: None,
83
11.7k
        }
84
11.7k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE3newCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE3newB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE3newCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
79
2
    pub fn new() -> Self {
80
2
        TrieStructure {
81
2
            nodes: Slab::new(),
82
2
            root_index: None,
83
2
        }
84
2
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE3newCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
79
19
    pub fn new() -> Self {
80
19
        TrieStructure {
81
19
            nodes: Slab::new(),
82
19
            root_index: None,
83
19
        }
84
19
    }
85
86
    /// Builds a new empty trie with a capacity for the given number of nodes.
87
    ///
88
    /// # Examples
89
    ///
90
    /// ```
91
    /// use smoldot::trie::trie_structure;
92
    ///
93
    /// let trie = trie_structure::TrieStructure::<()>::with_capacity(12);
94
    /// assert!(trie.is_empty());
95
    /// assert_eq!(trie.capacity(), 12);
96
    /// ```
97
1.50k
    pub fn with_capacity(capacity: usize) -> Self {
98
1.50k
        TrieStructure {
99
1.50k
            nodes: Slab::with_capacity(capacity),
100
1.50k
            root_index: None,
101
1.50k
        }
102
1.50k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13with_capacityB6_
Line
Count
Source
97
1.50k
    pub fn with_capacity(capacity: usize) -> Self {
98
1.50k
        TrieStructure {
99
1.50k
            nodes: Slab::with_capacity(capacity),
100
1.50k
            root_index: None,
101
1.50k
        }
102
1.50k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13with_capacityB6_
103
104
    /// Returns the number of nodes (storage or branch nodes) the trie can hold without
105
    /// reallocating.
106
    ///
107
    /// # Examples
108
    ///
109
    /// ```
110
    /// use smoldot::trie::trie_structure;
111
    ///
112
    /// let trie = trie_structure::TrieStructure::<()>::with_capacity(7);
113
    /// assert_eq!(trie.capacity(), 7);
114
    /// ```
115
0
    pub fn capacity(&self) -> usize {
116
0
        self.nodes.capacity()
117
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructurepE8capacityB6_
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE8capacityB6_
118
119
    /// Returns `true` if the trie doesn't contain any node.
120
    ///
121
    /// Equivalent to [`TrieStructure::len`] returning 0.
122
    ///
123
    /// # Examples
124
    ///
125
    /// ```
126
    /// use smoldot::trie::{self, trie_structure};
127
    ///
128
    /// let mut trie = trie_structure::TrieStructure::new();
129
    /// assert!(trie.is_empty());
130
    ///
131
    /// // Insert a node.
132
    /// trie
133
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
134
    ///     .into_vacant()
135
    ///     .unwrap()
136
    ///     .insert_storage_value()
137
    ///     .insert((), ());
138
    /// assert!(!trie.is_empty());
139
    ///
140
    /// // Remove the newly-inserted node.
141
    /// trie
142
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
143
    ///     .into_occupied()
144
    ///     .unwrap()
145
    ///     .into_storage()
146
    ///     .unwrap()
147
    ///     .remove();
148
    /// assert!(trie.is_empty());
149
    /// ```
150
1
    pub fn is_empty(&self) -> bool {
151
1
        self.nodes.is_empty()
152
1
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE8is_emptyB6_
Line
Count
Source
150
1
    pub fn is_empty(&self) -> bool {
151
1
        self.nodes.is_empty()
152
1
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE8is_emptyB6_
153
154
    /// Returns the number of nodes, both branch and storage nodes, in the trie structure.
155
4
    pub fn len(&self) -> usize {
156
4
        self.nodes.len()
157
4
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE3lenB6_
Line
Count
Source
155
4
    pub fn len(&self) -> usize {
156
4
        self.nodes.len()
157
4
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE3lenB6_
158
159
    /// Reduces the capacity of the trie as much as possible.
160
    ///
161
    /// See [`Vec::shrink_to_fit`].
162
0
    pub fn shrink_to_fit(&mut self) {
163
0
        self.nodes.shrink_to_fit();
164
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructurepE13shrink_to_fitB6_
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE13shrink_to_fitB6_
165
166
    /// Returns a list of all nodes in the structure, without any specific order.
167
8.14k
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
317k
        
self.nodes8.14k
.
iter8.14k
().
map8.14k
(|(k, _)| NodeIndex(k))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE14iter_unordered0B8_
Line
Count
Source
168
29.2k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE14iter_unordered0B8_
Line
Count
Source
168
2.65k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureuE14iter_unordered0B8_
Line
Count
Source
168
284k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE14iter_unordered0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE14iter_unordered0B8_
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE14iter_unordered0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
168
96
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE14iter_unordered0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
168
912
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
8.14k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE14iter_unorderedB6_
Line
Count
Source
167
1.50k
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
1.50k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
1.50k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14iter_unorderedB6_
Line
Count
Source
167
1.02k
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
1.02k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
1.02k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE14iter_unorderedB6_
Line
Count
Source
167
5.59k
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
5.59k
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
5.59k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14iter_unorderedCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE14iter_unorderedB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14iter_unorderedCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
167
2
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
2
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
2
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14iter_unorderedCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
167
19
    pub fn iter_unordered(&self) -> impl Iterator<Item = NodeIndex> {
168
19
        self.nodes.iter().map(|(k, _)| NodeIndex(k))
169
19
    }
170
171
    /// Returns a list of all nodes in the structure in lexicographic order of keys.
172
3.32M
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
3.32M
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
3.32M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12iter_orderedB6_
Line
Count
Source
172
131k
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
131k
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
131k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12iter_orderedB6_
Line
Count
Source
172
2.09M
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
2.09M
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
2.09M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE12iter_orderedB6_
Line
Count
Source
172
1.09M
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
1.09M
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
1.09M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12iter_orderedCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE12iter_orderedB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12iter_orderedCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
172
2
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
2
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
2
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12iter_orderedCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
172
19
    pub fn iter_ordered(&self) -> impl Iterator<Item = NodeIndex> {
173
19
        self.all_node_lexicographic_ordered().map(NodeIndex)
174
19
    }
175
176
    /// Returns the root node of the trie, or `None` if the trie is empty.
177
    ///
178
    /// # Examples
179
    ///
180
    /// ```
181
    /// use smoldot::trie::{self, trie_structure};
182
    ///
183
    /// let mut trie = trie_structure::TrieStructure::new();
184
    /// assert!(trie.root_node().is_none());
185
    ///
186
    /// // Insert a node. It becomes the root.
187
    /// trie
188
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
189
    ///     .into_vacant()
190
    ///     .unwrap()
191
    ///     .insert_storage_value()
192
    ///     .insert((), ());
193
    ///
194
    /// assert!(trie.root_node().is_some());
195
    /// assert!(trie.root_node().unwrap().parent().is_none());
196
    /// ```
197
3.00k
    pub fn root_node(&mut self) -> Option<NodeAccess<TUd>> {
198
3.00k
        Some(self.
node_by_index_inner3.00k
(self.root_index
?1
).
unwrap3.00k
())
199
3.00k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE9root_nodeB6_
Line
Count
Source
197
3.00k
    pub fn root_node(&mut self) -> Option<NodeAccess<TUd>> {
198
3.00k
        Some(self.
node_by_index_inner3.00k
(self.root_index
?1
).
unwrap3.00k
())
199
3.00k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE9root_nodeB6_
200
201
    /// Returns the user data associated with the root node of the trie, or `None` if the trie
202
    /// is empty.
203
    // TODO: this function exists only because `root_node` mutably borrows
204
35.2k
    pub fn root_user_data(&self) -> Option<&TUd> {
205
35.2k
        Some(&self.nodes[self.root_index
?3
].user_data)
206
35.2k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE14root_user_dataB6_
Line
Count
Source
204
1.50k
    pub fn root_user_data(&self) -> Option<&TUd> {
205
1.50k
        Some(&self.nodes[self.root_index
?0
].user_data)
206
1.50k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14root_user_dataB6_
Line
Count
Source
204
32.7k
    pub fn root_user_data(&self) -> Option<&TUd> {
205
32.7k
        Some(&self.nodes[self.root_index
?3
].user_data)
206
32.7k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE14root_user_dataB6_
Line
Count
Source
204
1.02k
    pub fn root_user_data(&self) -> Option<&TUd> {
205
1.02k
        Some(&self.nodes[self.root_index
?0
].user_data)
206
1.02k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE14root_user_dataB6_
207
208
    /// Returns an [`Entry`] corresponding to the node whose key is the concatenation of the list
209
    /// of nibbles passed as parameter.
210
    ///
211
    /// # Examples
212
    ///
213
    /// ```
214
    /// use smoldot::trie::{self, trie_structure};
215
    ///
216
    /// let mut trie = trie_structure::TrieStructure::new();
217
    ///
218
    /// let node: trie_structure::Entry<_, _> = trie
219
    ///     .node(trie::bytes_to_nibbles(b"ful".iter().cloned()));
220
    ///
221
    /// match node {
222
    ///     // `Occupied` is returned if a node with this key exists.
223
    ///     trie_structure::Entry::Occupied(_) => unreachable!(),
224
    ///
225
    ///     // `Vacant` is returned if no node with this key exists yet.
226
    ///     // In this example, a node is inserted.
227
    ///     trie_structure::Entry::Vacant(entry) => {
228
    ///         entry.insert_storage_value().insert((), ())
229
    ///     },
230
    /// };
231
    ///
232
    /// // The same node can for example be queried again.
233
    /// // This time, it will be in the `Occupied` state.
234
    /// match trie.node(trie::bytes_to_nibbles(b"ful".iter().cloned())) {
235
    ///     // `NodeAccess::Storage` is used if the node has been explicitly inserted.
236
    ///     trie_structure::Entry::Occupied(trie_structure::NodeAccess::Storage(_)) => {},
237
    ///
238
    ///     // `Branch` would be returned if this was a branch node. See below.
239
    ///     trie_structure::Entry::Occupied(trie_structure::NodeAccess::Branch(_)) => {
240
    ///         unreachable!()
241
    ///     },
242
    ///     trie_structure::Entry::Vacant(e) => unreachable!(),
243
    /// };
244
    ///
245
    /// // In order to demonstrate branch nodes, let's insert a node at the key `fez`.
246
    /// trie
247
    ///     .node(trie::bytes_to_nibbles(b"fez".iter().cloned()))
248
    ///     .into_vacant()
249
    ///     .unwrap()
250
    ///     .insert_storage_value()
251
    ///     .insert((), ());
252
    ///
253
    /// // The trie now contains not two but three nodes. A branch node whose key is `f` has
254
    /// // automatically been inserted as the parent of both `ful` and `fez`.
255
    /// assert_eq!(trie.len(), 3);
256
    /// match trie.node(trie::bytes_to_nibbles(b"f".iter().cloned())) {
257
    ///     trie_structure::Entry::Occupied(trie_structure::NodeAccess::Branch(_)) => {},
258
    ///     _ => unreachable!(),
259
    /// };
260
    /// ```
261
2.15M
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
2.15M
    where
263
2.15M
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
2.15M
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
332k
                node_index,
268
                has_storage_value: true,
269
332k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
332k
                trie: self,
271
332k
                node_index,
272
332k
            })),
273
            ExistingNodeInnerResult::Found {
274
166k
                node_index,
275
                has_storage_value: false,
276
166k
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
166k
                trie: self,
278
166k
                node_index,
279
166k
            })),
280
1.66M
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
1.66M
                trie: self,
282
1.66M
                key,
283
1.66M
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
2.15M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB5_12proof_encode4NodeEE4nodeINtNtNtNtB1e_4iter8adapters6copied6CopiedINtNtNtB1e_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
261
56.9k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
56.9k
    where
263
56.9k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
56.9k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
27.7k
                node_index,
268
                has_storage_value: true,
269
27.7k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
27.7k
                trie: self,
271
27.7k
                node_index,
272
27.7k
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
29.2k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
29.2k
                trie: self,
282
29.2k
                key,
283
29.2k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
56.9k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEEB7_
Line
Count
Source
261
3.06k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
3.06k
    where
263
3.06k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
3.06k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
492
                node_index,
268
                has_storage_value: true,
269
492
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
492
                trie: self,
271
492
                node_index,
272
492
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
2.57k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
2.57k
                trie: self,
282
2.57k
                key,
283
2.57k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
3.06k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB1Q_9into_iter8IntoIterNtNtB5_6nibble6NibbleEEB7_
Line
Count
Source
261
368k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
368k
    where
263
368k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
368k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
65.0k
                node_index,
268
                has_storage_value: true,
269
65.0k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
65.0k
                trie: self,
271
65.0k
                node_index,
272
65.0k
            })),
273
            ExistingNodeInnerResult::Found {
274
1.49k
                node_index,
275
                has_storage_value: false,
276
1.49k
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
1.49k
                trie: self,
278
1.49k
                node_index,
279
1.49k
            })),
280
301k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
301k
                trie: self,
282
301k
                key,
283
301k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
368k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEEB7_
Line
Count
Source
261
591k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
591k
    where
263
591k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
591k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
188k
                node_index,
268
                has_storage_value: true,
269
188k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
188k
                trie: self,
271
188k
                node_index,
272
188k
            })),
273
            ExistingNodeInnerResult::Found {
274
66.6k
                node_index,
275
                has_storage_value: false,
276
66.6k
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
66.6k
                trie: self,
278
66.6k
                node_index,
279
66.6k
            })),
280
336k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
336k
                trie: self,
282
336k
                key,
283
336k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
591k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1U_5slice4iter4IterhEEEEB7_
Line
Count
Source
261
6.22k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
6.22k
    where
263
6.22k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
6.22k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
1.03k
                node_index,
268
                has_storage_value: true,
269
1.03k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
1.03k
                trie: self,
271
1.03k
                node_index,
272
1.03k
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
5.18k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
5.18k
                trie: self,
282
5.18k
                key,
283
5.18k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
6.22k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE4nodeINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB5_6nibble6NibbleEEB7_
Line
Count
Source
261
890k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
890k
    where
263
890k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
890k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
49.6k
                node_index,
268
                has_storage_value: true,
269
49.6k
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
49.6k
                trie: self,
271
49.6k
                node_index,
272
49.6k
            })),
273
            ExistingNodeInnerResult::Found {
274
98.4k
                node_index,
275
                has_storage_value: false,
276
98.4k
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
98.4k
                trie: self,
278
98.4k
                node_index,
279
98.4k
            })),
280
742k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
742k
                trie: self,
282
742k
                key,
283
742k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
890k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE4nodeINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1p_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
261
36
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
36
    where
263
36
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
36
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
5
                node_index,
268
                has_storage_value: true,
269
5
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
5
                trie: self,
271
5
                node_index,
272
5
            })),
273
            ExistingNodeInnerResult::Found {
274
1
                node_index,
275
                has_storage_value: false,
276
1
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
1
                trie: self,
278
1
                node_index,
279
1
            })),
280
30
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
30
                trie: self,
282
30
                key,
283
30
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
36
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE4nodeINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1p_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
261
241k
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
241k
    where
263
241k
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
241k
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
0
                node_index,
268
                has_storage_value: true,
269
0
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
0
                trie: self,
271
0
                node_index,
272
0
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
241k
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
241k
                trie: self,
282
241k
                key,
283
241k
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
241k
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECscoAnRPySggw_6author
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB5_12proof_encode4NodeEE4nodeINtNtNtNtB1e_4iter8adapters6copied6CopiedINtNtNtB1e_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
_RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
261
70
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
70
    where
263
70
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
70
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
0
                node_index,
268
                has_storage_value: true,
269
0
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
0
                trie: self,
271
0
                node_index,
272
0
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
70
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
70
                trie: self,
282
70
                key,
283
70
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
70
    }
_RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE4nodeINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
261
665
    pub fn node<TKIter>(&mut self, key: TKIter) -> Entry<TUd, TKIter>
262
665
    where
263
665
        TKIter: Iterator<Item = Nibble> + Clone,
264
    {
265
665
        match self.existing_node_inner(key.clone()) {
266
            ExistingNodeInnerResult::Found {
267
0
                node_index,
268
                has_storage_value: true,
269
0
            } => Entry::Occupied(NodeAccess::Storage(StorageNodeAccess {
270
0
                trie: self,
271
0
                node_index,
272
0
            })),
273
            ExistingNodeInnerResult::Found {
274
0
                node_index,
275
                has_storage_value: false,
276
0
            } => Entry::Occupied(NodeAccess::Branch(BranchNodeAccess {
277
0
                trie: self,
278
0
                node_index,
279
0
            })),
280
665
            ExistingNodeInnerResult::NotFound { closest_ancestor } => Entry::Vacant(Vacant {
281
665
                trie: self,
282
665
                key,
283
665
                closest_ancestor: closest_ancestor.map(|(i, _)| i),
284
            }),
285
        }
286
665
    }
287
288
    /// Returns the [`NodeIndex`] of the node with the given full key, if any is found.
289
1.04M
    pub fn node_by_full_key<TKIter>(&self, key: TKIter) -> Option<NodeIndex>
290
1.04M
    where
291
1.04M
        TKIter: Iterator<Item = Nibble> + Clone,
292
    {
293
1.04M
        match self.existing_node_inner(key) {
294
209k
            ExistingNodeInnerResult::Found { node_index, .. } => Some(NodeIndex(node_index)),
295
838k
            ExistingNodeInnerResult::NotFound { .. } => None,
296
        }
297
1.04M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE16node_by_full_keyINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEEB7_
Line
Count
Source
289
1.04M
    pub fn node_by_full_key<TKIter>(&self, key: TKIter) -> Option<NodeIndex>
290
1.04M
    where
291
1.04M
        TKIter: Iterator<Item = Nibble> + Clone,
292
    {
293
1.04M
        match self.existing_node_inner(key) {
294
209k
            ExistingNodeInnerResult::Found { node_index, .. } => Some(NodeIndex(node_index)),
295
838k
            ExistingNodeInnerResult::NotFound { .. } => None,
296
        }
297
1.04M
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE16node_by_full_keypEB7_
298
299
    /// Returns `true` if the node with the given index is a storage node. Returns `false` if it
300
    /// is a branch node.
301
    ///
302
    /// # Panic
303
    ///
304
    /// Panics if the [`NodeIndex`] is invalid.
305
    ///
306
2.58M
    pub fn is_storage(&self, node: NodeIndex) -> bool {
307
2.58M
        self.nodes[node.0].has_storage_value
308
2.58M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE10is_storageB6_
Line
Count
Source
306
2.58M
    pub fn is_storage(&self, node: NodeIndex) -> bool {
307
2.58M
        self.nodes[node.0].has_storage_value
308
2.58M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE10is_storageB6_
309
310
    /// Returns the node with the given key, or `None` if no such node exists.
311
    ///
312
    /// This method is a shortcut for calling [`TrieStructure::node`] followed with
313
    /// [`Entry::into_occupied`].
314
0
    pub fn existing_node(
315
0
        &mut self,
316
0
        key: impl Iterator<Item = Nibble> + Clone,
317
0
    ) -> Option<NodeAccess<TUd>> {
318
        if let ExistingNodeInnerResult::Found {
319
0
            node_index,
320
0
            has_storage_value,
321
0
        } = self.existing_node_inner(key)
322
        {
323
0
            Some(if has_storage_value {
324
0
                NodeAccess::Storage(StorageNodeAccess {
325
0
                    trie: self,
326
0
                    node_index,
327
0
                })
328
            } else {
329
0
                NodeAccess::Branch(BranchNodeAccess {
330
0
                    trie: self,
331
0
                    node_index,
332
0
                })
333
            })
334
        } else {
335
0
            None
336
        }
337
0
    }
Unexecuted instantiation: _RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructurepE13existing_nodepEB7_
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE13existing_nodepEB7_
338
339
    /// Inner implementation of [`TrieStructure::existing_node`]. Traverses the tree, trying to
340
    /// find a node whose key is `key`.
341
3.21M
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
3.21M
        &self,
343
3.21M
        mut key: I,
344
3.21M
    ) -> ExistingNodeInnerResult<I> {
345
3.21M
        let 
mut current_index3.16M
= match self.root_index {
346
3.16M
            Some(ri) => ri,
347
            None => {
348
53.8k
                return ExistingNodeInnerResult::NotFound {
349
53.8k
                    closest_ancestor: None,
350
53.8k
                };
351
            }
352
        };
353
3.16M
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
3.16M
        let mut closest_ancestor = None;
356
357
        loop {
358
6.00M
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
6.00M
            for 
nibble2.18M
in current.partial_key.iter().cloned() {
363
2.18M
                if key.next() != Some(nibble) {
364
452k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
1.73M
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
5.55M
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
5.55M
            let 
child_index4.83M
= match key.next() {
374
4.83M
                Some(n) => n,
375
                None => {
376
715k
                    return ExistingNodeInnerResult::Found {
377
715k
                        node_index: current_index,
378
715k
                        has_storage_value: current.has_storage_value,
379
715k
                    };
380
                }
381
            };
382
383
4.83M
            if let Some(
next_index2.84M
) = current.children[usize::from(u8::from(child_index))] {
384
2.84M
                current_index = next_index;
385
2.84M
            } else {
386
1.99M
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
3.21M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB5_12proof_encode4NodeEE19existing_node_innerINtNtNtNtB1e_4iter8adapters6copied6CopiedINtNtNtB1e_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
341
56.9k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
56.9k
        &self,
343
56.9k
        mut key: I,
344
56.9k
    ) -> ExistingNodeInnerResult<I> {
345
56.9k
        let 
mut current_index55.4k
= match self.root_index {
346
55.4k
            Some(ri) => ri,
347
            None => {
348
1.50k
                return ExistingNodeInnerResult::NotFound {
349
1.50k
                    closest_ancestor: None,
350
1.50k
                };
351
            }
352
        };
353
55.4k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
55.4k
        let mut closest_ancestor = None;
356
357
        loop {
358
84.5k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
84.5k
            for 
nibble9.31k
in current.partial_key.iter().cloned() {
363
9.31k
                if key.next() != Some(nibble) {
364
7.30k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
2.01k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
77.2k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
77.2k
            let 
child_index49.5k
= match key.next() {
374
49.5k
                Some(n) => n,
375
                None => {
376
27.7k
                    return ExistingNodeInnerResult::Found {
377
27.7k
                        node_index: current_index,
378
27.7k
                        has_storage_value: current.has_storage_value,
379
27.7k
                    };
380
                }
381
            };
382
383
49.5k
            if let Some(
next_index29.1k
) = current.children[usize::from(u8::from(child_index))] {
384
29.1k
                current_index = next_index;
385
29.1k
            } else {
386
20.4k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
56.9k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEEB7_
Line
Count
Source
341
1.05M
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
1.05M
        &self,
343
1.05M
        mut key: I,
344
1.05M
    ) -> ExistingNodeInnerResult<I> {
345
1.05M
        let 
mut current_index1.05M
= match self.root_index {
346
1.05M
            Some(ri) => ri,
347
            None => {
348
1.02k
                return ExistingNodeInnerResult::NotFound {
349
1.02k
                    closest_ancestor: None,
350
1.02k
                };
351
            }
352
        };
353
1.05M
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
1.05M
        let mut closest_ancestor = None;
356
357
        loop {
358
1.12M
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
1.12M
            for 
nibble74.8k
in current.partial_key.iter().cloned() {
363
74.8k
                if key.next() != Some(nibble) {
364
70.3k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
4.54k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
1.05M
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
1.05M
            let 
child_index845k
= match key.next() {
374
845k
                Some(n) => n,
375
                None => {
376
210k
                    return ExistingNodeInnerResult::Found {
377
210k
                        node_index: current_index,
378
210k
                        has_storage_value: current.has_storage_value,
379
210k
                    };
380
                }
381
            };
382
383
845k
            if let Some(
next_index76.0k
) = current.children[usize::from(u8::from(child_index))] {
384
76.0k
                current_index = next_index;
385
76.0k
            } else {
386
769k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
1.05M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB1Q_9into_iter8IntoIterNtNtB5_6nibble6NibbleEEB7_
Line
Count
Source
341
368k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
368k
        &self,
343
368k
        mut key: I,
344
368k
    ) -> ExistingNodeInnerResult<I> {
345
368k
        let mut current_index = match self.root_index {
346
368k
            Some(ri) => ri,
347
            None => {
348
0
                return ExistingNodeInnerResult::NotFound {
349
0
                    closest_ancestor: None,
350
0
                };
351
            }
352
        };
353
368k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
368k
        let mut closest_ancestor = None;
356
357
        loop {
358
442k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
442k
            for 
nibble134k
in current.partial_key.iter().cloned() {
363
134k
                if key.next() != Some(nibble) {
364
37.5k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
97.1k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
404k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
404k
            let 
child_index338k
= match key.next() {
374
338k
                Some(n) => n,
375
                None => {
376
66.5k
                    return ExistingNodeInnerResult::Found {
377
66.5k
                        node_index: current_index,
378
66.5k
                        has_storage_value: current.has_storage_value,
379
66.5k
                    };
380
                }
381
            };
382
383
338k
            if let Some(
next_index74.2k
) = current.children[usize::from(u8::from(child_index))] {
384
74.2k
                current_index = next_index;
385
74.2k
            } else {
386
264k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
368k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEEB7_
Line
Count
Source
341
591k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
591k
        &self,
343
591k
        mut key: I,
344
591k
    ) -> ExistingNodeInnerResult<I> {
345
591k
        let 
mut current_index552k
= match self.root_index {
346
552k
            Some(ri) => ri,
347
            None => {
348
38.8k
                return ExistingNodeInnerResult::NotFound {
349
38.8k
                    closest_ancestor: None,
350
38.8k
                };
351
            }
352
        };
353
552k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
552k
        let mut closest_ancestor = None;
356
357
        loop {
358
624k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
624k
            for 
nibble75.5k
in current.partial_key.iter().cloned() {
363
75.5k
                if key.next() != Some(nibble) {
364
71.2k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
4.26k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
552k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
552k
            let 
child_index297k
= match key.next() {
374
297k
                Some(n) => n,
375
                None => {
376
254k
                    return ExistingNodeInnerResult::Found {
377
254k
                        node_index: current_index,
378
254k
                        has_storage_value: current.has_storage_value,
379
254k
                    };
380
                }
381
            };
382
383
297k
            if let Some(
next_index71.2k
) = current.children[usize::from(u8::from(child_index))] {
384
71.2k
                current_index = next_index;
385
71.2k
            } else {
386
226k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
591k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB2a_5slice4iter4IterhEEEEB7_
Line
Count
Source
341
6.22k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
6.22k
        &self,
343
6.22k
        mut key: I,
344
6.22k
    ) -> ExistingNodeInnerResult<I> {
345
6.22k
        let 
mut current_index4.17k
= match self.root_index {
346
4.17k
            Some(ri) => ri,
347
            None => {
348
2.04k
                return ExistingNodeInnerResult::NotFound {
349
2.04k
                    closest_ancestor: None,
350
2.04k
                };
351
            }
352
        };
353
4.17k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
4.17k
        let mut closest_ancestor = None;
356
357
        loop {
358
4.31k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
4.31k
            for 
nibble135
in current.partial_key.iter().cloned() {
363
135
                if key.next() != Some(nibble) {
364
130
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
5
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
4.18k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
4.18k
            let 
child_index3.14k
= match key.next() {
374
3.14k
                Some(n) => n,
375
                None => {
376
1.03k
                    return ExistingNodeInnerResult::Found {
377
1.03k
                        node_index: current_index,
378
1.03k
                        has_storage_value: current.has_storage_value,
379
1.03k
                    };
380
                }
381
            };
382
383
3.14k
            if let Some(
next_index133
) = current.children[usize::from(u8::from(child_index))] {
384
133
                current_index = next_index;
385
133
            } else {
386
3.00k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
6.22k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE19existing_node_innerINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB5_6nibble6NibbleEEB7_
Line
Count
Source
341
899k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
899k
        &self,
343
899k
        mut key: I,
344
899k
    ) -> ExistingNodeInnerResult<I> {
345
899k
        let 
mut current_index892k
= match self.root_index {
346
892k
            Some(ri) => ri,
347
            None => {
348
6.23k
                return ExistingNodeInnerResult::NotFound {
349
6.23k
                    closest_ancestor: None,
350
6.23k
                };
351
            }
352
        };
353
892k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
892k
        let mut closest_ancestor = None;
356
357
        loop {
358
3.07M
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
3.07M
            for 
nibble1.61M
in current.partial_key.iter().cloned() {
363
1.61M
                if key.next() != Some(nibble) {
364
251k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
1.36M
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
2.82M
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
2.82M
            let 
child_index2.66M
= match key.next() {
374
2.66M
                Some(n) => n,
375
                None => {
376
155k
                    return ExistingNodeInnerResult::Found {
377
155k
                        node_index: current_index,
378
155k
                        has_storage_value: current.has_storage_value,
379
155k
                    };
380
                }
381
            };
382
383
2.66M
            if let Some(
next_index2.18M
) = current.children[usize::from(u8::from(child_index))] {
384
2.18M
                current_index = next_index;
385
2.18M
            } else {
386
485k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
899k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE19existing_node_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1F_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
341
41
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
41
        &self,
343
41
        mut key: I,
344
41
    ) -> ExistingNodeInnerResult<I> {
345
41
        let 
mut current_index30
= match self.root_index {
346
30
            Some(ri) => ri,
347
            None => {
348
11
                return ExistingNodeInnerResult::NotFound {
349
11
                    closest_ancestor: None,
350
11
                };
351
            }
352
        };
353
30
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
30
        let mut closest_ancestor = None;
356
357
        loop {
358
42
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
68
            for nibble in 
current.partial_key.iter()42
.
cloned42
() {
363
68
                if key.next() != Some(nibble) {
364
10
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
58
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
32
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
32
            let 
child_index23
= match key.next() {
374
23
                Some(n) => n,
375
                None => {
376
9
                    return ExistingNodeInnerResult::Found {
377
9
                        node_index: current_index,
378
9
                        has_storage_value: current.has_storage_value,
379
9
                    };
380
                }
381
            };
382
383
23
            if let Some(
next_index12
) = current.children[usize::from(u8::from(child_index))] {
384
12
                current_index = next_index;
385
12
            } else {
386
11
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
41
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE19existing_node_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1F_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
341
241k
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
241k
        &self,
343
241k
        mut key: I,
344
241k
    ) -> ExistingNodeInnerResult<I> {
345
241k
        let 
mut current_index237k
= match self.root_index {
346
237k
            Some(ri) => ri,
347
            None => {
348
4.09k
                return ExistingNodeInnerResult::NotFound {
349
4.09k
                    closest_ancestor: None,
350
4.09k
                };
351
            }
352
        };
353
237k
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
237k
        let mut closest_ancestor = None;
356
357
        loop {
358
643k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
643k
            for 
nibble254k
in current.partial_key.iter().cloned() {
363
254k
                if key.next() != Some(nibble) {
364
13.2k
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
241k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
630k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
630k
            let child_index = match key.next() {
374
630k
                Some(n) => n,
375
                None => {
376
0
                    return ExistingNodeInnerResult::Found {
377
0
                        node_index: current_index,
378
0
                        has_storage_value: current.has_storage_value,
379
0
                    };
380
                }
381
            };
382
383
630k
            if let Some(
next_index405k
) = current.children[usize::from(u8::from(child_index))] {
384
405k
                current_index = next_index;
385
405k
            } else {
386
224k
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
241k
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECscoAnRPySggw_6author
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB5_12proof_encode4NodeEE19existing_node_innerINtNtNtNtB1e_4iter8adapters6copied6CopiedINtNtNtB1e_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
_RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
341
70
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
70
        &self,
343
70
        mut key: I,
344
70
    ) -> ExistingNodeInnerResult<I> {
345
70
        let 
mut current_index68
= match self.root_index {
346
68
            Some(ri) => ri,
347
            None => {
348
2
                return ExistingNodeInnerResult::NotFound {
349
2
                    closest_ancestor: None,
350
2
                };
351
            }
352
        };
353
68
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
68
        let mut closest_ancestor = None;
356
357
        loop {
358
158
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
2.13k
            for nibble in 
current.partial_key.iter()158
.
cloned158
() {
363
2.13k
                if key.next() != Some(nibble) {
364
26
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
2.11k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
132
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
132
            let child_index = match key.next() {
374
132
                Some(n) => n,
375
                None => {
376
0
                    return ExistingNodeInnerResult::Found {
377
0
                        node_index: current_index,
378
0
                        has_storage_value: current.has_storage_value,
379
0
                    };
380
                }
381
            };
382
383
132
            if let Some(
next_index90
) = current.children[usize::from(u8::from(child_index))] {
384
90
                current_index = next_index;
385
90
            } else {
386
42
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
70
    }
_RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE19existing_node_innerINtNtB5_6nibble14BytesToNibblesINtNtNtNtB1f_4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterhEEEECs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
341
665
    fn existing_node_inner<I: Iterator<Item = Nibble> + Clone>(
342
665
        &self,
343
665
        mut key: I,
344
665
    ) -> ExistingNodeInnerResult<I> {
345
665
        let 
mut current_index646
= match self.root_index {
346
646
            Some(ri) => ri,
347
            None => {
348
19
                return ExistingNodeInnerResult::NotFound {
349
19
                    closest_ancestor: None,
350
19
                };
351
            }
352
        };
353
646
        debug_assert!(self.nodes.get(current_index).unwrap().parent.is_none());
354
355
646
        let mut closest_ancestor = None;
356
357
        loop {
358
1.50k
            let current = self.nodes.get(current_index).unwrap();
359
360
            // First, we must remove `current`'s partial key from `key`, making sure that they
361
            // match.
362
20.2k
            for nibble in 
current.partial_key.iter()1.50k
.
cloned1.50k
() {
363
20.2k
                if key.next() != Some(nibble) {
364
247
                    return ExistingNodeInnerResult::NotFound { closest_ancestor };
365
20.0k
                }
366
            }
367
368
            // At this point, the tree traversal cursor (the `key` iterator) exactly matches
369
            // `current`.
370
1.25k
            closest_ancestor = Some((current_index, key.clone()));
371
372
            // If `key.next()` is `Some`, put it in `child_index`, otherwise return successfully.
373
1.25k
            let child_index = match key.next() {
374
1.25k
                Some(n) => n,
375
                None => {
376
0
                    return ExistingNodeInnerResult::Found {
377
0
                        node_index: current_index,
378
0
                        has_storage_value: current.has_storage_value,
379
0
                    };
380
                }
381
            };
382
383
1.25k
            if let Some(
next_index855
) = current.children[usize::from(u8::from(child_index))] {
384
855
                current_index = next_index;
385
855
            } else {
386
399
                return ExistingNodeInnerResult::NotFound { closest_ancestor };
387
            }
388
        }
389
665
    }
390
391
    /// Removes all nodes whose key starts with the given prefix.
392
    ///
393
    /// Returns the closest ancestors to the nodes that have been removed, or `None` if that
394
    /// closest ancestor is not a descendant of the new trie root.
395
8.12k
    pub fn remove_prefix(
396
8.12k
        &mut self,
397
8.12k
        prefix: impl Iterator<Item = Nibble> + Clone,
398
8.12k
    ) -> Option<NodeAccess<TUd>> {
399
        // `ancestor` is the node that doesn't have the prefix but is the common ancestor of all
400
        // the nodes to remove.
401
8.12k
        let (
ancestor_index7.24k
,
ancestor_child_nibble7.24k
) = match self.existing_node_inner(prefix.clone())
402
        {
403
6.89k
            ExistingNodeInnerResult::Found { node_index, .. } => {
404
6.89k
                match self.nodes.get(node_index).unwrap().parent {
405
6.67k
                    Some(p) => p,
406
                    None => {
407
                        // There is no parent, meaning that the trie is empty or the root of trie
408
                        // is a node with the requested prefix. Simply clear the entire trie.
409
221
                        self.nodes.clear();
410
221
                        self.root_index = None;
411
221
                        return None;
412
                    }
413
                }
414
            }
415
            ExistingNodeInnerResult::NotFound {
416
                closest_ancestor: None,
417
            } => {
418
                // The trie is empty, or the key of the root node of the trie doesn't start with
419
                // the requested prefix, or the key of the root node of the trie starts with the
420
                // requested prefix.
421
                // If the trie is empty. then there is nothing to do and we return `None`.
422
64
                let 
root_index44
= self.root_index
?20
;
423
424
                // Compare root key with the prefix.
425
44
                if !self.nodes[root_index]
426
44
                    .partial_key
427
44
                    .iter()
428
44
                    .zip(prefix)
429
150
                    .
all44
(|(a, b)| *a == b)
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEE0B9_
Line
Count
Source
429
147
                    .all(|(a, b)| *a == b)
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1B_5slice4iter4IterNtNtB7_6nibble6NibbleEEE0B9_
Line
Count
Source
429
3
                    .all(|(a, b)| *a == b)
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE13remove_prefixpE0B9_
430
                {
431
                    // Root node key doesn't match the prefix. Nothing to do.
432
26
                    return None;
433
18
                }
434
435
                // Root node key starts with the requested prefix. Clear the entire trie.
436
18
                self.nodes.clear();
437
18
                self.root_index = None;
438
18
                return None;
439
            }
440
            ExistingNodeInnerResult::NotFound {
441
1.16k
                closest_ancestor: Some((ancestor, mut prefix_remain)),
442
            } => {
443
                // It is possible that there is simply no node at all with the given prefix, in
444
                // which case there is closest ancestor but nothing to clear.
445
446
1.16k
                let child_index = prefix_remain.next().unwrap();
447
448
                // First possibility in case there is no node with the given prefix: the ancestor
449
                // simply has no child in the direction we want. For example, ancestor is
450
                // `[1, 2]`, there is a node at `[1, 2, 8]`, and we want to clear `[1, 2, 5]`.
451
696
                let direct_child = if let Some(c) =
452
1.16k
                    self.nodes[ancestor].children[usize::from(u8::from(child_index))]
453
                {
454
696
                    c
455
                } else {
456
464
                    return Some(self.node_by_index_inner(ancestor).unwrap());
457
                };
458
459
                // Second possibility in case there is no node with the given prefix: the ancestor
460
                // has a child in the direction we want, but this child doesn't have the prefix
461
                // that we want. For example, ancestor is `[1, 2]`, there is a node at
462
                // `[1, 2, 3, 8]`, and we want to clear `[1, 2, 3, 6]`.
463
                // TODO: this seems sub-optimal
464
696
                if !self.nodes[direct_child]
465
696
                    .partial_key
466
696
                    .iter()
467
696
                    .zip(prefix_remain)
468
1.33k
                    .
all696
(|(a, b)| *a == b)
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEEs_0B9_
Line
Count
Source
468
1.33k
                    .all(|(a, b)| *a == b)
Unexecuted instantiation: _RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1B_5slice4iter4IterNtNtB7_6nibble6NibbleEEEs_0B9_
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE13remove_prefixpEs_0B9_
469
                {
470
126
                    return Some(self.node_by_index_inner(ancestor).unwrap());
471
570
                }
472
473
570
                (ancestor, child_index)
474
            }
475
        };
476
477
        // Removes all the descendants of `ancestor` through `ancestor_child_nibble`.
478
        {
479
            // TODO: this performs allocations, do we care?
480
7.24k
            let first_remove_index = self
481
7.24k
                .nodes
482
7.24k
                .get_mut(ancestor_index)
483
7.24k
                .unwrap()
484
7.24k
                .children
485
7.24k
                .get_mut(usize::from(u8::from(ancestor_child_nibble)))
486
7.24k
                .unwrap()
487
7.24k
                .take()
488
7.24k
                .unwrap();
489
490
7.24k
            let mut to_remove = vec![first_remove_index];
491
25.2k
            while !to_remove.is_empty() {
492
18.0k
                let mut next_to_remove = Vec::new();
493
53.5k
                for node_index in 
to_remove18.0k
.
drain18.0k
(
..18.0k
) {
494
53.5k
                    let node = self.nodes.remove(node_index);
495
53.5k
                    next_to_remove.extend(node.children.iter().filter_map(|n| *n));
496
                }
497
18.0k
                mem::swap(&mut to_remove, &mut next_to_remove);
498
            }
499
        }
500
501
        // If `ancestor` is a branch node with only one child (had two children before this
502
        // function call, but we removed one earlier), we have to remove it from the tree as well.
503
        // If this is the case, `actual_ancestor_index` will be equal to `ancestor`'s parent.
504
        // Otherwise it is set to `ancestor_index`.
505
7.24k
        let actual_ancestor_index = {
506
7.24k
            let ancestor = self.nodes.get_mut(ancestor_index).unwrap();
507
7.24k
            debug_assert!(
508
24.3k
                
ancestor.has_storage_value7.24k
||
ancestor.children.iter()3.74k
.
any3.74k
(|c| c.is_some())
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEEs5_0B9_
Line
Count
Source
508
24.3k
                ancestor.has_storage_value || ancestor.children.iter().any(|c| c.is_some())
Unexecuted instantiation: _RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1B_5slice4iter4IterNtNtB7_6nibble6NibbleEEEs5_0B9_
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE13remove_prefixpEs5_0B9_
509
            );
510
7.24k
            if !ancestor.has_storage_value
511
59.9k
                && 
ancestor.children3.74k
.
iter3.74k
().
filter3.74k
(|c| c.is_some()).
count3.74k
() == 1
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEEs1_0B9_
Line
Count
Source
511
59.9k
                && ancestor.children.iter().filter(|c| c.is_some()).count() == 1
Unexecuted instantiation: _RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1B_5slice4iter4IterNtNtB7_6nibble6NibbleEEEs1_0B9_
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE13remove_prefixpEs1_0B9_
512
            {
513
1.91k
                let ancestor = self.nodes.remove(ancestor_index);
514
1.91k
                let sibling_node_index: usize = ancestor.children.iter().find_map(|c| *c).unwrap();
515
516
                // Update the sibling to point to the ancestor's parent.
517
                {
518
1.91k
                    let sibling = self.nodes.get_mut(sibling_node_index).unwrap();
519
1.91k
                    debug_assert_eq!(sibling.parent.as_ref().unwrap().0, ancestor_index);
520
1.91k
                    insert_front(
521
1.91k
                        &mut sibling.partial_key,
522
1.91k
                        ancestor.partial_key,
523
1.91k
                        sibling.parent.unwrap().1,
524
                    );
525
1.91k
                    sibling.parent = ancestor.parent;
526
                }
527
528
                // Update the ancestor's parent to point to the sibling.
529
1.91k
                if let Some((
ancestor_parent_index1.60k
,
parent_to_sibling_index1.60k
)) = ancestor.parent {
530
                    // Update the ancestory's parent to point to the sibling.
531
1.60k
                    let ancestor_parent = self.nodes.get_mut(ancestor_parent_index).unwrap();
532
1.60k
                    debug_assert_eq!(
533
1.60k
                        ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))],
534
1.60k
                        Some(ancestor_index)
535
                    );
536
1.60k
                    ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
537
1.60k
                        Some(sibling_node_index);
538
                } else {
539
311
                    debug_assert_eq!(self.root_index, Some(ancestor_index));
540
311
                    self.root_index = Some(sibling_node_index);
541
                }
542
543
1.91k
                ancestor.parent.map(|(idx, _)| idx)
544
            } else {
545
5.33k
                Some(ancestor_index)
546
            }
547
        };
548
549
        // Return value of the function.
550
7.24k
        actual_ancestor_index.map(move |idx| 
self6.93k
.
node_by_index_inner6.93k
(
idx6.93k
).
unwrap6.93k
())
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEEs4_0B9_
Line
Count
Source
550
6.93k
        actual_ancestor_index.map(move |idx| self.node_by_index_inner(idx).unwrap())
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1B_5slice4iter4IterNtNtB7_6nibble6NibbleEEEs4_0B9_
Line
Count
Source
550
2
        actual_ancestor_index.map(move |idx| self.node_by_index_inner(idx).unwrap())
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE13remove_prefixpEs4_0B9_
551
8.12k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE13remove_prefixINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB5_6nibble6NibbleEEB7_
Line
Count
Source
395
8.11k
    pub fn remove_prefix(
396
8.11k
        &mut self,
397
8.11k
        prefix: impl Iterator<Item = Nibble> + Clone,
398
8.11k
    ) -> Option<NodeAccess<TUd>> {
399
        // `ancestor` is the node that doesn't have the prefix but is the common ancestor of all
400
        // the nodes to remove.
401
8.11k
        let (
ancestor_index7.24k
,
ancestor_child_nibble7.24k
) = match self.existing_node_inner(prefix.clone())
402
        {
403
6.89k
            ExistingNodeInnerResult::Found { node_index, .. } => {
404
6.89k
                match self.nodes.get(node_index).unwrap().parent {
405
6.67k
                    Some(p) => p,
406
                    None => {
407
                        // There is no parent, meaning that the trie is empty or the root of trie
408
                        // is a node with the requested prefix. Simply clear the entire trie.
409
220
                        self.nodes.clear();
410
220
                        self.root_index = None;
411
220
                        return None;
412
                    }
413
                }
414
            }
415
            ExistingNodeInnerResult::NotFound {
416
                closest_ancestor: None,
417
            } => {
418
                // The trie is empty, or the key of the root node of the trie doesn't start with
419
                // the requested prefix, or the key of the root node of the trie starts with the
420
                // requested prefix.
421
                // If the trie is empty. then there is nothing to do and we return `None`.
422
63
                let 
root_index43
= self.root_index
?20
;
423
424
                // Compare root key with the prefix.
425
43
                if !self.nodes[root_index]
426
43
                    .partial_key
427
43
                    .iter()
428
43
                    .zip(prefix)
429
43
                    .all(|(a, b)| *a == b)
430
                {
431
                    // Root node key doesn't match the prefix. Nothing to do.
432
25
                    return None;
433
18
                }
434
435
                // Root node key starts with the requested prefix. Clear the entire trie.
436
18
                self.nodes.clear();
437
18
                self.root_index = None;
438
18
                return None;
439
            }
440
            ExistingNodeInnerResult::NotFound {
441
1.15k
                closest_ancestor: Some((ancestor, mut prefix_remain)),
442
            } => {
443
                // It is possible that there is simply no node at all with the given prefix, in
444
                // which case there is closest ancestor but nothing to clear.
445
446
1.15k
                let child_index = prefix_remain.next().unwrap();
447
448
                // First possibility in case there is no node with the given prefix: the ancestor
449
                // simply has no child in the direction we want. For example, ancestor is
450
                // `[1, 2]`, there is a node at `[1, 2, 8]`, and we want to clear `[1, 2, 5]`.
451
696
                let direct_child = if let Some(c) =
452
1.15k
                    self.nodes[ancestor].children[usize::from(u8::from(child_index))]
453
                {
454
696
                    c
455
                } else {
456
463
                    return Some(self.node_by_index_inner(ancestor).unwrap());
457
                };
458
459
                // Second possibility in case there is no node with the given prefix: the ancestor
460
                // has a child in the direction we want, but this child doesn't have the prefix
461
                // that we want. For example, ancestor is `[1, 2]`, there is a node at
462
                // `[1, 2, 3, 8]`, and we want to clear `[1, 2, 3, 6]`.
463
                // TODO: this seems sub-optimal
464
696
                if !self.nodes[direct_child]
465
696
                    .partial_key
466
696
                    .iter()
467
696
                    .zip(prefix_remain)
468
696
                    .all(|(a, b)| *a == b)
469
                {
470
126
                    return Some(self.node_by_index_inner(ancestor).unwrap());
471
570
                }
472
473
570
                (ancestor, child_index)
474
            }
475
        };
476
477
        // Removes all the descendants of `ancestor` through `ancestor_child_nibble`.
478
        {
479
            // TODO: this performs allocations, do we care?
480
7.24k
            let first_remove_index = self
481
7.24k
                .nodes
482
7.24k
                .get_mut(ancestor_index)
483
7.24k
                .unwrap()
484
7.24k
                .children
485
7.24k
                .get_mut(usize::from(u8::from(ancestor_child_nibble)))
486
7.24k
                .unwrap()
487
7.24k
                .take()
488
7.24k
                .unwrap();
489
490
7.24k
            let mut to_remove = vec![first_remove_index];
491
25.2k
            while !to_remove.is_empty() {
492
18.0k
                let mut next_to_remove = Vec::new();
493
53.5k
                for node_index in 
to_remove18.0k
.
drain18.0k
(
..18.0k
) {
494
53.5k
                    let node = self.nodes.remove(node_index);
495
53.5k
                    next_to_remove.extend(node.children.iter().filter_map(|n| *n));
496
                }
497
18.0k
                mem::swap(&mut to_remove, &mut next_to_remove);
498
            }
499
        }
500
501
        // If `ancestor` is a branch node with only one child (had two children before this
502
        // function call, but we removed one earlier), we have to remove it from the tree as well.
503
        // If this is the case, `actual_ancestor_index` will be equal to `ancestor`'s parent.
504
        // Otherwise it is set to `ancestor_index`.
505
7.24k
        let actual_ancestor_index = {
506
7.24k
            let ancestor = self.nodes.get_mut(ancestor_index).unwrap();
507
7.24k
            debug_assert!(
508
7.24k
                ancestor.has_storage_value || 
ancestor.children.iter()3.74k
.
any3.74k
(|c| c.is_some())
509
            );
510
7.24k
            if !ancestor.has_storage_value
511
3.74k
                && ancestor.children.iter().filter(|c| c.is_some()).count() == 1
512
            {
513
1.91k
                let ancestor = self.nodes.remove(ancestor_index);
514
1.91k
                let sibling_node_index: usize = ancestor.children.iter().find_map(|c| *c).unwrap();
515
516
                // Update the sibling to point to the ancestor's parent.
517
                {
518
1.91k
                    let sibling = self.nodes.get_mut(sibling_node_index).unwrap();
519
1.91k
                    debug_assert_eq!(sibling.parent.as_ref().unwrap().0, ancestor_index);
520
1.91k
                    insert_front(
521
1.91k
                        &mut sibling.partial_key,
522
1.91k
                        ancestor.partial_key,
523
1.91k
                        sibling.parent.unwrap().1,
524
                    );
525
1.91k
                    sibling.parent = ancestor.parent;
526
                }
527
528
                // Update the ancestor's parent to point to the sibling.
529
1.91k
                if let Some((
ancestor_parent_index1.60k
,
parent_to_sibling_index1.60k
)) = ancestor.parent {
530
                    // Update the ancestory's parent to point to the sibling.
531
1.60k
                    let ancestor_parent = self.nodes.get_mut(ancestor_parent_index).unwrap();
532
1.60k
                    debug_assert_eq!(
533
1.60k
                        ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))],
534
1.60k
                        Some(ancestor_index)
535
                    );
536
1.60k
                    ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
537
1.60k
                        Some(sibling_node_index);
538
                } else {
539
311
                    debug_assert_eq!(self.root_index, Some(ancestor_index));
540
311
                    self.root_index = Some(sibling_node_index);
541
                }
542
543
1.91k
                ancestor.parent.map(|(idx, _)| idx)
544
            } else {
545
5.32k
                Some(ancestor_index)
546
            }
547
        };
548
549
        // Return value of the function.
550
7.24k
        actual_ancestor_index.map(move |idx| self.node_by_index_inner(idx).unwrap())
551
8.11k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE13remove_prefixINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1z_5slice4iter4IterNtNtB5_6nibble6NibbleEEEB7_
Line
Count
Source
395
5
    pub fn remove_prefix(
396
5
        &mut self,
397
5
        prefix: impl Iterator<Item = Nibble> + Clone,
398
5
    ) -> Option<NodeAccess<TUd>> {
399
        // `ancestor` is the node that doesn't have the prefix but is the common ancestor of all
400
        // the nodes to remove.
401
5
        let (
ancestor_index2
,
ancestor_child_nibble2
) = match self.existing_node_inner(prefix.clone())
402
        {
403
3
            ExistingNodeInnerResult::Found { node_index, .. } => {
404
3
                match self.nodes.get(node_index).unwrap().parent {
405
2
                    Some(p) => p,
406
                    None => {
407
                        // There is no parent, meaning that the trie is empty or the root of trie
408
                        // is a node with the requested prefix. Simply clear the entire trie.
409
1
                        self.nodes.clear();
410
1
                        self.root_index = None;
411
1
                        return None;
412
                    }
413
                }
414
            }
415
            ExistingNodeInnerResult::NotFound {
416
                closest_ancestor: None,
417
            } => {
418
                // The trie is empty, or the key of the root node of the trie doesn't start with
419
                // the requested prefix, or the key of the root node of the trie starts with the
420
                // requested prefix.
421
                // If the trie is empty. then there is nothing to do and we return `None`.
422
1
                let root_index = self.root_index
?0
;
423
424
                // Compare root key with the prefix.
425
1
                if !self.nodes[root_index]
426
1
                    .partial_key
427
1
                    .iter()
428
1
                    .zip(prefix)
429
1
                    .all(|(a, b)| *a == b)
430
                {
431
                    // Root node key doesn't match the prefix. Nothing to do.
432
1
                    return None;
433
0
                }
434
435
                // Root node key starts with the requested prefix. Clear the entire trie.
436
0
                self.nodes.clear();
437
0
                self.root_index = None;
438
0
                return None;
439
            }
440
            ExistingNodeInnerResult::NotFound {
441
1
                closest_ancestor: Some((ancestor, mut prefix_remain)),
442
            } => {
443
                // It is possible that there is simply no node at all with the given prefix, in
444
                // which case there is closest ancestor but nothing to clear.
445
446
1
                let child_index = prefix_remain.next().unwrap();
447
448
                // First possibility in case there is no node with the given prefix: the ancestor
449
                // simply has no child in the direction we want. For example, ancestor is
450
                // `[1, 2]`, there is a node at `[1, 2, 8]`, and we want to clear `[1, 2, 5]`.
451
0
                let direct_child = if let Some(c) =
452
1
                    self.nodes[ancestor].children[usize::from(u8::from(child_index))]
453
                {
454
0
                    c
455
                } else {
456
1
                    return Some(self.node_by_index_inner(ancestor).unwrap());
457
                };
458
459
                // Second possibility in case there is no node with the given prefix: the ancestor
460
                // has a child in the direction we want, but this child doesn't have the prefix
461
                // that we want. For example, ancestor is `[1, 2]`, there is a node at
462
                // `[1, 2, 3, 8]`, and we want to clear `[1, 2, 3, 6]`.
463
                // TODO: this seems sub-optimal
464
0
                if !self.nodes[direct_child]
465
0
                    .partial_key
466
0
                    .iter()
467
0
                    .zip(prefix_remain)
468
0
                    .all(|(a, b)| *a == b)
469
                {
470
0
                    return Some(self.node_by_index_inner(ancestor).unwrap());
471
0
                }
472
473
0
                (ancestor, child_index)
474
            }
475
        };
476
477
        // Removes all the descendants of `ancestor` through `ancestor_child_nibble`.
478
        {
479
            // TODO: this performs allocations, do we care?
480
2
            let first_remove_index = self
481
2
                .nodes
482
2
                .get_mut(ancestor_index)
483
2
                .unwrap()
484
2
                .children
485
2
                .get_mut(usize::from(u8::from(ancestor_child_nibble)))
486
2
                .unwrap()
487
2
                .take()
488
2
                .unwrap();
489
490
2
            let mut to_remove = vec![first_remove_index];
491
8
            while !to_remove.is_empty() {
492
6
                let mut next_to_remove = Vec::new();
493
8
                for node_index in 
to_remove6
.
drain6
(
..6
) {
494
8
                    let node = self.nodes.remove(node_index);
495
8
                    next_to_remove.extend(node.children.iter().filter_map(|n| *n));
496
                }
497
6
                mem::swap(&mut to_remove, &mut next_to_remove);
498
            }
499
        }
500
501
        // If `ancestor` is a branch node with only one child (had two children before this
502
        // function call, but we removed one earlier), we have to remove it from the tree as well.
503
        // If this is the case, `actual_ancestor_index` will be equal to `ancestor`'s parent.
504
        // Otherwise it is set to `ancestor_index`.
505
2
        let actual_ancestor_index = {
506
2
            let ancestor = self.nodes.get_mut(ancestor_index).unwrap();
507
2
            debug_assert!(
508
2
                ancestor.has_storage_value || 
ancestor.children.iter()0
.
any0
(|c| c.is_some())
509
            );
510
2
            if !ancestor.has_storage_value
511
0
                && ancestor.children.iter().filter(|c| c.is_some()).count() == 1
512
            {
513
0
                let ancestor = self.nodes.remove(ancestor_index);
514
0
                let sibling_node_index: usize = ancestor.children.iter().find_map(|c| *c).unwrap();
515
516
                // Update the sibling to point to the ancestor's parent.
517
                {
518
0
                    let sibling = self.nodes.get_mut(sibling_node_index).unwrap();
519
0
                    debug_assert_eq!(sibling.parent.as_ref().unwrap().0, ancestor_index);
520
0
                    insert_front(
521
0
                        &mut sibling.partial_key,
522
0
                        ancestor.partial_key,
523
0
                        sibling.parent.unwrap().1,
524
                    );
525
0
                    sibling.parent = ancestor.parent;
526
                }
527
528
                // Update the ancestor's parent to point to the sibling.
529
0
                if let Some((ancestor_parent_index, parent_to_sibling_index)) = ancestor.parent {
530
                    // Update the ancestory's parent to point to the sibling.
531
0
                    let ancestor_parent = self.nodes.get_mut(ancestor_parent_index).unwrap();
532
0
                    debug_assert_eq!(
533
0
                        ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))],
534
0
                        Some(ancestor_index)
535
                    );
536
0
                    ancestor_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
537
0
                        Some(sibling_node_index);
538
                } else {
539
0
                    debug_assert_eq!(self.root_index, Some(ancestor_index));
540
0
                    self.root_index = Some(sibling_node_index);
541
                }
542
543
0
                ancestor.parent.map(|(idx, _)| idx)
544
            } else {
545
2
                Some(ancestor_index)
546
            }
547
        };
548
549
        // Return value of the function.
550
2
        actual_ancestor_index.map(move |idx| self.node_by_index_inner(idx).unwrap())
551
5
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE13remove_prefixpEB7_
552
553
    /// Returns true if the structure of this trie is the same as the structure of `other`.
554
    ///
555
    /// Everything is compared for equality except for the user datas.
556
    ///
557
    /// > **Note**: This function does a preliminary check for `self.len() == other.len()`. If the
558
    /// >           length are different, `false` is immediately returned. If the lengths are
559
    /// >           equal, the function performs the expensive operation of traversing both
560
    /// >           tries in order to detect a potential mismatch.
561
    ///
562
    /// # Examples
563
    ///
564
    /// ```
565
    /// use smoldot::trie::{self, trie_structure};
566
    ///
567
    /// let mut trie1 = trie_structure::TrieStructure::new();
568
    /// let mut trie2 = trie_structure::TrieStructure::new();
569
    /// assert!(trie1.structure_equal(&trie2));
570
    ///
571
    /// // Insert a node in the first trie.
572
    /// trie1
573
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
574
    ///     .into_vacant()
575
    ///     .unwrap()
576
    ///     .insert_storage_value()
577
    ///     .insert(1234, 5678);
578
    /// assert!(!trie1.structure_equal(&trie2));
579
    ///
580
    /// // Insert the same node in the second trie, but with a different user data.
581
    /// // The type of the user data of the second trie (strings) isn't even the same as for the
582
    /// // first trie (i32s).
583
    /// trie2
584
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
585
    ///     .into_vacant()
586
    ///     .unwrap()
587
    ///     .insert_storage_value()
588
    ///     .insert("hello", "world");
589
    ///
590
    /// // `structure_equal` returns true because both tries have the same nodes.
591
    /// assert!(trie1.structure_equal(&trie2));
592
    /// ```
593
3.84k
    pub fn structure_equal<T>(&self, other: &TrieStructure<T>) -> bool {
594
3.84k
        if self.nodes.len() != other.nodes.len() {
595
0
            return false;
596
3.84k
        }
597
598
3.84k
        let mut me_iter = self.all_node_lexicographic_ordered();
599
3.84k
        let mut other_iter = other.all_node_lexicographic_ordered();
600
601
        loop {
602
719k
            let (
me_node_idx715k
,
other_node_idx715k
) = match (me_iter.next(), other_iter.next()) {
603
715k
                (Some(a), Some(b)) => (a, b),
604
3.84k
                (None, None) => return true,
605
0
                _ => return false,
606
            };
607
608
715k
            let me_node = self.nodes.get(me_node_idx).unwrap();
609
715k
            let other_node = other.nodes.get(other_node_idx).unwrap();
610
611
715k
            if me_node.has_storage_value != other_node.has_storage_value {
612
0
                return false;
613
715k
            }
614
615
715k
            match (me_node.parent, other_node.parent) {
616
711k
                (Some((_, i)), Some((_, j))) if i == j => {}
617
3.84k
                (None, None) => {}
618
0
                _ => return false,
619
            }
620
621
715k
            if me_node.partial_key != other_node.partial_key {
622
0
                return false;
623
715k
            }
624
        }
625
3.84k
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE15structure_equaluEB7_
Line
Count
Source
593
3.84k
    pub fn structure_equal<T>(&self, other: &TrieStructure<T>) -> bool {
594
3.84k
        if self.nodes.len() != other.nodes.len() {
595
0
            return false;
596
3.84k
        }
597
598
3.84k
        let mut me_iter = self.all_node_lexicographic_ordered();
599
3.84k
        let mut other_iter = other.all_node_lexicographic_ordered();
600
601
        loop {
602
719k
            let (
me_node_idx715k
,
other_node_idx715k
) = match (me_iter.next(), other_iter.next()) {
603
715k
                (Some(a), Some(b)) => (a, b),
604
3.84k
                (None, None) => return true,
605
0
                _ => return false,
606
            };
607
608
715k
            let me_node = self.nodes.get(me_node_idx).unwrap();
609
715k
            let other_node = other.nodes.get(other_node_idx).unwrap();
610
611
715k
            if me_node.has_storage_value != other_node.has_storage_value {
612
0
                return false;
613
715k
            }
614
615
715k
            match (me_node.parent, other_node.parent) {
616
711k
                (Some((_, i)), Some((_, j))) if i == j => {}
617
3.84k
                (None, None) => {}
618
0
                _ => return false,
619
            }
620
621
715k
            if me_node.partial_key != other_node.partial_key {
622
0
                return false;
623
715k
            }
624
        }
625
3.84k
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE15structure_equalpEB7_
626
627
    /// Returns all nodes whose full key is within the given range, in lexicographic order.
628
    // TODO: change API to accept the range trait?
629
    #[inline]
630
0
    pub fn range<'a>(
631
0
        &'a self,
632
0
        start_bound: ops::Bound<&'a [u8]>, // TODO: why does this require a `'a` lifetime? I don't get it
633
0
        end_bound: ops::Bound<&'a [u8]>,
634
0
    ) -> impl Iterator<Item = NodeIndex> + use<'a, TUd> {
635
0
        let start_bound = match start_bound {
636
0
            ops::Bound::Included(key) => {
637
0
                ops::Bound::Included(bytes_to_nibbles(key.iter().copied()))
638
            }
639
0
            ops::Bound::Excluded(key) => {
640
0
                ops::Bound::Excluded(bytes_to_nibbles(key.iter().copied()))
641
            }
642
0
            ops::Bound::Unbounded => ops::Bound::Unbounded,
643
        };
644
645
0
        let end_bound = match end_bound {
646
0
            ops::Bound::Included(key) => {
647
0
                ops::Bound::Included(bytes_to_nibbles(key.iter().copied()))
648
            }
649
0
            ops::Bound::Excluded(key) => {
650
0
                ops::Bound::Excluded(bytes_to_nibbles(key.iter().copied()))
651
            }
652
0
            ops::Bound::Unbounded => ops::Bound::Unbounded,
653
        };
654
655
0
        self.range_inner(start_bound, end_bound).map(NodeIndex)
656
0
    }
Unexecuted instantiation: _RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructurepE5rangeB6_
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE5rangeB6_
657
658
    /// Returns all nodes whose full key is within the given range, in lexicographic order.
659
6.60M
    pub fn range_iter<'a>(
660
6.60M
        &'a self,
661
6.60M
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
662
6.60M
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
663
6.60M
    ) -> impl Iterator<Item = NodeIndex> {
664
6.60M
        self.range_inner(start_bound, end_bound).map(NodeIndex)
665
6.60M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE10range_iterINtNtB1Q_9into_iter8IntoIterNtNtB5_6nibble6NibbleEINtNtNtNtB1f_4iter7sources5empty5EmptyB47_EEB7_
Line
Count
Source
659
6.34M
    pub fn range_iter<'a>(
660
6.34M
        &'a self,
661
6.34M
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
662
6.34M
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
663
6.34M
    ) -> impl Iterator<Item = NodeIndex> {
664
6.34M
        self.range_inner(start_bound, end_bound).map(NodeIndex)
665
6.34M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE10range_iterINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1w_5slice4iter4IterNtNtB5_6nibble6NibbleEEB1n_EB7_
Line
Count
Source
659
262k
    pub fn range_iter<'a>(
660
262k
        &'a self,
661
262k
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
662
262k
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
663
262k
    ) -> impl Iterator<Item = NodeIndex> {
664
262k
        self.range_inner(start_bound, end_bound).map(NodeIndex)
665
262k
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE10range_iterppEB7_
666
667
    /// Returns all nodes whose full key is within the given range, in lexicographic order.
668
6.60M
    fn range_inner<'a>(
669
6.60M
        &'a self,
670
6.60M
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
671
6.60M
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
672
6.60M
    ) -> impl Iterator<Item = usize> {
673
        // Start by processing the end bound to obtain an "end key".
674
        // This end key is always assumed to be excluded. In other words, only keys strictly
675
        // inferior to the end key are returned. If the user provides `Included`, we modify the key
676
        // and append a dummy `0` nibble at the end of it. If the user provides `Unbounded`, we use
677
        // an infinite-sized key so that every finite key is always inferior to it.
678
        //
679
        // The algorithm later down this function will pop nibbles from the start of `end_key`.
680
        // Because `end_key` is always excluded, this iterator must always contain at least one
681
        // nibble, otherwise the iteration should have ended.
682
6.60M
        let mut end_key = match end_bound {
683
6.43M
            ops::Bound::Unbounded => either::Left(iter::repeat(Nibble::max())),
684
87.3k
            ops::Bound::Excluded(end_key) => either::Right(end_key.chain(None)),
685
87.3k
            ops::Bound::Included(end_key) => either::Right(end_key.chain(Some(Nibble::zero()))),
686
        }
687
6.60M
        .peekable();
688
689
        // The user passed `Excluded(&[])`. Return an empty range.
690
6.60M
        if end_key.peek().is_none() {
691
14.5k
            return either::Right(iter::empty());
692
6.59M
        }
693
694
        // The code below creates a variable named `iter`. This `iter` represents the cursor
695
        // where the iterator is.
696
        // `iter` also contains an optional nibble. If this optional nibble is `None`, the
697
        // iteration is currently at the node itself. If it is `Some`, the iteration isn't at the
698
        // node itself but at its child of the given nibble (which potentially doesn't exist).
699
        // If it is `Some(None)`, then the iteration is right after the last children of the node.
700
        // In other words, `Some(None)` represents an overflow.
701
6.59M
        let 
mut iter6.59M
:
(usize, Option<Option<Nibble>>)6.59M
= match self.root_index {
702
6.59M
            Some(idx) => (idx, None),
703
            None => {
704
                // Trie is empty. Special case.
705
363
                return either::Right(iter::empty());
706
            }
707
        };
708
709
        // Equal to `len(key(iter)) - len(key(iter) ∩ end_key)`. In other words, the number of
710
        // nibbles at the end of `iter`'s key that do not match the end key. This also includes
711
        // the optional nibble within `iter` if any.
712
6.59M
        let mut iter_key_nibbles_extra: usize = 0;
713
714
        // Transform `start_bound` into something more simple to process.
715
6.59M
        let (mut start_key, start_key_is_inclusive) = match start_bound {
716
82.7k
            ops::Bound::Unbounded => (either::Right(iter::empty()), true),
717
6.42M
            ops::Bound::Included(k) => (either::Left(k), true),
718
82.4k
            ops::Bound::Excluded(k) => (either::Left(k), false),
719
        };
720
721
        // Iterate down the tree, updating the variables above. At each iteration, one of the
722
        // three following is true:
723
        //
724
        // - `iter` is inferior or inferior or equal (depending on `start_key_is_inclusive`) to
725
        //   `start_key`.
726
        // - `iter` is the first node that is superior or strictly superior (depending on
727
        //   `start_key_is_inclusive`) to `start_key`.
728
        // - `iter` points to a non-existing node that is inferior/inferior-or-equal to
729
        //   `start_key`, but is right before the first node that is superior/strictly superior to
730
        //   `start_key`.
731
        //
732
        // As soon as we reach one of the last two conditions, we stop iterating, as it means
733
        // that `iter` is at the correct position.
734
        'start_search: loop {
735
8.15M
            debug_assert!(iter.1.is_none());
736
8.15M
            let iter_node = self.nodes.get(iter.0).unwrap();
737
738
            // Compare the nibbles at the front of `start_key` with the ones of `iter_node`.
739
            // Consumes the nibbles at the start of `start_key`.
740
8.15M
            let pk_compare = {
741
8.15M
                let mut result = cmp::Ordering::Equal;
742
8.15M
                for 
iter_node_pk_nibble2.99M
in iter_node.partial_key.iter() {
743
2.99M
                    match start_key
744
2.99M
                        .next()
745
2.99M
                        .map(|nibble| 
nibble2.93M
.
cmp2.93M
(
iter_node_pk_nibble2.93M
))
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1S_9into_iter8IntoIterNtNtB7_6nibble6NibbleEINtNtNtNtB1h_4iter7sources5empty5EmptyB4a_EE0B9_
Line
Count
Source
745
2.91M
                        .map(|nibble| nibble.cmp(iter_node_pk_nibble))
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1z_5slice4iter4IterNtNtB7_6nibble6NibbleEEB1q_E0B9_
Line
Count
Source
745
18.5k
                        .map(|nibble| nibble.cmp(iter_node_pk_nibble))
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE11range_innerppE0B9_
746
                    {
747
                        None | Some(cmp::Ordering::Less) => {
748
365k
                            result = cmp::Ordering::Less;
749
365k
                            break;
750
                        }
751
2.33M
                        Some(cmp::Ordering::Equal) => {}
752
                        Some(cmp::Ordering::Greater) => {
753
300k
                            result = cmp::Ordering::Greater;
754
300k
                            break;
755
                        }
756
                    }
757
                }
758
8.15M
                result
759
            };
760
761
8.15M
            match pk_compare {
762
                cmp::Ordering::Less | cmp::Ordering::Equal => {
763
                    // Update the value of `iter_key_nibbles_extra` to take the current value
764
                    // of `iter` into account, as it hasn't been done yet.
765
7.85M
                    for 
iter_node_pk_nibble3.39M
in iter_node.partial_key.iter().cloned() {
766
3.39M
                        if iter_key_nibbles_extra == 0
767
74.6k
                            && iter_node_pk_nibble == *end_key.peek().unwrap()
768
                        {
769
5.71k
                            let _ = end_key.next();
770
                            // `iter` is already past the end bound. Return an empty range.
771
5.71k
                            if end_key.peek().is_none() {
772
13
                                return either::Right(iter::empty());
773
5.70k
                            }
774
3.38M
                        } else if iter_key_nibbles_extra == 0
775
68.9k
                            && iter_node_pk_nibble > *end_key.peek().unwrap()
776
                        {
777
855
                            return either::Right(iter::empty());
778
3.38M
                        } else {
779
3.38M
                            iter_key_nibbles_extra += 1;
780
3.38M
                        }
781
                    }
782
783
7.85M
                    if pk_compare == cmp::Ordering::Less {
784
                        // `iter` is strictly superior to `start_key`. `iter` is now at the
785
                        // correct position.
786
364k
                        break 'start_search;
787
7.49M
                    }
788
                }
789
                cmp::Ordering::Greater => {
790
                    // `iter` is strictly inferior to `start_key`, and all of its children will
791
                    // also be strictly inferior to `start_key`.
792
                    // Stop the search immediately after the current node in the parent.
793
300k
                    let Some((
parent299k
,
parent_nibble299k
)) = iter_node.parent else {
794
401
                        return either::Right(iter::empty());
795
                    };
796
299k
                    let next_nibble = parent_nibble.checked_add(1);
797
299k
                    if iter_key_nibbles_extra == 0 {
798
19.3k
                        return either::Right(iter::empty());
799
280k
                    }
800
280k
                    iter_key_nibbles_extra -= 1;
801
280k
                    if iter_key_nibbles_extra == 0 && 
next_nibble278k
== Some(*
end_key278k
.peek().unwrap())
802
                    {
803
18.9k
                        let _ = end_key.next();
804
                        // `iter` is already past the end bound. Return an empty range.
805
18.9k
                        if end_key.peek().is_none() {
806
36
                            return either::Right(iter::empty());
807
18.9k
                        }
808
261k
                    } else {
809
261k
                        iter_key_nibbles_extra += 1;
810
261k
                    }
811
280k
                    iter = (parent, Some(next_nibble));
812
280k
                    break 'start_search;
813
                }
814
            }
815
816
            // Remove the next nibble from `start_key` and update `iter` based on it.
817
7.49M
            if let Some(
next_nibble7.33M
) = start_key.next() {
818
7.33M
                if iter_key_nibbles_extra == 0 && 
next_nibble6.45M
== *
end_key6.45M
.peek().unwrap() {
819
403k
                    let _ = end_key.next();
820
                    // `iter` is already past the end bound. Return an empty range.
821
403k
                    if end_key.peek().is_none() {
822
991
                        return either::Right(iter::empty());
823
402k
                    }
824
6.93M
                } else if iter_key_nibbles_extra == 0 && 
next_nibble6.05M
> *
end_key6.05M
.peek().unwrap() {
825
45.5k
                    return either::Right(iter::empty());
826
6.88M
                } else {
827
6.88M
                    iter_key_nibbles_extra += 1;
828
6.88M
                }
829
830
7.29M
                if let Some(
child1.56M
) = iter_node.children[usize::from(u8::from(next_nibble))] {
831
1.56M
                    // Update `iter` and continue searching.
832
1.56M
                    iter = (child, None);
833
1.56M
                } else {
834
                    // `iter` is strictly inferior to `start_key`.
835
5.72M
                    iter.1 = Some(Some(next_nibble));
836
5.72M
                    break 'start_search;
837
                }
838
            } else {
839
                // `iter.0` is an exact match with `start_key`. If the starting bound is
840
                // `Excluded`, we don't want to start iterating at `iter` but at `next(iter)`,
841
                // which we do by adding a zero nibble afterwards.
842
156k
                debug_assert!(iter.1.is_none());
843
156k
                if !start_key_is_inclusive {
844
16.4k
                    iter.1 = Some(Some(Nibble::zero()));
845
16.4k
                    if iter_key_nibbles_extra == 0 && 
*13.7k
end_key13.7k
.peek().unwrap() == Nibble::zero() {
846
1.37k
                        let _ = end_key.next();
847
                        // `iter` is already past the end bound. Return an empty range.
848
1.37k
                        if end_key.peek().is_none() {
849
875
                            return either::Right(iter::empty());
850
498
                        }
851
15.0k
                    } else {
852
15.0k
                        iter_key_nibbles_extra += 1;
853
15.0k
                    }
854
139k
                }
855
856
155k
                break 'start_search;
857
            }
858
        }
859
860
        // `iter` is now at the correct position and we can start yielding nodes until we reach
861
        // the end. This is done in the iterator that is returned from the function.
862
863
13.3M
        either::Left(
iter::from_fn6.52M
(move || {
864
            loop {
865
                // `end_key` must never be empty, as otherwise the iteration has ended.
866
                // We return `None` instead of panicking, as it is legitimately possible to reach
867
                // this situation through some code paths.
868
37.6M
                let _ = end_key.peek()
?17.2k
;
869
870
                // If `iter` points to an actual node, yield it and jump to the position right
871
                // after.
872
37.5M
                let Some(
iter_127.6M
) = iter.1 else {
873
9.90M
                    iter.1 = Some(Some(Nibble::zero()));
874
9.90M
                    if iter_key_nibbles_extra == 0 && 
*204k
end_key204k
.peek().unwrap() == Nibble::zero() {
875
14.0k
                        let _ = end_key.next();
876
9.89M
                    } else {
877
9.89M
                        iter_key_nibbles_extra += 1;
878
9.89M
                    }
879
9.90M
                    return Some(iter.0);
880
                };
881
882
27.6M
                let node = self.nodes.get(iter.0).unwrap();
883
884
9.41M
                if let Some(child) =
885
27.6M
                    iter_1.and_then(|iter_1| node.children[
usize::from27.3M
(
u8::from27.3M
(
iter_127.3M
))])
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1f_NtNtB9_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1U_9into_iter8IntoIterNtNtB9_6nibble6NibbleEINtNtNtNtB1j_4iter7sources5empty5EmptyB4c_EEs_00Bb_
Line
Count
Source
885
9.30M
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1B_5slice4iter4IterNtNtB9_6nibble6NibbleEEB1s_Es_00Bb_
Line
Count
Source
885
18.0M
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
Unexecuted instantiation: _RNCNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_13TrieStructurepE11range_innerppEs_00Bb_
886
                {
887
                    // `child` might be after the end bound if its partial key is superior or
888
                    // equal to the `end_key`.
889
12.8M
                    for child_pk_nibble in 
self.nodes9.41M
.
get9.41M
(child).unwrap().partial_key.
iter9.41M
() {
890
12.8M
                        match child_pk_nibble.cmp(end_key.peek().unwrap()) {
891
594k
                            cmp::Ordering::Greater if iter_key_nibbles_extra == 
011.8k
=> return
None11.8k
,
892
12.0M
                            cmp::Ordering::Greater | cmp::Ordering::Less => {
893
12.0M
                                iter_key_nibbles_extra += 1;
894
12.0M
                            }
895
808k
                            cmp::Ordering::Equal if iter_key_nibbles_extra != 
0790k
=> {
896
790k
                                iter_key_nibbles_extra += 1;
897
790k
                            }
898
                            cmp::Ordering::Equal => {
899
18.0k
                                debug_assert_eq!(iter_key_nibbles_extra, 0);
900
18.0k
                                let _ = end_key.next();
901
18.0k
                                let _ = end_key.peek()
?311
;
902
                            }
903
                        }
904
                    }
905
906
9.40M
                    iter = (child, None);
907
18.2M
                } else if iter_key_nibbles_extra == 0
908
17.8M
                    || (iter_key_nibbles_extra == 1
909
5.90M
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1f_NtNtB9_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1U_9into_iter8IntoIterNtNtB9_6nibble6NibbleEINtNtNtNtB1j_4iter7sources5empty5EmptyB4c_EEs_0s_0Bb_
Line
Count
Source
909
5.38M
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1B_5slice4iter4IterNtNtB9_6nibble6NibbleEEB1s_Es_0s_0Bb_
Line
Count
Source
909
523k
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
Unexecuted instantiation: _RNCNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_13TrieStructurepE11range_innerppEs_0s_0Bb_
910
                {
911
414k
                    return None;
912
17.8M
                } else if let Some(
child_index7.45M
) = iter_1.and_then(|iter_1|
{17.4M
913
17.4M
                    node.children[(usize::from(u8::from(iter_1)))
914
17.4M
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
5.90M
                            *end_key.peek().unwrap()
916
                        } else {
917
11.5M
                            Nibble::max()
918
                        }))]
919
17.4M
                        .iter()
920
149M
                        .
position17.4M
(|c| c.is_some())
_RNCNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB9_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtBb_16TrieEntryVersionEEIB1h_NtNtBb_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1W_9into_iter8IntoIterNtNtBb_6nibble6NibbleEINtNtNtNtB1l_4iter7sources5empty5EmptyB4e_EEs_0s0_00Bd_
Line
Count
Source
920
41.7M
                        .position(|c| c.is_some())
_RNCNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB9_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1D_5slice4iter4IterNtNtBb_6nibble6NibbleEEB1u_Es_0s0_00Bd_
Line
Count
Source
920
107M
                        .position(|c| c.is_some())
Unexecuted instantiation: _RNCNCNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB9_13TrieStructurepE11range_innerppEs_0s0_00Bd_
921
17.4M
                }) {
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1f_NtNtB9_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1U_9into_iter8IntoIterNtNtB9_6nibble6NibbleEINtNtNtNtB1j_4iter7sources5empty5EmptyB4c_EEs_0s0_0Bb_
Line
Count
Source
912
6.23M
                } else if let Some(child_index) = iter_1.and_then(|iter_1| {
913
6.23M
                    node.children[(usize::from(u8::from(iter_1)))
914
6.23M
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
5.38M
                            *end_key.peek().unwrap()
916
                        } else {
917
852k
                            Nibble::max()
918
                        }))]
919
6.23M
                        .iter()
920
6.23M
                        .position(|c| c.is_some())
921
6.23M
                }) {
_RNCNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1B_5slice4iter4IterNtNtB9_6nibble6NibbleEEB1s_Es_0s0_0Bb_
Line
Count
Source
912
11.2M
                } else if let Some(child_index) = iter_1.and_then(|iter_1| {
913
11.2M
                    node.children[(usize::from(u8::from(iter_1)))
914
11.2M
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
523k
                            *end_key.peek().unwrap()
916
                        } else {
917
10.7M
                            Nibble::max()
918
                        }))]
919
11.2M
                        .iter()
920
11.2M
                        .position(|c| c.is_some())
921
11.2M
                }) {
Unexecuted instantiation: _RNCNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_13TrieStructurepE11range_innerppEs_0s0_0Bb_
922
7.45M
                    let child_nibble = Nibble::try_from(
923
7.45M
                        u8::try_from(usize::from(u8::from(iter_1.unwrap())) + child_index).unwrap(),
924
                    )
925
7.45M
                    .unwrap();
926
927
7.45M
                    if iter_key_nibbles_extra == 1 && 
child_nibble2.99M
== *
end_key2.99M
.peek().unwrap() {
928
307k
                        iter_key_nibbles_extra = 0;
929
307k
                        let _ = end_key.next();
930
7.14M
                    }
931
932
7.45M
                    iter.1 = Some(Some(child_nibble));
933
                } else {
934
                    // `iter` has no child. Go to the parent.
935
10.4M
                    let node = self.nodes.get(iter.0).unwrap();
936
937
                    // End the iterator if we were about to jump out of the end bound.
938
10.4M
                    if iter_key_nibbles_extra < 2 + node.partial_key.len() {
939
2.98M
                        return None;
940
7.41M
                    }
941
942
7.41M
                    let Some((parent_node_index, parent_nibble_direction)) = node.parent else {
943
0
                        return None;
944
                    };
945
7.41M
                    iter_key_nibbles_extra -= 2;
946
7.41M
                    iter_key_nibbles_extra -= node.partial_key.len();
947
7.41M
                    let next_sibling_nibble = parent_nibble_direction.checked_add(1);
948
7.41M
                    if iter_key_nibbles_extra == 0
949
1.64M
                        && next_sibling_nibble == Some(*end_key.peek().unwrap())
950
148k
                    {
951
148k
                        let _ = end_key.next();
952
7.26M
                    } else {
953
7.26M
                        iter_key_nibbles_extra += 1;
954
7.26M
                    }
955
7.41M
                    iter = (parent_node_index, Some(next_sibling_nibble));
956
                }
957
            }
958
13.3M
        }))
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1S_9into_iter8IntoIterNtNtB7_6nibble6NibbleEINtNtNtNtB1h_4iter7sources5empty5EmptyB4a_EEs_0B9_
Line
Count
Source
863
6.32M
        either::Left(iter::from_fn(move || {
864
            loop {
865
                // `end_key` must never be empty, as otherwise the iteration has ended.
866
                // We return `None` instead of panicking, as it is legitimately possible to reach
867
                // this situation through some code paths.
868
12.3M
                let _ = end_key.peek()
?0
;
869
870
                // If `iter` points to an actual node, yield it and jump to the position right
871
                // after.
872
12.3M
                let Some(
iter_19.30M
) = iter.1 else {
873
3.09M
                    iter.1 = Some(Some(Nibble::zero()));
874
3.09M
                    if iter_key_nibbles_extra == 0 && 
*53.4k
end_key53.4k
.peek().unwrap() == Nibble::zero() {
875
0
                        let _ = end_key.next();
876
3.09M
                    } else {
877
3.09M
                        iter_key_nibbles_extra += 1;
878
3.09M
                    }
879
3.09M
                    return Some(iter.0);
880
                };
881
882
9.30M
                let node = self.nodes.get(iter.0).unwrap();
883
884
2.70M
                if let Some(child) =
885
9.30M
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
886
                {
887
                    // `child` might be after the end bound if its partial key is superior or
888
                    // equal to the `end_key`.
889
7.66M
                    for child_pk_nibble in 
self.nodes2.70M
.
get2.70M
(child).unwrap().partial_key.
iter2.70M
() {
890
7.66M
                        match child_pk_nibble.cmp(end_key.peek().unwrap()) {
891
0
                            cmp::Ordering::Greater if iter_key_nibbles_extra == 0 => return None,
892
7.18M
                            cmp::Ordering::Greater | cmp::Ordering::Less => {
893
7.18M
                                iter_key_nibbles_extra += 1;
894
7.18M
                            }
895
481k
                            cmp::Ordering::Equal if iter_key_nibbles_extra != 
0466k
=> {
896
466k
                                iter_key_nibbles_extra += 1;
897
466k
                            }
898
                            cmp::Ordering::Equal => {
899
15.1k
                                debug_assert_eq!(iter_key_nibbles_extra, 0);
900
15.1k
                                let _ = end_key.next();
901
15.1k
                                let _ = end_key.peek()
?0
;
902
                            }
903
                        }
904
                    }
905
906
2.70M
                    iter = (child, None);
907
6.60M
                } else if iter_key_nibbles_extra == 0
908
6.23M
                    || (iter_key_nibbles_extra == 1
909
5.38M
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
910
                {
911
362k
                    return None;
912
6.23M
                } else if let Some(
child_index2.60M
) = iter_1.and_then(|iter_1| {
913
                    node.children[(usize::from(u8::from(iter_1)))
914
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
                            *end_key.peek().unwrap()
916
                        } else {
917
                            Nibble::max()
918
                        }))]
919
                        .iter()
920
                        .position(|c| c.is_some())
921
                }) {
922
2.60M
                    let child_nibble = Nibble::try_from(
923
2.60M
                        u8::try_from(usize::from(u8::from(iter_1.unwrap())) + child_index).unwrap(),
924
                    )
925
2.60M
                    .unwrap();
926
927
2.60M
                    if iter_key_nibbles_extra == 1 && 
child_nibble2.56M
== *
end_key2.56M
.peek().unwrap() {
928
260k
                        iter_key_nibbles_extra = 0;
929
260k
                        let _ = end_key.next();
930
2.34M
                    }
931
932
2.60M
                    iter.1 = Some(Some(child_nibble));
933
                } else {
934
                    // `iter` has no child. Go to the parent.
935
3.63M
                    let node = self.nodes.get(iter.0).unwrap();
936
937
                    // End the iterator if we were about to jump out of the end bound.
938
3.63M
                    if iter_key_nibbles_extra < 2 + node.partial_key.len() {
939
2.86M
                        return None;
940
765k
                    }
941
942
765k
                    let Some((parent_node_index, parent_nibble_direction)) = node.parent else {
943
0
                        return None;
944
                    };
945
765k
                    iter_key_nibbles_extra -= 2;
946
765k
                    iter_key_nibbles_extra -= node.partial_key.len();
947
765k
                    let next_sibling_nibble = parent_nibble_direction.checked_add(1);
948
765k
                    if iter_key_nibbles_extra == 0
949
728k
                        && next_sibling_nibble == Some(*end_key.peek().unwrap())
950
48.8k
                    {
951
48.8k
                        let _ = end_key.next();
952
716k
                    } else {
953
716k
                        iter_key_nibbles_extra += 1;
954
716k
                    }
955
765k
                    iter = (parent_node_index, Some(next_sibling_nibble));
956
                }
957
            }
958
6.32M
        }))
_RNCINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1z_5slice4iter4IterNtNtB7_6nibble6NibbleEEB1q_Es_0B9_
Line
Count
Source
863
7.00M
        either::Left(iter::from_fn(move || {
864
            loop {
865
                // `end_key` must never be empty, as otherwise the iteration has ended.
866
                // We return `None` instead of panicking, as it is legitimately possible to reach
867
                // this situation through some code paths.
868
25.2M
                let _ = end_key.peek()
?17.2k
;
869
870
                // If `iter` points to an actual node, yield it and jump to the position right
871
                // after.
872
25.1M
                let Some(
iter_118.3M
) = iter.1 else {
873
6.80M
                    iter.1 = Some(Some(Nibble::zero()));
874
6.80M
                    if iter_key_nibbles_extra == 0 && 
*151k
end_key151k
.peek().unwrap() == Nibble::zero() {
875
14.0k
                        let _ = end_key.next();
876
6.79M
                    } else {
877
6.79M
                        iter_key_nibbles_extra += 1;
878
6.79M
                    }
879
6.80M
                    return Some(iter.0);
880
                };
881
882
18.3M
                let node = self.nodes.get(iter.0).unwrap();
883
884
6.70M
                if let Some(child) =
885
18.3M
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
886
                {
887
                    // `child` might be after the end bound if its partial key is superior or
888
                    // equal to the `end_key`.
889
6.70M
                    for 
child_pk_nibble5.20M
in self.nodes.get(child).unwrap().partial_key.iter() {
890
5.20M
                        match child_pk_nibble.cmp(end_key.peek().unwrap()) {
891
594k
                            cmp::Ordering::Greater if iter_key_nibbles_extra == 
011.8k
=> return
None11.8k
,
892
4.86M
                            cmp::Ordering::Greater | cmp::Ordering::Less => {
893
4.86M
                                iter_key_nibbles_extra += 1;
894
4.86M
                            }
895
327k
                            cmp::Ordering::Equal if iter_key_nibbles_extra != 
0324k
=> {
896
324k
                                iter_key_nibbles_extra += 1;
897
324k
                            }
898
                            cmp::Ordering::Equal => {
899
2.84k
                                debug_assert_eq!(iter_key_nibbles_extra, 0);
900
2.84k
                                let _ = end_key.next();
901
2.84k
                                let _ = end_key.peek()
?311
;
902
                            }
903
                        }
904
                    }
905
906
6.69M
                    iter = (child, None);
907
11.6M
                } else if iter_key_nibbles_extra == 0
908
11.6M
                    || (iter_key_nibbles_extra == 1
909
523k
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
910
                {
911
51.7k
                    return None;
912
11.6M
                } else if let Some(
child_index4.84M
) = iter_1.and_then(|iter_1| {
913
                    node.children[(usize::from(u8::from(iter_1)))
914
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
                            *end_key.peek().unwrap()
916
                        } else {
917
                            Nibble::max()
918
                        }))]
919
                        .iter()
920
                        .position(|c| c.is_some())
921
                }) {
922
4.84M
                    let child_nibble = Nibble::try_from(
923
4.84M
                        u8::try_from(usize::from(u8::from(iter_1.unwrap())) + child_index).unwrap(),
924
                    )
925
4.84M
                    .unwrap();
926
927
4.84M
                    if iter_key_nibbles_extra == 1 && 
child_nibble436k
== *
end_key436k
.peek().unwrap() {
928
46.8k
                        iter_key_nibbles_extra = 0;
929
46.8k
                        let _ = end_key.next();
930
4.79M
                    }
931
932
4.84M
                    iter.1 = Some(Some(child_nibble));
933
                } else {
934
                    // `iter` has no child. Go to the parent.
935
6.76M
                    let node = self.nodes.get(iter.0).unwrap();
936
937
                    // End the iterator if we were about to jump out of the end bound.
938
6.76M
                    if iter_key_nibbles_extra < 2 + node.partial_key.len() {
939
116k
                        return None;
940
6.65M
                    }
941
942
6.65M
                    let Some((parent_node_index, parent_nibble_direction)) = node.parent else {
943
0
                        return None;
944
                    };
945
6.65M
                    iter_key_nibbles_extra -= 2;
946
6.65M
                    iter_key_nibbles_extra -= node.partial_key.len();
947
6.65M
                    let next_sibling_nibble = parent_nibble_direction.checked_add(1);
948
6.65M
                    if iter_key_nibbles_extra == 0
949
915k
                        && next_sibling_nibble == Some(*end_key.peek().unwrap())
950
99.3k
                    {
951
99.3k
                        let _ = end_key.next();
952
6.55M
                    } else {
953
6.55M
                        iter_key_nibbles_extra += 1;
954
6.55M
                    }
955
6.65M
                    iter = (parent_node_index, Some(next_sibling_nibble));
956
                }
957
            }
958
7.00M
        }))
Unexecuted instantiation: _RNCINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE11range_innerppEs_0B9_
959
6.60M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB5_16TrieEntryVersionEEIB1b_NtNtB5_9trie_node17MerkleValueOutputEEE11range_innerINtNtB1Q_9into_iter8IntoIterNtNtB5_6nibble6NibbleEINtNtNtNtB1f_4iter7sources5empty5EmptyB48_EEB7_
Line
Count
Source
668
6.34M
    fn range_inner<'a>(
669
6.34M
        &'a self,
670
6.34M
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
671
6.34M
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
672
6.34M
    ) -> impl Iterator<Item = usize> {
673
        // Start by processing the end bound to obtain an "end key".
674
        // This end key is always assumed to be excluded. In other words, only keys strictly
675
        // inferior to the end key are returned. If the user provides `Included`, we modify the key
676
        // and append a dummy `0` nibble at the end of it. If the user provides `Unbounded`, we use
677
        // an infinite-sized key so that every finite key is always inferior to it.
678
        //
679
        // The algorithm later down this function will pop nibbles from the start of `end_key`.
680
        // Because `end_key` is always excluded, this iterator must always contain at least one
681
        // nibble, otherwise the iteration should have ended.
682
6.34M
        let mut end_key = match end_bound {
683
6.34M
            ops::Bound::Unbounded => either::Left(iter::repeat(Nibble::max())),
684
0
            ops::Bound::Excluded(end_key) => either::Right(end_key.chain(None)),
685
0
            ops::Bound::Included(end_key) => either::Right(end_key.chain(Some(Nibble::zero()))),
686
        }
687
6.34M
        .peekable();
688
689
        // The user passed `Excluded(&[])`. Return an empty range.
690
6.34M
        if end_key.peek().is_none() {
691
0
            return either::Right(iter::empty());
692
6.34M
        }
693
694
        // The code below creates a variable named `iter`. This `iter` represents the cursor
695
        // where the iterator is.
696
        // `iter` also contains an optional nibble. If this optional nibble is `None`, the
697
        // iteration is currently at the node itself. If it is `Some`, the iteration isn't at the
698
        // node itself but at its child of the given nibble (which potentially doesn't exist).
699
        // If it is `Some(None)`, then the iteration is right after the last children of the node.
700
        // In other words, `Some(None)` represents an overflow.
701
6.34M
        let mut iter: (usize, Option<Option<Nibble>>) = match self.root_index {
702
6.34M
            Some(idx) => (idx, None),
703
            None => {
704
                // Trie is empty. Special case.
705
0
                return either::Right(iter::empty());
706
            }
707
        };
708
709
        // Equal to `len(key(iter)) - len(key(iter) ∩ end_key)`. In other words, the number of
710
        // nibbles at the end of `iter`'s key that do not match the end key. This also includes
711
        // the optional nibble within `iter` if any.
712
6.34M
        let mut iter_key_nibbles_extra: usize = 0;
713
714
        // Transform `start_bound` into something more simple to process.
715
6.34M
        let (mut start_key, start_key_is_inclusive) = match start_bound {
716
0
            ops::Bound::Unbounded => (either::Right(iter::empty()), true),
717
6.34M
            ops::Bound::Included(k) => (either::Left(k), true),
718
0
            ops::Bound::Excluded(k) => (either::Left(k), false),
719
        };
720
721
        // Iterate down the tree, updating the variables above. At each iteration, one of the
722
        // three following is true:
723
        //
724
        // - `iter` is inferior or inferior or equal (depending on `start_key_is_inclusive`) to
725
        //   `start_key`.
726
        // - `iter` is the first node that is superior or strictly superior (depending on
727
        //   `start_key_is_inclusive`) to `start_key`.
728
        // - `iter` points to a non-existing node that is inferior/inferior-or-equal to
729
        //   `start_key`, but is right before the first node that is superior/strictly superior to
730
        //   `start_key`.
731
        //
732
        // As soon as we reach one of the last two conditions, we stop iterating, as it means
733
        // that `iter` is at the correct position.
734
        'start_search: loop {
735
7.86M
            debug_assert!(iter.1.is_none());
736
7.86M
            let iter_node = self.nodes.get(iter.0).unwrap();
737
738
            // Compare the nibbles at the front of `start_key` with the ones of `iter_node`.
739
            // Consumes the nibbles at the start of `start_key`.
740
7.86M
            let pk_compare = {
741
7.86M
                let mut result = cmp::Ordering::Equal;
742
7.86M
                for 
iter_node_pk_nibble2.97M
in iter_node.partial_key.iter() {
743
2.97M
                    match start_key
744
2.97M
                        .next()
745
2.97M
                        .map(|nibble| nibble.cmp(iter_node_pk_nibble))
746
                    {
747
                        None | Some(cmp::Ordering::Less) => {
748
351k
                            result = cmp::Ordering::Less;
749
351k
                            break;
750
                        }
751
2.33M
                        Some(cmp::Ordering::Equal) => {}
752
                        Some(cmp::Ordering::Greater) => {
753
291k
                            result = cmp::Ordering::Greater;
754
291k
                            break;
755
                        }
756
                    }
757
                }
758
7.86M
                result
759
            };
760
761
7.86M
            match pk_compare {
762
                cmp::Ordering::Less | cmp::Ordering::Equal => {
763
                    // Update the value of `iter_key_nibbles_extra` to take the current value
764
                    // of `iter` into account, as it hasn't been done yet.
765
7.57M
                    for 
iter_node_pk_nibble3.37M
in iter_node.partial_key.iter().cloned() {
766
3.37M
                        if iter_key_nibbles_extra == 0
767
72.3k
                            && iter_node_pk_nibble == *end_key.peek().unwrap()
768
                        {
769
5.54k
                            let _ = end_key.next();
770
                            // `iter` is already past the end bound. Return an empty range.
771
5.54k
                            if end_key.peek().is_none() {
772
0
                                return either::Right(iter::empty());
773
5.54k
                            }
774
3.36M
                        } else if iter_key_nibbles_extra == 0
775
66.8k
                            && iter_node_pk_nibble > *end_key.peek().unwrap()
776
                        {
777
0
                            return either::Right(iter::empty());
778
3.36M
                        } else {
779
3.36M
                            iter_key_nibbles_extra += 1;
780
3.36M
                        }
781
                    }
782
783
7.57M
                    if pk_compare == cmp::Ordering::Less {
784
                        // `iter` is strictly superior to `start_key`. `iter` is now at the
785
                        // correct position.
786
351k
                        break 'start_search;
787
7.21M
                    }
788
                }
789
                cmp::Ordering::Greater => {
790
                    // `iter` is strictly inferior to `start_key`, and all of its children will
791
                    // also be strictly inferior to `start_key`.
792
                    // Stop the search immediately after the current node in the parent.
793
291k
                    let Some((parent, parent_nibble)) = iter_node.parent else {
794
0
                        return either::Right(iter::empty());
795
                    };
796
291k
                    let next_nibble = parent_nibble.checked_add(1);
797
291k
                    if iter_key_nibbles_extra == 0 {
798
18.6k
                        return either::Right(iter::empty());
799
272k
                    }
800
272k
                    iter_key_nibbles_extra -= 1;
801
272k
                    if iter_key_nibbles_extra == 0 && 
next_nibble271k
== Some(*
end_key271k
.peek().unwrap())
802
                    {
803
18.3k
                        let _ = end_key.next();
804
                        // `iter` is already past the end bound. Return an empty range.
805
18.3k
                        if end_key.peek().is_none() {
806
0
                            return either::Right(iter::empty());
807
18.3k
                        }
808
254k
                    } else {
809
254k
                        iter_key_nibbles_extra += 1;
810
254k
                    }
811
272k
                    iter = (parent, Some(next_nibble));
812
272k
                    break 'start_search;
813
                }
814
            }
815
816
            // Remove the next nibble from `start_key` and update `iter` based on it.
817
7.21M
            if let Some(
next_nibble7.17M
) = start_key.next() {
818
7.17M
                if iter_key_nibbles_extra == 0 && 
next_nibble6.31M
== *
end_key6.31M
.peek().unwrap() {
819
395k
                    let _ = end_key.next();
820
                    // `iter` is already past the end bound. Return an empty range.
821
395k
                    if end_key.peek().is_none() {
822
0
                        return either::Right(iter::empty());
823
395k
                    }
824
6.78M
                } else if iter_key_nibbles_extra == 0 && 
next_nibble5.92M
> *
end_key5.92M
.peek().unwrap() {
825
0
                    return either::Right(iter::empty());
826
6.78M
                } else {
827
6.78M
                    iter_key_nibbles_extra += 1;
828
6.78M
                }
829
830
7.17M
                if let Some(
child1.51M
) = iter_node.children[usize::from(u8::from(next_nibble))] {
831
1.51M
                    // Update `iter` and continue searching.
832
1.51M
                    iter = (child, None);
833
1.51M
                } else {
834
                    // `iter` is strictly inferior to `start_key`.
835
5.65M
                    iter.1 = Some(Some(next_nibble));
836
5.65M
                    break 'start_search;
837
                }
838
            } else {
839
                // `iter.0` is an exact match with `start_key`. If the starting bound is
840
                // `Excluded`, we don't want to start iterating at `iter` but at `next(iter)`,
841
                // which we do by adding a zero nibble afterwards.
842
41.2k
                debug_assert!(iter.1.is_none());
843
41.2k
                if !start_key_is_inclusive {
844
0
                    iter.1 = Some(Some(Nibble::zero()));
845
0
                    if iter_key_nibbles_extra == 0 && *end_key.peek().unwrap() == Nibble::zero() {
846
0
                        let _ = end_key.next();
847
                        // `iter` is already past the end bound. Return an empty range.
848
0
                        if end_key.peek().is_none() {
849
0
                            return either::Right(iter::empty());
850
0
                        }
851
0
                    } else {
852
0
                        iter_key_nibbles_extra += 1;
853
0
                    }
854
41.2k
                }
855
856
41.2k
                break 'start_search;
857
            }
858
        }
859
860
        // `iter` is now at the correct position and we can start yielding nodes until we reach
861
        // the end. This is done in the iterator that is returned from the function.
862
863
6.32M
        either::Left(iter::from_fn(move || {
864
            loop {
865
                // `end_key` must never be empty, as otherwise the iteration has ended.
866
                // We return `None` instead of panicking, as it is legitimately possible to reach
867
                // this situation through some code paths.
868
                let _ = end_key.peek()?;
869
870
                // If `iter` points to an actual node, yield it and jump to the position right
871
                // after.
872
                let Some(iter_1) = iter.1 else {
873
                    iter.1 = Some(Some(Nibble::zero()));
874
                    if iter_key_nibbles_extra == 0 && *end_key.peek().unwrap() == Nibble::zero() {
875
                        let _ = end_key.next();
876
                    } else {
877
                        iter_key_nibbles_extra += 1;
878
                    }
879
                    return Some(iter.0);
880
                };
881
882
                let node = self.nodes.get(iter.0).unwrap();
883
884
                if let Some(child) =
885
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
886
                {
887
                    // `child` might be after the end bound if its partial key is superior or
888
                    // equal to the `end_key`.
889
                    for child_pk_nibble in self.nodes.get(child).unwrap().partial_key.iter() {
890
                        match child_pk_nibble.cmp(end_key.peek().unwrap()) {
891
                            cmp::Ordering::Greater if iter_key_nibbles_extra == 0 => return None,
892
                            cmp::Ordering::Greater | cmp::Ordering::Less => {
893
                                iter_key_nibbles_extra += 1;
894
                            }
895
                            cmp::Ordering::Equal if iter_key_nibbles_extra != 0 => {
896
                                iter_key_nibbles_extra += 1;
897
                            }
898
                            cmp::Ordering::Equal => {
899
                                debug_assert_eq!(iter_key_nibbles_extra, 0);
900
                                let _ = end_key.next();
901
                                let _ = end_key.peek()?;
902
                            }
903
                        }
904
                    }
905
906
                    iter = (child, None);
907
                } else if iter_key_nibbles_extra == 0
908
                    || (iter_key_nibbles_extra == 1
909
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
910
                {
911
                    return None;
912
                } else if let Some(child_index) = iter_1.and_then(|iter_1| {
913
                    node.children[(usize::from(u8::from(iter_1)))
914
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
                            *end_key.peek().unwrap()
916
                        } else {
917
                            Nibble::max()
918
                        }))]
919
                        .iter()
920
                        .position(|c| c.is_some())
921
                }) {
922
                    let child_nibble = Nibble::try_from(
923
                        u8::try_from(usize::from(u8::from(iter_1.unwrap())) + child_index).unwrap(),
924
                    )
925
                    .unwrap();
926
927
                    if iter_key_nibbles_extra == 1 && child_nibble == *end_key.peek().unwrap() {
928
                        iter_key_nibbles_extra = 0;
929
                        let _ = end_key.next();
930
                    }
931
932
                    iter.1 = Some(Some(child_nibble));
933
                } else {
934
                    // `iter` has no child. Go to the parent.
935
                    let node = self.nodes.get(iter.0).unwrap();
936
937
                    // End the iterator if we were about to jump out of the end bound.
938
                    if iter_key_nibbles_extra < 2 + node.partial_key.len() {
939
                        return None;
940
                    }
941
942
                    let Some((parent_node_index, parent_nibble_direction)) = node.parent else {
943
                        return None;
944
                    };
945
                    iter_key_nibbles_extra -= 2;
946
                    iter_key_nibbles_extra -= node.partial_key.len();
947
                    let next_sibling_nibble = parent_nibble_direction.checked_add(1);
948
                    if iter_key_nibbles_extra == 0
949
                        && next_sibling_nibble == Some(*end_key.peek().unwrap())
950
                    {
951
                        let _ = end_key.next();
952
                    } else {
953
                        iter_key_nibbles_extra += 1;
954
                    }
955
                    iter = (parent_node_index, Some(next_sibling_nibble));
956
                }
957
            }
958
        }))
959
6.34M
    }
_RINvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB3_13TrieStructureuE11range_innerINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1x_5slice4iter4IterNtNtB5_6nibble6NibbleEEB1o_EB7_
Line
Count
Source
668
262k
    fn range_inner<'a>(
669
262k
        &'a self,
670
262k
        start_bound: ops::Bound<impl Iterator<Item = Nibble>>,
671
262k
        end_bound: ops::Bound<impl Iterator<Item = Nibble> + 'a>,
672
262k
    ) -> impl Iterator<Item = usize> {
673
        // Start by processing the end bound to obtain an "end key".
674
        // This end key is always assumed to be excluded. In other words, only keys strictly
675
        // inferior to the end key are returned. If the user provides `Included`, we modify the key
676
        // and append a dummy `0` nibble at the end of it. If the user provides `Unbounded`, we use
677
        // an infinite-sized key so that every finite key is always inferior to it.
678
        //
679
        // The algorithm later down this function will pop nibbles from the start of `end_key`.
680
        // Because `end_key` is always excluded, this iterator must always contain at least one
681
        // nibble, otherwise the iteration should have ended.
682
262k
        let mut end_key = match end_bound {
683
87.4k
            ops::Bound::Unbounded => either::Left(iter::repeat(Nibble::max())),
684
87.3k
            ops::Bound::Excluded(end_key) => either::Right(end_key.chain(None)),
685
87.3k
            ops::Bound::Included(end_key) => either::Right(end_key.chain(Some(Nibble::zero()))),
686
        }
687
262k
        .peekable();
688
689
        // The user passed `Excluded(&[])`. Return an empty range.
690
262k
        if end_key.peek().is_none() {
691
14.5k
            return either::Right(iter::empty());
692
247k
        }
693
694
        // The code below creates a variable named `iter`. This `iter` represents the cursor
695
        // where the iterator is.
696
        // `iter` also contains an optional nibble. If this optional nibble is `None`, the
697
        // iteration is currently at the node itself. If it is `Some`, the iteration isn't at the
698
        // node itself but at its child of the given nibble (which potentially doesn't exist).
699
        // If it is `Some(None)`, then the iteration is right after the last children of the node.
700
        // In other words, `Some(None)` represents an overflow.
701
247k
        let 
mut iter247k
:
(usize, Option<Option<Nibble>>)247k
= match self.root_index {
702
247k
            Some(idx) => (idx, None),
703
            None => {
704
                // Trie is empty. Special case.
705
363
                return either::Right(iter::empty());
706
            }
707
        };
708
709
        // Equal to `len(key(iter)) - len(key(iter) ∩ end_key)`. In other words, the number of
710
        // nibbles at the end of `iter`'s key that do not match the end key. This also includes
711
        // the optional nibble within `iter` if any.
712
247k
        let mut iter_key_nibbles_extra: usize = 0;
713
714
        // Transform `start_bound` into something more simple to process.
715
247k
        let (mut start_key, start_key_is_inclusive) = match start_bound {
716
82.7k
            ops::Bound::Unbounded => (either::Right(iter::empty()), true),
717
82.0k
            ops::Bound::Included(k) => (either::Left(k), true),
718
82.4k
            ops::Bound::Excluded(k) => (either::Left(k), false),
719
        };
720
721
        // Iterate down the tree, updating the variables above. At each iteration, one of the
722
        // three following is true:
723
        //
724
        // - `iter` is inferior or inferior or equal (depending on `start_key_is_inclusive`) to
725
        //   `start_key`.
726
        // - `iter` is the first node that is superior or strictly superior (depending on
727
        //   `start_key_is_inclusive`) to `start_key`.
728
        // - `iter` points to a non-existing node that is inferior/inferior-or-equal to
729
        //   `start_key`, but is right before the first node that is superior/strictly superior to
730
        //   `start_key`.
731
        //
732
        // As soon as we reach one of the last two conditions, we stop iterating, as it means
733
        // that `iter` is at the correct position.
734
        'start_search: loop {
735
296k
            debug_assert!(iter.1.is_none());
736
296k
            let iter_node = self.nodes.get(iter.0).unwrap();
737
738
            // Compare the nibbles at the front of `start_key` with the ones of `iter_node`.
739
            // Consumes the nibbles at the start of `start_key`.
740
296k
            let pk_compare = {
741
296k
                let mut result = cmp::Ordering::Equal;
742
296k
                for 
iter_node_pk_nibble23.7k
in iter_node.partial_key.iter() {
743
23.7k
                    match start_key
744
23.7k
                        .next()
745
23.7k
                        .map(|nibble| nibble.cmp(iter_node_pk_nibble))
746
                    {
747
                        None | Some(cmp::Ordering::Less) => {
748
13.9k
                            result = cmp::Ordering::Less;
749
13.9k
                            break;
750
                        }
751
1.11k
                        Some(cmp::Ordering::Equal) => {}
752
                        Some(cmp::Ordering::Greater) => {
753
8.70k
                            result = cmp::Ordering::Greater;
754
8.70k
                            break;
755
                        }
756
                    }
757
                }
758
296k
                result
759
            };
760
761
296k
            match pk_compare {
762
                cmp::Ordering::Less | cmp::Ordering::Equal => {
763
                    // Update the value of `iter_key_nibbles_extra` to take the current value
764
                    // of `iter` into account, as it hasn't been done yet.
765
287k
                    for 
iter_node_pk_nibble21.2k
in iter_node.partial_key.iter().cloned() {
766
21.2k
                        if iter_key_nibbles_extra == 0
767
2.35k
                            && iter_node_pk_nibble == *end_key.peek().unwrap()
768
                        {
769
174
                            let _ = end_key.next();
770
                            // `iter` is already past the end bound. Return an empty range.
771
174
                            if end_key.peek().is_none() {
772
13
                                return either::Right(iter::empty());
773
161
                            }
774
21.1k
                        } else if iter_key_nibbles_extra == 0
775
2.17k
                            && iter_node_pk_nibble > *end_key.peek().unwrap()
776
                        {
777
855
                            return either::Right(iter::empty());
778
20.2k
                        } else {
779
20.2k
                            iter_key_nibbles_extra += 1;
780
20.2k
                        }
781
                    }
782
783
286k
                    if pk_compare == cmp::Ordering::Less {
784
                        // `iter` is strictly superior to `start_key`. `iter` is now at the
785
                        // correct position.
786
13.1k
                        break 'start_search;
787
273k
                    }
788
                }
789
                cmp::Ordering::Greater => {
790
                    // `iter` is strictly inferior to `start_key`, and all of its children will
791
                    // also be strictly inferior to `start_key`.
792
                    // Stop the search immediately after the current node in the parent.
793
8.70k
                    let Some((
parent8.30k
,
parent_nibble8.30k
)) = iter_node.parent else {
794
401
                        return either::Right(iter::empty());
795
                    };
796
8.30k
                    let next_nibble = parent_nibble.checked_add(1);
797
8.30k
                    if iter_key_nibbles_extra == 0 {
798
683
                        return either::Right(iter::empty());
799
7.61k
                    }
800
7.61k
                    iter_key_nibbles_extra -= 1;
801
7.61k
                    if iter_key_nibbles_extra == 0 && 
next_nibble7.06k
== Some(*
end_key7.06k
.peek().unwrap())
802
                    {
803
619
                        let _ = end_key.next();
804
                        // `iter` is already past the end bound. Return an empty range.
805
619
                        if end_key.peek().is_none() {
806
36
                            return either::Right(iter::empty());
807
583
                        }
808
7.00k
                    } else {
809
7.00k
                        iter_key_nibbles_extra += 1;
810
7.00k
                    }
811
7.58k
                    iter = (parent, Some(next_nibble));
812
7.58k
                    break 'start_search;
813
                }
814
            }
815
816
            // Remove the next nibble from `start_key` and update `iter` based on it.
817
273k
            if let Some(
next_nibble158k
) = start_key.next() {
818
158k
                if iter_key_nibbles_extra == 0 && 
next_nibble137k
== *
end_key137k
.peek().unwrap() {
819
8.59k
                    let _ = end_key.next();
820
                    // `iter` is already past the end bound. Return an empty range.
821
8.59k
                    if end_key.peek().is_none() {
822
991
                        return either::Right(iter::empty());
823
7.60k
                    }
824
149k
                } else if iter_key_nibbles_extra == 0 && 
next_nibble129k
> *
end_key129k
.peek().unwrap() {
825
45.5k
                    return either::Right(iter::empty());
826
104k
                } else {
827
104k
                    iter_key_nibbles_extra += 1;
828
104k
                }
829
830
111k
                if let Some(
child48.9k
) = iter_node.children[usize::from(u8::from(next_nibble))] {
831
48.9k
                    // Update `iter` and continue searching.
832
48.9k
                    iter = (child, None);
833
48.9k
                } else {
834
                    // `iter` is strictly inferior to `start_key`.
835
62.8k
                    iter.1 = Some(Some(next_nibble));
836
62.8k
                    break 'start_search;
837
                }
838
            } else {
839
                // `iter.0` is an exact match with `start_key`. If the starting bound is
840
                // `Excluded`, we don't want to start iterating at `iter` but at `next(iter)`,
841
                // which we do by adding a zero nibble afterwards.
842
115k
                debug_assert!(iter.1.is_none());
843
115k
                if !start_key_is_inclusive {
844
16.4k
                    iter.1 = Some(Some(Nibble::zero()));
845
16.4k
                    if iter_key_nibbles_extra == 0 && 
*13.7k
end_key13.7k
.peek().unwrap() == Nibble::zero() {
846
1.37k
                        let _ = end_key.next();
847
                        // `iter` is already past the end bound. Return an empty range.
848
1.37k
                        if end_key.peek().is_none() {
849
875
                            return either::Right(iter::empty());
850
498
                        }
851
15.0k
                    } else {
852
15.0k
                        iter_key_nibbles_extra += 1;
853
15.0k
                    }
854
98.7k
                }
855
856
114k
                break 'start_search;
857
            }
858
        }
859
860
        // `iter` is now at the correct position and we can start yielding nodes until we reach
861
        // the end. This is done in the iterator that is returned from the function.
862
863
197k
        either::Left(iter::from_fn(move || {
864
            loop {
865
                // `end_key` must never be empty, as otherwise the iteration has ended.
866
                // We return `None` instead of panicking, as it is legitimately possible to reach
867
                // this situation through some code paths.
868
                let _ = end_key.peek()?;
869
870
                // If `iter` points to an actual node, yield it and jump to the position right
871
                // after.
872
                let Some(iter_1) = iter.1 else {
873
                    iter.1 = Some(Some(Nibble::zero()));
874
                    if iter_key_nibbles_extra == 0 && *end_key.peek().unwrap() == Nibble::zero() {
875
                        let _ = end_key.next();
876
                    } else {
877
                        iter_key_nibbles_extra += 1;
878
                    }
879
                    return Some(iter.0);
880
                };
881
882
                let node = self.nodes.get(iter.0).unwrap();
883
884
                if let Some(child) =
885
                    iter_1.and_then(|iter_1| node.children[usize::from(u8::from(iter_1))])
886
                {
887
                    // `child` might be after the end bound if its partial key is superior or
888
                    // equal to the `end_key`.
889
                    for child_pk_nibble in self.nodes.get(child).unwrap().partial_key.iter() {
890
                        match child_pk_nibble.cmp(end_key.peek().unwrap()) {
891
                            cmp::Ordering::Greater if iter_key_nibbles_extra == 0 => return None,
892
                            cmp::Ordering::Greater | cmp::Ordering::Less => {
893
                                iter_key_nibbles_extra += 1;
894
                            }
895
                            cmp::Ordering::Equal if iter_key_nibbles_extra != 0 => {
896
                                iter_key_nibbles_extra += 1;
897
                            }
898
                            cmp::Ordering::Equal => {
899
                                debug_assert_eq!(iter_key_nibbles_extra, 0);
900
                                let _ = end_key.next();
901
                                let _ = end_key.peek()?;
902
                            }
903
                        }
904
                    }
905
906
                    iter = (child, None);
907
                } else if iter_key_nibbles_extra == 0
908
                    || (iter_key_nibbles_extra == 1
909
                        && iter_1.map_or(true, |iter_1| iter_1 > *end_key.peek().unwrap()))
910
                {
911
                    return None;
912
                } else if let Some(child_index) = iter_1.and_then(|iter_1| {
913
                    node.children[(usize::from(u8::from(iter_1)))
914
                        ..=usize::from(u8::from(if iter_key_nibbles_extra == 1 {
915
                            *end_key.peek().unwrap()
916
                        } else {
917
                            Nibble::max()
918
                        }))]
919
                        .iter()
920
                        .position(|c| c.is_some())
921
                }) {
922
                    let child_nibble = Nibble::try_from(
923
                        u8::try_from(usize::from(u8::from(iter_1.unwrap())) + child_index).unwrap(),
924
                    )
925
                    .unwrap();
926
927
                    if iter_key_nibbles_extra == 1 && child_nibble == *end_key.peek().unwrap() {
928
                        iter_key_nibbles_extra = 0;
929
                        let _ = end_key.next();
930
                    }
931
932
                    iter.1 = Some(Some(child_nibble));
933
                } else {
934
                    // `iter` has no child. Go to the parent.
935
                    let node = self.nodes.get(iter.0).unwrap();
936
937
                    // End the iterator if we were about to jump out of the end bound.
938
                    if iter_key_nibbles_extra < 2 + node.partial_key.len() {
939
                        return None;
940
                    }
941
942
                    let Some((parent_node_index, parent_nibble_direction)) = node.parent else {
943
                        return None;
944
                    };
945
                    iter_key_nibbles_extra -= 2;
946
                    iter_key_nibbles_extra -= node.partial_key.len();
947
                    let next_sibling_nibble = parent_nibble_direction.checked_add(1);
948
                    if iter_key_nibbles_extra == 0
949
                        && next_sibling_nibble == Some(*end_key.peek().unwrap())
950
                    {
951
                        let _ = end_key.next();
952
                    } else {
953
                        iter_key_nibbles_extra += 1;
954
                    }
955
                    iter = (parent_node_index, Some(next_sibling_nibble));
956
                }
957
            }
958
        }))
959
262k
    }
Unexecuted instantiation: _RINvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB3_13TrieStructurepE11range_innerppEB7_
960
961
    /// Iterates over all nodes of the trie in a lexicographic order.
962
3.33M
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
9.70M
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
9.70M
            if let Some(
first_child3.30M
) = tree
965
9.70M
                .nodes
966
9.70M
                .get(node_index)
967
9.70M
                .unwrap()
968
9.70M
                .children
969
9.70M
                .iter()
970
9.70M
                .find_map(|c| *c)
971
            {
972
3.30M
                return Some(first_child);
973
6.40M
            }
974
975
6.40M
            if let Some(
next_sibling3.06M
) = tree.next_sibling(node_index) {
976
3.06M
                return Some(next_sibling);
977
3.33M
            }
978
979
3.33M
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
5.49M
            while let Some(
idx2.71M
) = return_value {
981
2.71M
                if let Some(
next_sibling551k
) = tree.next_sibling(idx) {
982
551k
                    return Some(next_sibling);
983
2.16M
                }
984
2.16M
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
2.78M
            return_value
987
9.70M
        }
_RINvNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB26_NtNtB7_9trie_node17MerkleValueOutputEEEB9_
Line
Count
Source
963
4.74M
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
4.74M
            if let Some(
first_child1.66M
) = tree
965
4.74M
                .nodes
966
4.74M
                .get(node_index)
967
4.74M
                .unwrap()
968
4.74M
                .children
969
4.74M
                .iter()
970
4.74M
                .find_map(|c| *c)
971
            {
972
1.66M
                return Some(first_child);
973
3.08M
            }
974
975
3.08M
            if let Some(
next_sibling1.29M
) = tree.next_sibling(node_index) {
976
1.29M
                return Some(next_sibling);
977
1.78M
            }
978
979
1.78M
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
2.99M
            while let Some(
idx1.26M
) = return_value {
981
1.26M
                if let Some(
next_sibling58.6k
) = tree.next_sibling(idx) {
982
58.6k
                    return Some(next_sibling);
983
1.20M
                }
984
1.20M
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
1.72M
            return_value
987
4.74M
        }
_RINvNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB26_NtNtB7_9trie_node17MerkleValueOutputEEEB9_
Line
Count
Source
963
939k
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
939k
            if let Some(
first_child237k
) = tree
965
939k
                .nodes
966
939k
                .get(node_index)
967
939k
                .unwrap()
968
939k
                .children
969
939k
                .iter()
970
939k
                .find_map(|c| *c)
971
            {
972
237k
                return Some(first_child);
973
702k
            }
974
975
702k
            if let Some(
next_sibling468k
) = tree.next_sibling(node_index) {
976
468k
                return Some(next_sibling);
977
233k
            }
978
979
233k
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
368k
            while let Some(
idx237k
) = return_value {
981
237k
                if let Some(
next_sibling102k
) = tree.next_sibling(idx) {
982
102k
                    return Some(next_sibling);
983
134k
                }
984
134k
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
131k
            return_value
987
939k
        }
_RINvNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextuEB9_
Line
Count
Source
963
4.01M
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
4.01M
            if let Some(
first_child1.39M
) = tree
965
4.01M
                .nodes
966
4.01M
                .get(node_index)
967
4.01M
                .unwrap()
968
4.01M
                .children
969
4.01M
                .iter()
970
4.01M
                .find_map(|c| *c)
971
            {
972
1.39M
                return Some(first_child);
973
2.61M
            }
974
975
2.61M
            if let Some(
next_sibling1.30M
) = tree.next_sibling(node_index) {
976
1.30M
                return Some(next_sibling);
977
1.31M
            }
978
979
1.31M
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
2.13M
            while let Some(
idx1.20M
) = return_value {
981
1.20M
                if let Some(
next_sibling389k
) = tree.next_sibling(idx) {
982
389k
                    return Some(next_sibling);
983
818k
                }
984
818k
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
928k
            return_value
987
4.01M
        }
Unexecuted instantiation: _RINvNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB26_NtNtB7_9trie_node17MerkleValueOutputEEECscoAnRPySggw_6author
Unexecuted instantiation: _RINvNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextpEB9_
_RINvNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB26_NtNtB7_9trie_node17MerkleValueOutputEEECsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
963
96
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
96
            if let Some(
first_child26
) = tree
965
96
                .nodes
966
96
                .get(node_index)
967
96
                .unwrap()
968
96
                .children
969
96
                .iter()
970
96
                .find_map(|c| *c)
971
            {
972
26
                return Some(first_child);
973
70
            }
974
975
70
            if let Some(
next_sibling46
) = tree.next_sibling(node_index) {
976
46
                return Some(next_sibling);
977
24
            }
978
979
24
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
28
            while let Some(
idx26
) = return_value {
981
26
                if let Some(
next_sibling22
) = tree.next_sibling(idx) {
982
22
                    return Some(next_sibling);
983
4
                }
984
4
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
2
            return_value
987
96
        }
_RINvNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructurepE30all_node_lexicographic_ordered19ancestry_order_nextTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB26_NtNtB7_9trie_node17MerkleValueOutputEEECs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
963
912
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
912
            if let Some(
first_child247
) = tree
965
912
                .nodes
966
912
                .get(node_index)
967
912
                .unwrap()
968
912
                .children
969
912
                .iter()
970
912
                .find_map(|c| *c)
971
            {
972
247
                return Some(first_child);
973
665
            }
974
975
665
            if let Some(
next_sibling437
) = tree.next_sibling(node_index) {
976
437
                return Some(next_sibling);
977
228
            }
978
979
228
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
266
            while let Some(
idx247
) = return_value {
981
247
                if let Some(
next_sibling209
) = tree.next_sibling(idx) {
982
209
                    return Some(next_sibling);
983
38
                }
984
38
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
19
            return_value
987
912
        }
988
989
9.70M
        
iter::successors3.33M
(
self.root_index3.33M
, move |n| ancestry_order_next(self, *n))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB6_16TrieEntryVersionEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_ordered0B8_
Line
Count
Source
989
939k
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_ordered0B8_
Line
Count
Source
989
4.74M
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureuE30all_node_lexicographic_ordered0B8_
Line
Count
Source
989
4.01M
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_ordered0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructurepE30all_node_lexicographic_ordered0B8_
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_ordered0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
989
96
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_ordered0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
989
912
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
3.33M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_orderedB6_
Line
Count
Source
962
131k
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
            if let Some(first_child) = tree
965
                .nodes
966
                .get(node_index)
967
                .unwrap()
968
                .children
969
                .iter()
970
                .find_map(|c| *c)
971
            {
972
                return Some(first_child);
973
            }
974
975
            if let Some(next_sibling) = tree.next_sibling(node_index) {
976
                return Some(next_sibling);
977
            }
978
979
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
            while let Some(idx) = return_value {
981
                if let Some(next_sibling) = tree.next_sibling(idx) {
982
                    return Some(next_sibling);
983
                }
984
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
            return_value
987
        }
988
989
131k
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
131k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_orderedB6_
Line
Count
Source
962
2.09M
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
            if let Some(first_child) = tree
965
                .nodes
966
                .get(node_index)
967
                .unwrap()
968
                .children
969
                .iter()
970
                .find_map(|c| *c)
971
            {
972
                return Some(first_child);
973
            }
974
975
            if let Some(next_sibling) = tree.next_sibling(node_index) {
976
                return Some(next_sibling);
977
            }
978
979
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
            while let Some(idx) = return_value {
981
                if let Some(next_sibling) = tree.next_sibling(idx) {
982
                    return Some(next_sibling);
983
                }
984
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
            return_value
987
        }
988
989
2.09M
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
2.09M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE30all_node_lexicographic_orderedB6_
Line
Count
Source
962
1.10M
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
            if let Some(first_child) = tree
965
                .nodes
966
                .get(node_index)
967
                .unwrap()
968
                .children
969
                .iter()
970
                .find_map(|c| *c)
971
            {
972
                return Some(first_child);
973
            }
974
975
            if let Some(next_sibling) = tree.next_sibling(node_index) {
976
                return Some(next_sibling);
977
            }
978
979
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
            while let Some(idx) = return_value {
981
                if let Some(next_sibling) = tree.next_sibling(idx) {
982
                    return Some(next_sibling);
983
                }
984
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
            return_value
987
        }
988
989
1.10M
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
1.10M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_orderedCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE30all_node_lexicographic_orderedB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_orderedCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
962
2
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
            if let Some(first_child) = tree
965
                .nodes
966
                .get(node_index)
967
                .unwrap()
968
                .children
969
                .iter()
970
                .find_map(|c| *c)
971
            {
972
                return Some(first_child);
973
            }
974
975
            if let Some(next_sibling) = tree.next_sibling(node_index) {
976
                return Some(next_sibling);
977
            }
978
979
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
            while let Some(idx) = return_value {
981
                if let Some(next_sibling) = tree.next_sibling(idx) {
982
                    return Some(next_sibling);
983
                }
984
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
            return_value
987
        }
988
989
2
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
2
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE30all_node_lexicographic_orderedCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
962
19
    fn all_node_lexicographic_ordered(&self) -> impl Iterator<Item = usize> {
963
        fn ancestry_order_next<TUd>(tree: &TrieStructure<TUd>, node_index: usize) -> Option<usize> {
964
            if let Some(first_child) = tree
965
                .nodes
966
                .get(node_index)
967
                .unwrap()
968
                .children
969
                .iter()
970
                .find_map(|c| *c)
971
            {
972
                return Some(first_child);
973
            }
974
975
            if let Some(next_sibling) = tree.next_sibling(node_index) {
976
                return Some(next_sibling);
977
            }
978
979
            let mut return_value = tree.nodes[node_index].parent.map(|(i, _)| i);
980
            while let Some(idx) = return_value {
981
                if let Some(next_sibling) = tree.next_sibling(idx) {
982
                    return Some(next_sibling);
983
                }
984
                return_value = tree.nodes[idx].parent.map(|(i, _)| i);
985
            }
986
            return_value
987
        }
988
989
19
        iter::successors(self.root_index, move |n| ancestry_order_next(self, *n))
990
19
    }
991
992
    /// Returns the [`NodeAccess`] of the node at the given index, or `None` if no such node
993
    /// exists.
994
    ///
995
    /// # Context
996
    ///
997
    /// Each node inserted in the trie is placed in the underlying data structure at a specific
998
    /// [`NodeIndex`] that never changes until the node is removed from the trie.
999
    ///
1000
    /// This [`NodeIndex`] can be retrieved by calling [`NodeAccess::node_index`],
1001
    /// [`StorageNodeAccess::node_index`] or [`BranchNodeAccess::node_index`]. The same node can
1002
    /// later be accessed again by calling [`TrieStructure::node_by_index`].
1003
    ///
1004
    /// A [`NodeIndex`] value can be reused after its previous node has been removed.
1005
    ///
1006
    /// # Examples
1007
    ///
1008
    /// ```
1009
    /// use smoldot::trie::{self, trie_structure};
1010
    ///
1011
    /// let mut trie = trie_structure::TrieStructure::new();
1012
    ///
1013
    /// // Insert an example node.
1014
    /// let inserted_node = trie
1015
    ///     .node(trie::bytes_to_nibbles(b"foo".iter().cloned()))
1016
    ///     .into_vacant()
1017
    ///     .unwrap()
1018
    ///     .insert_storage_value()
1019
    ///     .insert(12, 80);
1020
    /// let node_index = inserted_node.node_index();
1021
    /// drop(inserted_node);  // Drops the borrow to this node.
1022
    ///
1023
    /// // At this point, no borrow of the `trie` object exists anymore.
1024
    ///
1025
    /// // Later, the same node can be accessed again.
1026
    /// let mut node = trie.node_by_index(node_index).unwrap();
1027
    /// assert_eq!(*node.user_data(), 12);
1028
    /// ```
1029
1.04M
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
1.04M
        self.node_by_index_inner(node_index.0)
1031
1.04M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13node_by_indexB6_
Line
Count
Source
1029
29.2k
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
29.2k
        self.node_by_index_inner(node_index.0)
1031
29.2k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_by_indexB6_
Line
Count
Source
1029
485k
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
485k
        self.node_by_index_inner(node_index.0)
1031
485k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_by_indexB6_
Line
Count
Source
1029
5.30k
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
5.30k
        self.node_by_index_inner(node_index.0)
1031
5.30k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE13node_by_indexB6_
Line
Count
Source
1029
526k
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
526k
        self.node_by_index_inner(node_index.0)
1031
526k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_by_indexCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13node_by_indexB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_by_indexCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1029
192
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
192
        self.node_by_index_inner(node_index.0)
1031
192
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_by_indexCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1029
1.82k
    pub fn node_by_index(&mut self, node_index: NodeIndex) -> Option<NodeAccess<TUd>> {
1030
1.82k
        self.node_by_index_inner(node_index.0)
1031
1.82k
    }
1032
1033
    /// Internal function. Returns the [`NodeAccess`] of the node at the given index.
1034
1.56M
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
1.56M
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
1.30M
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
1.30M
                trie: self,
1038
1.30M
                node_index,
1039
1.30M
            }))
1040
        } else {
1041
251k
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
251k
                trie: self,
1043
251k
                node_index,
1044
251k
            }))
1045
        }
1046
1.56M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE19node_by_index_innerB6_
Line
Count
Source
1034
67.9k
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
67.9k
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
67.9k
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
67.9k
                trie: self,
1038
67.9k
                node_index,
1039
67.9k
            }))
1040
        } else {
1041
0
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
0
                trie: self,
1043
0
                node_index,
1044
0
            }))
1045
        }
1046
67.9k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE19node_by_index_innerB6_
Line
Count
Source
1034
899k
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
899k
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
767k
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
767k
                trie: self,
1038
767k
                node_index,
1039
767k
            }))
1040
        } else {
1041
131k
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
131k
                trie: self,
1043
131k
                node_index,
1044
131k
            }))
1045
        }
1046
899k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE19node_by_index_innerB6_
Line
Count
Source
1034
8.56k
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
8.56k
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
8.24k
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
8.24k
                trie: self,
1038
8.24k
                node_index,
1039
8.24k
            }))
1040
        } else {
1041
320
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
320
                trie: self,
1043
320
                node_index,
1044
320
            }))
1045
        }
1046
8.56k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE19node_by_index_innerB6_
Line
Count
Source
1034
580k
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
580k
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
462k
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
462k
                trie: self,
1038
462k
                node_index,
1039
462k
            }))
1040
        } else {
1041
118k
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
118k
                trie: self,
1043
118k
                node_index,
1044
118k
            }))
1045
        }
1046
580k
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE19node_by_index_innerCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE19node_by_index_innerB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE19node_by_index_innerCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1034
380
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
380
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
280
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
280
                trie: self,
1038
280
                node_index,
1039
280
            }))
1040
        } else {
1041
100
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
100
                trie: self,
1043
100
                node_index,
1044
100
            }))
1045
        }
1046
380
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE19node_by_index_innerCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1034
3.61k
    fn node_by_index_inner(&mut self, node_index: usize) -> Option<NodeAccess<TUd>> {
1035
3.61k
        if self.nodes.get(node_index)
?0
.has_storage_value {
1036
2.66k
            Some(NodeAccess::Storage(StorageNodeAccess {
1037
2.66k
                trie: self,
1038
2.66k
                node_index,
1039
2.66k
            }))
1040
        } else {
1041
950
            Some(NodeAccess::Branch(BranchNodeAccess {
1042
950
                trie: self,
1043
950
                node_index,
1044
950
            }))
1045
        }
1046
3.61k
    }
1047
1048
    /// Returns the key of the node at the given index, or `None` if no such node exists.
1049
    ///
1050
    /// This method is a shortcut for [`TrieStructure::node_by_index`] followed with
1051
    /// [`NodeAccess::full_key`].
1052
11.1M
    pub fn node_full_key_by_index(
1053
11.1M
        &self,
1054
11.1M
        node_index: NodeIndex,
1055
11.1M
    ) -> Option<impl Iterator<Item = Nibble>> {
1056
11.1M
        if !self.nodes.contains(node_index.0) {
1057
0
            return None;
1058
11.1M
        }
1059
1060
11.1M
        Some(self.node_full_key(node_index.0))
1061
11.1M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE22node_full_key_by_indexB6_
Line
Count
Source
1052
4.71M
    pub fn node_full_key_by_index(
1053
4.71M
        &self,
1054
4.71M
        node_index: NodeIndex,
1055
4.71M
    ) -> Option<impl Iterator<Item = Nibble>> {
1056
4.71M
        if !self.nodes.contains(node_index.0) {
1057
0
            return None;
1058
4.71M
        }
1059
1060
4.71M
        Some(self.node_full_key(node_index.0))
1061
4.71M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE22node_full_key_by_indexB6_
Line
Count
Source
1052
3.64M
    pub fn node_full_key_by_index(
1053
3.64M
        &self,
1054
3.64M
        node_index: NodeIndex,
1055
3.64M
    ) -> Option<impl Iterator<Item = Nibble>> {
1056
3.64M
        if !self.nodes.contains(node_index.0) {
1057
0
            return None;
1058
3.64M
        }
1059
1060
3.64M
        Some(self.node_full_key(node_index.0))
1061
3.64M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE22node_full_key_by_indexB6_
Line
Count
Source
1052
2.82M
    pub fn node_full_key_by_index(
1053
2.82M
        &self,
1054
2.82M
        node_index: NodeIndex,
1055
2.82M
    ) -> Option<impl Iterator<Item = Nibble>> {
1056
2.82M
        if !self.nodes.contains(node_index.0) {
1057
0
            return None;
1058
2.82M
        }
1059
1060
2.82M
        Some(self.node_full_key(node_index.0))
1061
2.82M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructurepE22node_full_key_by_indexB6_
1062
1063
    /// Returns the full key of the node with the given index.
1064
    ///
1065
    /// # Panic
1066
    ///
1067
    /// Panics if `target` is not a valid index.
1068
12.4M
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
12.4M
        self.node_path(target)
1070
12.4M
            .chain(iter::once(target))
1071
23.2M
            .
flat_map12.4M
(move |n| {
1072
23.2M
                let node = self.nodes.get(n).unwrap();
1073
23.2M
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
23.2M
                let partial_key = node.partial_key.iter().cloned();
1075
23.2M
                child_index.chain(partial_key)
1076
23.2M
            })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE13node_full_key0B8_
Line
Count
Source
1071
34.6k
            .flat_map(move |n| {
1072
34.6k
                let node = self.nodes.get(n).unwrap();
1073
34.6k
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
34.6k
                let partial_key = node.partial_key.iter().cloned();
1075
34.6k
                child_index.chain(partial_key)
1076
34.6k
            })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE13node_full_key0B8_
Line
Count
Source
1071
7.56M
            .flat_map(move |n| {
1072
7.56M
                let node = self.nodes.get(n).unwrap();
1073
7.56M
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
7.56M
                let partial_key = node.partial_key.iter().cloned();
1075
7.56M
                child_index.chain(partial_key)
1076
7.56M
            })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB6_16TrieEntryVersionEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE13node_full_key0B8_
Line
Count
Source
1071
7.65M
            .flat_map(move |n| {
1072
7.65M
                let node = self.nodes.get(n).unwrap();
1073
7.65M
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
7.65M
                let partial_key = node.partial_key.iter().cloned();
1075
7.65M
                child_index.chain(partial_key)
1076
7.65M
            })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureuE13node_full_key0B8_
Line
Count
Source
1071
8.00M
            .flat_map(move |n| {
1072
8.00M
                let node = self.nodes.get(n).unwrap();
1073
8.00M
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
8.00M
                let partial_key = node.partial_key.iter().cloned();
1075
8.00M
                child_index.chain(partial_key)
1076
8.00M
            })
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE13node_full_key0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE13node_full_key0B8_
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE13node_full_key0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1071
132
            .flat_map(move |n| {
1072
132
                let node = self.nodes.get(n).unwrap();
1073
132
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
132
                let partial_key = node.partial_key.iter().cloned();
1075
132
                child_index.chain(partial_key)
1076
132
            })
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE13node_full_key0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1071
1.25k
            .flat_map(move |n| {
1072
1.25k
                let node = self.nodes.get(n).unwrap();
1073
1.25k
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
1.25k
                let partial_key = node.partial_key.iter().cloned();
1075
1.25k
                child_index.chain(partial_key)
1076
1.25k
            })
1077
12.4M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13node_full_keyB6_
Line
Count
Source
1068
25.6k
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
25.6k
        self.node_path(target)
1070
25.6k
            .chain(iter::once(target))
1071
25.6k
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
25.6k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_full_keyB6_
Line
Count
Source
1068
4.71M
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
4.71M
        self.node_path(target)
1070
4.71M
            .chain(iter::once(target))
1071
4.71M
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
4.71M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_full_keyB6_
Line
Count
Source
1068
3.93M
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
3.93M
        self.node_path(target)
1070
3.93M
            .chain(iter::once(target))
1071
3.93M
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
3.93M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE13node_full_keyB6_
Line
Count
Source
1068
3.79M
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
3.79M
        self.node_path(target)
1070
3.79M
            .chain(iter::once(target))
1071
3.79M
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
3.79M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_full_keyCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE13node_full_keyB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_full_keyCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1068
66
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
66
        self.node_path(target)
1070
66
            .chain(iter::once(target))
1071
66
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
66
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE13node_full_keyCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1068
627
    fn node_full_key(&self, target: usize) -> impl Iterator<Item = Nibble> {
1069
627
        self.node_path(target)
1070
627
            .chain(iter::once(target))
1071
627
            .flat_map(move |n| {
1072
                let node = self.nodes.get(n).unwrap();
1073
                let child_index = node.parent.into_iter().map(|p| p.1);
1074
                let partial_key = node.partial_key.iter().cloned();
1075
                child_index.chain(partial_key)
1076
            })
1077
627
    }
1078
1079
    /// Returns the indices of the nodes to traverse to reach `target`. The returned iterator
1080
    /// does *not* include `target`. In other words, if `target` is the root node, this returns
1081
    /// an empty iterator.
1082
    ///
1083
    /// # Panic
1084
    ///
1085
    /// Panics if `target` is not a valid index.
1086
12.4M
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
12.4M
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
23.2M
        
iter::successors12.4M
(
Some(usize::MAX)12.4M
, move |&current| {
1094
23.2M
            self.reverse_node_path(target)
1095
24.7M
                .
take_while23.2M
(move |n| *n != current)
_RNCNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB6_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB8_12proof_encode4NodeEE9node_path00Ba_
Line
Count
Source
1095
18.2k
                .take_while(move |n| *n != current)
_RNCNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB6_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1e_NtNtB8_9trie_node17MerkleValueOutputEEE9node_path00Ba_
Line
Count
Source
1095
5.93M
                .take_while(move |n| *n != current)
_RNCNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB6_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB8_16TrieEntryVersionEEIB1e_NtNtB8_9trie_node17MerkleValueOutputEEE9node_path00Ba_
Line
Count
Source
1095
7.63M
                .take_while(move |n| *n != current)
_RNCNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB6_13TrieStructureuE9node_path00Ba_
Line
Count
Source
1095
11.1M
                .take_while(move |n| *n != current)
Unexecuted instantiation: _RNCNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB6_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1e_NtNtB8_9trie_node17MerkleValueOutputEEE9node_path00CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB6_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB8_12proof_encode4NodeEE9node_path00Ba_
_RNCNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB6_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1e_NtNtB8_9trie_node17MerkleValueOutputEEE9node_path00CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1095
154
                .take_while(move |n| *n != current)
_RNCNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB6_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1e_NtNtB8_9trie_node17MerkleValueOutputEEE9node_path00Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1095
1.46k
                .take_while(move |n| *n != current)
1096
23.2M
                .last()
1097
23.2M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE9node_path0B8_
Line
Count
Source
1093
34.6k
        iter::successors(Some(usize::MAX), move |&current| {
1094
34.6k
            self.reverse_node_path(target)
1095
34.6k
                .take_while(move |n| *n != current)
1096
34.6k
                .last()
1097
34.6k
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE9node_path0B8_
Line
Count
Source
1093
7.56M
        iter::successors(Some(usize::MAX), move |&current| {
1094
7.56M
            self.reverse_node_path(target)
1095
7.56M
                .take_while(move |n| *n != current)
1096
7.56M
                .last()
1097
7.56M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB6_16TrieEntryVersionEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE9node_path0B8_
Line
Count
Source
1093
7.65M
        iter::successors(Some(usize::MAX), move |&current| {
1094
7.65M
            self.reverse_node_path(target)
1095
7.65M
                .take_while(move |n| *n != current)
1096
7.65M
                .last()
1097
7.65M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureuE9node_path0B8_
Line
Count
Source
1093
8.00M
        iter::successors(Some(usize::MAX), move |&current| {
1094
8.00M
            self.reverse_node_path(target)
1095
8.00M
                .take_while(move |n| *n != current)
1096
8.00M
                .last()
1097
8.00M
        })
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE9node_path0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE9node_path0B8_
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE9node_path0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1093
132
        iter::successors(Some(usize::MAX), move |&current| {
1094
132
            self.reverse_node_path(target)
1095
132
                .take_while(move |n| *n != current)
1096
132
                .last()
1097
132
        })
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE9node_path0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1093
1.25k
        iter::successors(Some(usize::MAX), move |&current| {
1094
1.25k
            self.reverse_node_path(target)
1095
1.25k
                .take_while(move |n| *n != current)
1096
1.25k
                .last()
1097
1.25k
        })
1098
12.4M
        .skip(1)
1099
12.4M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE9node_pathB6_
Line
Count
Source
1086
25.6k
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
25.6k
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
25.6k
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
25.6k
        .skip(1)
1099
25.6k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE9node_pathB6_
Line
Count
Source
1086
4.71M
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
4.71M
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
4.71M
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
4.71M
        .skip(1)
1099
4.71M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE9node_pathB6_
Line
Count
Source
1086
3.93M
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
3.93M
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
3.93M
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
3.93M
        .skip(1)
1099
3.93M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE9node_pathB6_
Line
Count
Source
1086
3.79M
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
3.79M
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
3.79M
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
3.79M
        .skip(1)
1099
3.79M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE9node_pathCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE9node_pathB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE9node_pathCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1086
66
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
66
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
66
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
66
        .skip(1)
1099
66
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE9node_pathCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1086
627
    fn node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1087
627
        debug_assert!(self.nodes.get(usize::MAX).is_none());
1088
        // First element is an invalid key, each successor is the last element of
1089
        // `reverse_node_path(target)` that isn't equal to `current`.
1090
        // Since the first element is invalid, we skip it.
1091
        // Since `reverse_node_path` never produces `target`, we know that it also won't be
1092
        // produced here.
1093
627
        iter::successors(Some(usize::MAX), move |&current| {
1094
            self.reverse_node_path(target)
1095
                .take_while(move |n| *n != current)
1096
                .last()
1097
        })
1098
627
        .skip(1)
1099
627
    }
1100
1101
    /// Returns the indices of the nodes starting from `target` towards the root node. The returned
1102
    /// iterator does *not* include `target` but does include the root node if it is different
1103
    /// from `target`. If `target` is the root node, this returns an empty iterator.
1104
    ///
1105
    /// # Panic
1106
    ///
1107
    /// Panics if `target` is not a valid index.
1108
23.2M
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
48.0M
        
iter::successors23.2M
(
Some(target)23.2M
, move |current| {
1112
48.0M
            Some(self.nodes.get(*current).unwrap().parent
?21.1M
.0)
1113
48.0M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE17reverse_node_path0B8_
Line
Count
Source
1111
52.8k
        iter::successors(Some(target), move |current| {
1112
52.8k
            Some(self.nodes.get(*current).unwrap().parent
?34.1k
.0)
1113
52.8k
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE17reverse_node_path0B8_
Line
Count
Source
1111
13.5M
        iter::successors(Some(target), move |current| {
1112
13.5M
            Some(self.nodes.get(*current).unwrap().parent
?7.33M
.0)
1113
13.5M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB6_16TrieEntryVersionEEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE17reverse_node_path0B8_
Line
Count
Source
1111
15.2M
        iter::successors(Some(target), move |current| {
1112
15.2M
            Some(self.nodes.get(*current).unwrap().parent
?7.46M
.0)
1113
15.2M
        })
_RNCNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB4_13TrieStructureuE17reverse_node_path0B8_
Line
Count
Source
1111
19.1M
        iter::successors(Some(target), move |current| {
1112
19.1M
            Some(self.nodes.get(*current).unwrap().parent
?6.33M
.0)
1113
19.1M
        })
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE17reverse_node_path0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB6_12proof_encode4NodeEE17reverse_node_path0B8_
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE17reverse_node_path0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1111
286
        iter::successors(Some(target), move |current| {
1112
286
            Some(self.nodes.get(*current).unwrap().parent
?110
.0)
1113
286
        })
_RNCNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB4_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1c_NtNtB6_9trie_node17MerkleValueOutputEEE17reverse_node_path0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1111
2.71k
        iter::successors(Some(target), move |current| {
1112
2.71k
            Some(self.nodes.get(*current).unwrap().parent
?1.04k
.0)
1113
2.71k
        })
1114
23.2M
        .skip(1)
1115
23.2M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE17reverse_node_pathB6_
Line
Count
Source
1108
34.6k
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
34.6k
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
34.6k
        .skip(1)
1115
34.6k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE17reverse_node_pathB6_
Line
Count
Source
1108
7.56M
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
7.56M
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
7.56M
        .skip(1)
1115
7.56M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE17reverse_node_pathB6_
Line
Count
Source
1108
7.65M
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
7.65M
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
7.65M
        .skip(1)
1115
7.65M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE17reverse_node_pathB6_
Line
Count
Source
1108
8.00M
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
8.00M
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
8.00M
        .skip(1)
1115
8.00M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE17reverse_node_pathCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE17reverse_node_pathB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE17reverse_node_pathCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1108
132
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
132
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
132
        .skip(1)
1115
132
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE17reverse_node_pathCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1108
1.25k
    fn reverse_node_path(&self, target: usize) -> impl Iterator<Item = usize> {
1109
        // First element is `target`, each successor is `current.parent`.
1110
        // Since `target` must explicitly not be included, we skip the first element.
1111
1.25k
        iter::successors(Some(target), move |current| {
1112
            Some(self.nodes.get(*current).unwrap().parent?.0)
1113
        })
1114
1.25k
        .skip(1)
1115
1.25k
    }
1116
1117
    /// Returns the next sibling of the given node.
1118
    ///
1119
    /// # Panic
1120
    ///
1121
    /// Panics if `node_index` is not a valid index.
1122
9.14M
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
9.14M
        let (
parent_index6.36M
,
child_index6.36M
) = self.nodes.get(node_index).unwrap().parent
?2.78M
;
1124
6.36M
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
29.0M
        for idx in 
(u8::from(child_index) + 1)6.36M
..16 {
1127
29.0M
            if let Some(
child3.63M
) = parent.children[usize::from(idx)] {
1128
3.63M
                return Some(child);
1129
25.4M
            }
1130
        }
1131
1132
2.72M
        None
1133
9.14M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE12next_siblingB6_
Line
Count
Source
1122
29.2k
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
29.2k
        let (
parent_index27.7k
,
child_index27.7k
) = self.nodes.get(node_index).unwrap().parent
?1.50k
;
1124
27.7k
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
87.5k
        for idx in 
(u8::from(child_index) + 1)27.7k
..16 {
1127
87.5k
            if let Some(
child19.7k
) = parent.children[usize::from(idx)] {
1128
19.7k
                return Some(child);
1129
67.8k
            }
1130
        }
1131
1132
7.95k
        None
1133
29.2k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12next_siblingB6_
Line
Count
Source
1122
4.34M
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
4.34M
        let (
parent_index2.62M
,
child_index2.62M
) = self.nodes.get(node_index).unwrap().parent
?1.72M
;
1124
2.62M
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
13.4M
        for idx in 
(u8::from(child_index) + 1)2.62M
..16 {
1127
13.4M
            if let Some(
child1.35M
) = parent.children[usize::from(idx)] {
1128
1.35M
                return Some(child);
1129
12.0M
            }
1130
        }
1131
1132
1.26M
        None
1133
4.34M
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB4_16TrieEntryVersionEEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12next_siblingB6_
Line
Count
Source
1122
939k
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
939k
        let (
parent_index808k
,
child_index808k
) = self.nodes.get(node_index).unwrap().parent
?131k
;
1124
808k
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
2.65M
        for idx in 
(u8::from(child_index) + 1)808k
..16 {
1127
2.65M
            if let Some(
child571k
) = parent.children[usize::from(idx)] {
1128
571k
                return Some(child);
1129
2.08M
            }
1130
        }
1131
1132
237k
        None
1133
939k
    }
_RNvMNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB2_13TrieStructureuE12next_siblingB6_
Line
Count
Source
1122
3.82M
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
3.82M
        let (
parent_index2.89M
,
child_index2.89M
) = self.nodes.get(node_index).unwrap().parent
?928k
;
1124
2.89M
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
12.8M
        for idx in 
(u8::from(child_index) + 1)2.89M
..16 {
1127
12.8M
            if let Some(
child1.69M
) = parent.children[usize::from(idx)] {
1128
1.69M
                return Some(child);
1129
11.1M
            }
1130
        }
1131
1132
1.20M
        None
1133
3.82M
    }
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12next_siblingCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB4_12proof_encode4NodeEE12next_siblingB6_
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12next_siblingCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1122
96
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
96
        let (
parent_index94
,
child_index94
) = self.nodes.get(node_index).unwrap().parent
?2
;
1124
94
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
292
        for idx in 
(u8::from(child_index) + 1)94
..16 {
1127
292
            if let Some(
child68
) = parent.children[usize::from(idx)] {
1128
68
                return Some(child);
1129
224
            }
1130
        }
1131
1132
26
        None
1133
96
    }
_RNvMNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB2_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB4_9trie_node17MerkleValueOutputEEE12next_siblingCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1122
912
    fn next_sibling(&self, node_index: usize) -> Option<usize> {
1123
912
        let (
parent_index893
,
child_index893
) = self.nodes.get(node_index).unwrap().parent
?19
;
1124
893
        let parent = self.nodes.get(parent_index).unwrap();
1125
1126
2.77k
        for idx in 
(u8::from(child_index) + 1)893
..16 {
1127
2.77k
            if let Some(
child646
) = parent.children[usize::from(idx)] {
1128
646
                return Some(child);
1129
2.12k
            }
1130
        }
1131
1132
247
        None
1133
912
    }
1134
}
1135
1136
impl<TUd> Default for TrieStructure<TUd> {
1137
0
    fn default() -> Self {
1138
0
        Self::new()
1139
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structures_0pEINtB5_13TrieStructurepENtNtCs1p5UDGgVI4d_4core7default7Default7defaultB9_
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structures_0pEINtB5_13TrieStructurepENtNtCs1p5UDGgVI4d_4core7default7Default7defaultB9_
1140
}
1141
1142
impl<TUd: fmt::Debug> fmt::Debug for TrieStructure<TUd> {
1143
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1144
0
        f.debug_list()
1145
0
            .entries(
1146
0
                self.all_node_lexicographic_ordered()
1147
0
                    .map(|idx| (idx, self.nodes.get(idx).unwrap())),
Unexecuted instantiation: _RNCNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1f_NtNtB9_9trie_node17MerkleValueOutputEEENtNtB1j_3fmt5Debug3fmt0Bb_
Unexecuted instantiation: _RNCNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1f_NtNtB9_9trie_node17MerkleValueOutputEEENtNtB1j_3fmt5Debug3fmt0Bb_
Unexecuted instantiation: _RNCNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_13TrieStructureuENtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt0Bb_
Unexecuted instantiation: _RNCNvXININtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structures0_0pEINtB7_13TrieStructurepENtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmt0Bb_
1148
            )
1149
0
            .finish()
1150
0
    }
Unexecuted instantiation: _RNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEENtNtB1h_3fmt5Debug3fmtB9_
Unexecuted instantiation: _RNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEENtNtB1h_3fmt5Debug3fmtB9_
Unexecuted instantiation: _RNvXs0_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureuENtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmtB9_
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structures0_0pEINtB5_13TrieStructurepENtNtCs1p5UDGgVI4d_4core3fmt5Debug3fmtB9_
1151
}
1152
1153
impl<TUd> ops::Index<NodeIndex> for TrieStructure<TUd> {
1154
    type Output = TUd;
1155
1156
    #[track_caller]
1157
1.93M
    fn index(&self, node_index: NodeIndex) -> &TUd {
1158
1.93M
        &self.nodes[node_index.0].user_data
1159
1.93M
    }
_RNvXs1_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEEINtNtNtB1h_3ops5index5IndexNtB5_9NodeIndexE5indexB9_
Line
Count
Source
1157
469k
    fn index(&self, node_index: NodeIndex) -> &TUd {
1158
469k
        &self.nodes[node_index.0].user_data
1159
469k
    }
_RNvXs1_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEEINtNtNtB1h_3ops5index5IndexNtB5_9NodeIndexE5indexB9_
Line
Count
Source
1157
1.46M
    fn index(&self, node_index: NodeIndex) -> &TUd {
1158
1.46M
        &self.nodes[node_index.0].user_data
1159
1.46M
    }
Unexecuted instantiation: _RNvXs1_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEEINtNtNtB1h_3ops5index5IndexNtB5_9NodeIndexE5indexCscoAnRPySggw_6author
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structures1_0pEINtB5_13TrieStructurepEINtNtNtCs1p5UDGgVI4d_4core3ops5index5IndexNtB5_9NodeIndexE5indexB9_
_RNvXs1_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEEINtNtNtB1h_3ops5index5IndexNtB5_9NodeIndexE5indexCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1157
96
    fn index(&self, node_index: NodeIndex) -> &TUd {
1158
96
        &self.nodes[node_index.0].user_data
1159
96
    }
_RNvXs1_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13TrieStructureTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEEINtNtNtB1h_3ops5index5IndexNtB5_9NodeIndexE5indexCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1157
912
    fn index(&self, node_index: NodeIndex) -> &TUd {
1158
912
        &self.nodes[node_index.0].user_data
1159
912
    }
1160
}
1161
1162
impl<TUd> ops::IndexMut<NodeIndex> for TrieStructure<TUd> {
1163
    #[track_caller]
1164
0
    fn index_mut(&mut self, node_index: NodeIndex) -> &mut TUd {
1165
0
        &mut self.nodes[node_index.0].user_data
1166
0
    }
Unexecuted instantiation: _RNvXININtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structures2_0pEINtB5_13TrieStructurepEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutNtB5_9NodeIndexE9index_mutB9_
Unexecuted instantiation: _RNvXININtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structures2_0pEINtB5_13TrieStructurepEINtNtNtCs1p5UDGgVI4d_4core3ops5index8IndexMutNtB5_9NodeIndexE9index_mutB9_
1167
}
1168
1169
enum ExistingNodeInnerResult<I> {
1170
    Found {
1171
        node_index: usize,
1172
        has_storage_value: bool,
1173
    },
1174
    NotFound {
1175
        /// Closest ancestor that actually exists.
1176
        /// If `Some`, also contains `desired_nibbles - closest_ancestor_key`. This iterator is
1177
        /// guaranteed to have at least one element.
1178
        closest_ancestor: Option<(usize, I)>,
1179
    },
1180
}
1181
1182
/// Index of a node in the trie. Never invalidated, except when if node in question is destroyed.
1183
///
1184
/// See [`TrieStructure::node_by_index`] for more information.
1185
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1186
pub struct NodeIndex(usize);
1187
1188
/// Access to a entry for a potential node within the [`TrieStructure`].
1189
///
1190
/// See [`TrieStructure::node`] for more information.
1191
pub enum Entry<'a, TUd, TKIter> {
1192
    /// There exists a node with this key.
1193
    Occupied(NodeAccess<'a, TUd>),
1194
    /// This entry is vacant.
1195
    Vacant(Vacant<'a, TUd, TKIter>),
1196
}
1197
1198
impl<'a, TUd, TKIter> Entry<'a, TUd, TKIter> {
1199
    /// Returns `Some` if `self` is an [`Entry::Vacant`].
1200
30
    pub fn into_vacant(self) -> Option<Vacant<'a, TUd, TKIter>> {
1201
30
        match self {
1202
30
            Entry::Vacant(e) => Some(e),
1203
0
            _ => None,
1204
        }
1205
30
    }
_RNvMs3_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_5EntryuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1c_5slice4iter4IterNtNtB7_6nibble6NibbleEEE11into_vacantB9_
Line
Count
Source
1200
30
    pub fn into_vacant(self) -> Option<Vacant<'a, TUd, TKIter>> {
1201
30
        match self {
1202
30
            Entry::Vacant(e) => Some(e),
1203
0
            _ => None,
1204
        }
1205
30
    }
Unexecuted instantiation: _RNvMs3_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_5EntryppE11into_vacantB9_
1206
1207
    /// Returns `Some` if `self` is an [`Entry::Occupied`].
1208
6
    pub fn into_occupied(self) -> Option<NodeAccess<'a, TUd>> {
1209
6
        match self {
1210
6
            Entry::Occupied(e) => Some(e),
1211
0
            _ => None,
1212
        }
1213
6
    }
_RNvMs3_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_5EntryuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1c_5slice4iter4IterNtNtB7_6nibble6NibbleEEE13into_occupiedB9_
Line
Count
Source
1208
6
    pub fn into_occupied(self) -> Option<NodeAccess<'a, TUd>> {
1209
6
        match self {
1210
6
            Entry::Occupied(e) => Some(e),
1211
0
            _ => None,
1212
        }
1213
6
    }
Unexecuted instantiation: _RNvMs3_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_5EntryppE13into_occupiedB9_
1214
}
1215
1216
/// Access to a node within the [`TrieStructure`].
1217
pub enum NodeAccess<'a, TUd> {
1218
    Storage(StorageNodeAccess<'a, TUd>),
1219
    Branch(BranchNodeAccess<'a, TUd>),
1220
}
1221
1222
impl<'a, TUd> NodeAccess<'a, TUd> {
1223
    /// Returns `Some` if `self` is an [`NodeAccess::Storage`].
1224
1
    pub fn into_storage(self) -> Option<StorageNodeAccess<'a, TUd>> {
1225
1
        match self {
1226
1
            NodeAccess::Storage(e) => Some(e),
1227
0
            _ => None,
1228
        }
1229
1
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessuE12into_storageB9_
Line
Count
Source
1224
1
    pub fn into_storage(self) -> Option<StorageNodeAccess<'a, TUd>> {
1225
1
        match self {
1226
1
            NodeAccess::Storage(e) => Some(e),
1227
0
            _ => None,
1228
        }
1229
1
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE12into_storageB9_
1230
1231
    /// Returns an opaque [`NodeIndex`] representing the node in the trie.
1232
    ///
1233
    /// It can later be used to retrieve this same node using [`TrieStructure::node_by_index`].
1234
1.50k
    pub fn node_index(&self) -> NodeIndex {
1235
1.50k
        match self {
1236
1.50k
            NodeAccess::Storage(n) => n.node_index(),
1237
0
            NodeAccess::Branch(n) => n.node_index(),
1238
        }
1239
1.50k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
Line
Count
Source
1234
1.50k
    pub fn node_index(&self) -> NodeIndex {
1235
1.50k
        match self {
1236
1.50k
            NodeAccess::Storage(n) => n.node_index(),
1237
0
            NodeAccess::Branch(n) => n.node_index(),
1238
        }
1239
1.50k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
1240
1241
    /// Returns the parent of this node, or `None` if this is the root node.
1242
9.45k
    pub fn into_parent(self) -> Option<NodeAccess<'a, TUd>> {
1243
9.45k
        match self {
1244
9.45k
            NodeAccess::Storage(n) => n.into_parent(),
1245
0
            NodeAccess::Branch(n) => n.into_parent(),
1246
        }
1247
9.45k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
Line
Count
Source
1242
9.45k
    pub fn into_parent(self) -> Option<NodeAccess<'a, TUd>> {
1243
9.45k
        match self {
1244
9.45k
            NodeAccess::Storage(n) => n.into_parent(),
1245
0
            NodeAccess::Branch(n) => n.into_parent(),
1246
        }
1247
9.45k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
1248
1249
    /// Returns the parent of this node, or `None` if this is the root node.
1250
0
    pub fn parent(&mut self) -> Option<NodeAccess<TUd>> {
1251
0
        match self {
1252
0
            NodeAccess::Storage(n) => n.parent(),
1253
0
            NodeAccess::Branch(n) => n.parent(),
1254
        }
1255
0
    }
Unexecuted instantiation: _RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccesspE6parentB9_
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE6parentB9_
1256
1257
    /// Returns the user data of the child at the given index.
1258
    ///
1259
    /// > **Note**: This method exists because it accepts `&self` rather than `&mut self`. A
1260
    /// >           cleaner alternative would be to split the [`NodeAccess`] struct into
1261
    /// >           `NodeAccessRef` and `NodeAccessMut`, but that's a lot of efforts compare to
1262
    /// >           this single method.
1263
935k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1264
935k
        match self {
1265
844k
            NodeAccess::Storage(n) => n.child_user_data(index),
1266
91.1k
            NodeAccess::Branch(n) => n.child_user_data(index),
1267
        }
1268
935k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
Line
Count
Source
1263
467k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1264
467k
        match self {
1265
467k
            NodeAccess::Storage(n) => n.child_user_data(index),
1266
0
            NodeAccess::Branch(n) => n.child_user_data(index),
1267
        }
1268
467k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessuE15child_user_dataB9_
Line
Count
Source
1263
467k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1264
467k
        match self {
1265
376k
            NodeAccess::Storage(n) => n.child_user_data(index),
1266
91.1k
            NodeAccess::Branch(n) => n.child_user_data(index),
1267
        }
1268
467k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
1269
1270
    /// Returns the child of this node at the given index.
1271
7.63M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1272
7.63M
        match self {
1273
6.49M
            NodeAccess::Storage(n) => n.child(index),
1274
1.13M
            NodeAccess::Branch(n) => n.child(index),
1275
        }
1276
7.63M
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1271
84.8k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1272
84.8k
        match self {
1273
82.3k
            NodeAccess::Storage(n) => n.child(index),
1274
2.56k
            NodeAccess::Branch(n) => n.child(index),
1275
        }
1276
84.8k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1271
7.51M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1272
7.51M
        match self {
1273
6.38M
            NodeAccess::Storage(n) => n.child(index),
1274
1.12M
            NodeAccess::Branch(n) => n.child(index),
1275
        }
1276
7.51M
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE5childCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE5childB9_
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE5childCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1271
3.07k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1272
3.07k
        match self {
1273
2.24k
            NodeAccess::Storage(n) => n.child(index),
1274
832
            NodeAccess::Branch(n) => n.child(index),
1275
        }
1276
3.07k
    }
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE5childCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1271
29.1k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1272
29.1k
        match self {
1273
21.2k
            NodeAccess::Storage(n) => n.child(index),
1274
7.90k
            NodeAccess::Branch(n) => n.child(index),
1275
        }
1276
29.1k
    }
1277
1278
    /// Returns the child of this node given the given index.
1279
    ///
1280
    /// Returns back `self` if there is no such child at this index.
1281
0
    pub fn into_child(self, index: Nibble) -> Result<NodeAccess<'a, TUd>, Self> {
1282
0
        match self {
1283
0
            NodeAccess::Storage(n) => n.into_child(index).map_err(NodeAccess::Storage),
1284
0
            NodeAccess::Branch(n) => n.into_child(index).map_err(NodeAccess::Branch),
1285
        }
1286
0
    }
Unexecuted instantiation: _RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccesspE10into_childB9_
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE10into_childB9_
1287
1288
    /// Returns the first child of this node.
1289
    ///
1290
    /// Returns back `self` if this node doesn't have any children.
1291
29.2k
    pub fn into_first_child(self) -> Result<NodeAccess<'a, TUd>, Self> {
1292
29.2k
        match self {
1293
29.2k
            NodeAccess::Storage(n) => n.into_first_child().map_err(NodeAccess::Storage),
1294
0
            NodeAccess::Branch(n) => n.into_first_child().map_err(NodeAccess::Branch),
1295
        }
1296
29.2k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
Line
Count
Source
1291
29.2k
    pub fn into_first_child(self) -> Result<NodeAccess<'a, TUd>, Self> {
1292
29.2k
        match self {
1293
29.2k
            NodeAccess::Storage(n) => n.into_first_child().map_err(NodeAccess::Storage),
1294
0
            NodeAccess::Branch(n) => n.into_first_child().map_err(NodeAccess::Branch),
1295
        }
1296
29.2k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
1297
1298
    /// Returns the next sibling of this node.
1299
    ///
1300
    /// Returns back `self` if this node is the last child of its parent.
1301
29.2k
    pub fn into_next_sibling(self) -> Result<NodeAccess<'a, TUd>, Self> {
1302
29.2k
        match self {
1303
29.2k
            NodeAccess::Storage(n) => n.into_next_sibling().map_err(NodeAccess::Storage),
1304
0
            NodeAccess::Branch(n) => n.into_next_sibling().map_err(NodeAccess::Branch),
1305
        }
1306
29.2k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
Line
Count
Source
1301
29.2k
    pub fn into_next_sibling(self) -> Result<NodeAccess<'a, TUd>, Self> {
1302
29.2k
        match self {
1303
29.2k
            NodeAccess::Storage(n) => n.into_next_sibling().map_err(NodeAccess::Storage),
1304
0
            NodeAccess::Branch(n) => n.into_next_sibling().map_err(NodeAccess::Branch),
1305
        }
1306
29.2k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
1307
1308
    /// Returns true if this node is the root node of the trie.
1309
473k
    pub fn is_root_node(&self) -> bool {
1310
473k
        match self {
1311
402k
            NodeAccess::Storage(n) => n.is_root_node(),
1312
70.7k
            NodeAccess::Branch(n) => n.is_root_node(),
1313
        }
1314
473k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1309
469k
    pub fn is_root_node(&self) -> bool {
1310
469k
        match self {
1311
399k
            NodeAccess::Storage(n) => n.is_root_node(),
1312
70.4k
            NodeAccess::Branch(n) => n.is_root_node(),
1313
        }
1314
469k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1309
2.65k
    pub fn is_root_node(&self) -> bool {
1310
2.65k
        match self {
1311
2.57k
            NodeAccess::Storage(n) => n.is_root_node(),
1312
80
            NodeAccess::Branch(n) => n.is_root_node(),
1313
        }
1314
2.65k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE12is_root_nodeB9_
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1309
96
    pub fn is_root_node(&self) -> bool {
1310
96
        match self {
1311
70
            NodeAccess::Storage(n) => n.is_root_node(),
1312
26
            NodeAccess::Branch(n) => n.is_root_node(),
1313
        }
1314
96
    }
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1309
912
    pub fn is_root_node(&self) -> bool {
1310
912
        match self {
1311
665
            NodeAccess::Storage(n) => n.is_root_node(),
1312
247
            NodeAccess::Branch(n) => n.is_root_node(),
1313
        }
1314
912
    }
1315
1316
    /// Returns the full key of the node.
1317
0
    pub fn full_key(&self) -> impl Iterator<Item = Nibble> {
1318
0
        match self {
1319
0
            NodeAccess::Storage(n) => Either::Left(n.full_key()),
1320
0
            NodeAccess::Branch(n) => Either::Right(n.full_key()),
1321
        }
1322
0
    }
Unexecuted instantiation: _RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccesspE8full_keyB9_
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE8full_keyB9_
1323
1324
    /// Returns the partial key of the node.
1325
506k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
506k
        match self {
1327
429k
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
76.8k
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
506k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1325
5.30k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
5.30k
        match self {
1327
5.14k
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
160
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
5.30k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1325
469k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
469k
        match self {
1327
399k
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
70.4k
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
469k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessuE11partial_keyB9_
Line
Count
Source
1325
29.2k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
29.2k
        match self {
1327
23.5k
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
5.69k
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
29.2k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE11partial_keyB9_
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1325
192
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
192
        match self {
1327
140
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
52
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
192
    }
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1325
1.82k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1326
1.82k
        match self {
1327
1.33k
            NodeAccess::Storage(n) => Either::Left(n.partial_key()),
1328
494
            NodeAccess::Branch(n) => Either::Right(n.partial_key()),
1329
        }
1330
1.82k
    }
1331
1332
    /// Returns the user data stored in the node.
1333
1.42M
    pub fn user_data(&mut self) -> &mut TUd {
1334
1.42M
        match self {
1335
1.22M
            NodeAccess::Storage(n) => n.user_data(),
1336
201k
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
1.42M
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
Line
Count
Source
1333
71.5k
    pub fn user_data(&mut self) -> &mut TUd {
1334
71.5k
        match self {
1335
71.5k
            NodeAccess::Storage(n) => n.user_data(),
1336
0
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
71.5k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1333
5.90k
    pub fn user_data(&mut self) -> &mut TUd {
1334
5.90k
        match self {
1335
5.66k
            NodeAccess::Storage(n) => n.user_data(),
1336
240
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
5.90k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1333
1.34M
    pub fn user_data(&mut self) -> &mut TUd {
1334
1.34M
        match self {
1335
1.14M
            NodeAccess::Storage(n) => n.user_data(),
1336
200k
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
1.34M
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1333
380
    pub fn user_data(&mut self) -> &mut TUd {
1334
380
        match self {
1335
280
            NodeAccess::Storage(n) => n.user_data(),
1336
100
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
380
    }
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1333
3.61k
    pub fn user_data(&mut self) -> &mut TUd {
1334
3.61k
        match self {
1335
2.66k
            NodeAccess::Storage(n) => n.user_data(),
1336
950
            NodeAccess::Branch(n) => n.user_data(),
1337
        }
1338
3.61k
    }
1339
1340
    /// Returns the user data stored in the node.
1341
489k
    pub fn into_user_data(self) -> &'a mut TUd {
1342
489k
        match self {
1343
418k
            NodeAccess::Storage(n) => n.into_user_data(),
1344
71.4k
            NodeAccess::Branch(n) => n.into_user_data(),
1345
        }
1346
489k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1341
2.65k
    pub fn into_user_data(self) -> &'a mut TUd {
1342
2.65k
        match self {
1343
2.57k
            NodeAccess::Storage(n) => n.into_user_data(),
1344
80
            NodeAccess::Branch(n) => n.into_user_data(),
1345
        }
1346
2.65k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1341
485k
    pub fn into_user_data(self) -> &'a mut TUd {
1342
485k
        match self {
1343
414k
            NodeAccess::Storage(n) => n.into_user_data(),
1344
71.1k
            NodeAccess::Branch(n) => n.into_user_data(),
1345
        }
1346
485k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE14into_user_dataB9_
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1341
96
    pub fn into_user_data(self) -> &'a mut TUd {
1342
96
        match self {
1343
70
            NodeAccess::Storage(n) => n.into_user_data(),
1344
26
            NodeAccess::Branch(n) => n.into_user_data(),
1345
        }
1346
96
    }
_RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1a_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1341
912
    pub fn into_user_data(self) -> &'a mut TUd {
1342
912
        match self {
1343
665
            NodeAccess::Storage(n) => n.into_user_data(),
1344
247
            NodeAccess::Branch(n) => n.into_user_data(),
1345
        }
1346
912
    }
1347
1348
    /// Returns true if the node has a storage value associated to it.
1349
29.2k
    pub fn has_storage_value(&self) -> bool {
1350
29.2k
        match self {
1351
23.5k
            NodeAccess::Storage(_) => true,
1352
5.70k
            NodeAccess::Branch(_) => false,
1353
        }
1354
29.2k
    }
_RNvMs4_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_10NodeAccessuE17has_storage_valueB9_
Line
Count
Source
1349
29.2k
    pub fn has_storage_value(&self) -> bool {
1350
29.2k
        match self {
1351
23.5k
            NodeAccess::Storage(_) => true,
1352
5.70k
            NodeAccess::Branch(_) => false,
1353
        }
1354
29.2k
    }
Unexecuted instantiation: _RNvMs4_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_10NodeAccesspE17has_storage_valueB9_
1355
}
1356
1357
/// Access to a node within the [`TrieStructure`] that is known to have a storage value associated
1358
/// to it.
1359
pub struct StorageNodeAccess<'a, TUd> {
1360
    trie: &'a mut TrieStructure<TUd>,
1361
    node_index: usize,
1362
}
1363
1364
impl<'a, TUd> StorageNodeAccess<'a, TUd> {
1365
    /// Returns an opaque [`NodeIndex`] representing the node in the trie.
1366
    ///
1367
    /// It can later be used to retrieve this same node using [`TrieStructure::node_by_index`].
1368
1.50k
    pub fn node_index(&self) -> NodeIndex {
1369
1.50k
        NodeIndex(self.node_index)
1370
1.50k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
Line
Count
Source
1368
1.50k
    pub fn node_index(&self) -> NodeIndex {
1369
1.50k
        NodeIndex(self.node_index)
1370
1.50k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
1371
1372
    /// Returns the parent of this node, or `None` if this is the root node.
1373
9.45k
    pub fn into_parent(self) -> Option<NodeAccess<'a, TUd>> {
1374
9.45k
        let 
parent_idx7.95k
= self.trie.nodes.get(self.node_index).unwrap().parent
?1.50k
.0;
1375
7.95k
        Some(self.trie.node_by_index_inner(parent_idx).unwrap())
1376
9.45k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
Line
Count
Source
1373
9.45k
    pub fn into_parent(self) -> Option<NodeAccess<'a, TUd>> {
1374
9.45k
        let 
parent_idx7.95k
= self.trie.nodes.get(self.node_index).unwrap().parent
?1.50k
.0;
1375
7.95k
        Some(self.trie.node_by_index_inner(parent_idx).unwrap())
1376
9.45k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
1377
1378
    /// Returns the parent of this node, or `None` if this is the root node.
1379
0
    pub fn parent(&mut self) -> Option<NodeAccess<TUd>> {
1380
0
        let parent_idx = self.trie.nodes.get(self.node_index).unwrap().parent?.0;
1381
0
        Some(self.trie.node_by_index_inner(parent_idx).unwrap())
1382
0
    }
Unexecuted instantiation: _RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE6parentB9_
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE6parentB9_
1383
1384
    /// Returns the first child of this node.
1385
    ///
1386
    /// Returns back `self` if this node doesn't have any children.
1387
29.2k
    pub fn into_first_child(self) -> Result<NodeAccess<'a, TUd>, Self> {
1388
29.2k
        let first_child_idx = self
1389
29.2k
            .trie
1390
29.2k
            .nodes
1391
29.2k
            .get(self.node_index)
1392
29.2k
            .unwrap()
1393
29.2k
            .children
1394
29.2k
            .iter()
1395
29.2k
            .find_map(|c| *c);
1396
1397
29.2k
        let 
first_child_idx7.95k
= match first_child_idx {
1398
7.95k
            Some(fc) => fc,
1399
21.2k
            None => return Err(self),
1400
        };
1401
1402
7.95k
        Ok(self.trie.node_by_index_inner(first_child_idx).unwrap())
1403
29.2k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
Line
Count
Source
1387
29.2k
    pub fn into_first_child(self) -> Result<NodeAccess<'a, TUd>, Self> {
1388
29.2k
        let first_child_idx = self
1389
29.2k
            .trie
1390
29.2k
            .nodes
1391
29.2k
            .get(self.node_index)
1392
29.2k
            .unwrap()
1393
29.2k
            .children
1394
29.2k
            .iter()
1395
29.2k
            .find_map(|c| *c);
1396
1397
29.2k
        let 
first_child_idx7.95k
= match first_child_idx {
1398
7.95k
            Some(fc) => fc,
1399
21.2k
            None => return Err(self),
1400
        };
1401
1402
7.95k
        Ok(self.trie.node_by_index_inner(first_child_idx).unwrap())
1403
29.2k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
1404
1405
    /// Returns the next sibling of this node.
1406
    ///
1407
    /// Returns back `self` if this node is the last child of its parent.
1408
29.2k
    pub fn into_next_sibling(self) -> Result<NodeAccess<'a, TUd>, Self> {
1409
29.2k
        let 
next_sibling_idx19.7k
= match self.trie.next_sibling(self.node_index) {
1410
19.7k
            Some(ns) => ns,
1411
9.45k
            None => return Err(self),
1412
        };
1413
1414
19.7k
        Ok(self.trie.node_by_index_inner(next_sibling_idx).unwrap())
1415
29.2k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
Line
Count
Source
1408
29.2k
    pub fn into_next_sibling(self) -> Result<NodeAccess<'a, TUd>, Self> {
1409
29.2k
        let 
next_sibling_idx19.7k
= match self.trie.next_sibling(self.node_index) {
1410
19.7k
            Some(ns) => ns,
1411
9.45k
            None => return Err(self),
1412
        };
1413
1414
19.7k
        Ok(self.trie.node_by_index_inner(next_sibling_idx).unwrap())
1415
29.2k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
1416
1417
    /// Returns the child of this node at the given index.
1418
6.49M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1419
203k
        let child_idx =
1420
6.49M
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?6.29M
;
1421
203k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1422
6.49M
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1418
82.3k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1419
2.93k
        let child_idx =
1420
82.3k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?79.3k
;
1421
2.93k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1422
82.3k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1418
6.38M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1419
200k
        let child_idx =
1420
6.38M
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?6.18M
;
1421
200k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1422
6.38M
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE5childCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE5childB9_
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE5childCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1418
2.24k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1419
0
        let child_idx =
1420
2.24k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]?;
1421
0
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1422
2.24k
    }
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE5childCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1418
21.2k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1419
0
        let child_idx =
1420
21.2k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]?;
1421
0
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1422
21.2k
    }
1423
1424
    /// Returns the user data of the child at the given index.
1425
    ///
1426
    /// > **Note**: This method exists because it accepts `&self` rather than `&mut self`. A
1427
    /// >           cleaner alternative would be to split the [`NodeAccess`] struct into
1428
    /// >           `NodeAccessRef` and `NodeAccessMut`, but that's a lot of efforts compare to
1429
    /// >           this single method.
1430
844k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1431
39.6k
        let child_idx =
1432
844k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?804k
;
1433
39.6k
        Some(&self.trie.nodes.get(child_idx).unwrap().user_data)
1434
844k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
Line
Count
Source
1430
467k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1431
27.7k
        let child_idx =
1432
467k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?439k
;
1433
27.7k
        Some(&self.trie.nodes.get(child_idx).unwrap().user_data)
1434
467k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessuE15child_user_dataB9_
Line
Count
Source
1430
376k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1431
11.9k
        let child_idx =
1432
376k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?364k
;
1433
11.9k
        Some(&self.trie.nodes.get(child_idx).unwrap().user_data)
1434
376k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
1435
1436
    /// Returns the child of this node given the given index.
1437
    ///
1438
    /// Returns back `self` if there is no such child at this index.
1439
0
    pub fn into_child(self, index: Nibble) -> Result<NodeAccess<'a, TUd>, Self> {
1440
0
        let child_idx = match self.trie.nodes.get(self.node_index).unwrap().children
1441
0
            [usize::from(u8::from(index))]
1442
        {
1443
0
            Some(c) => c,
1444
0
            None => return Err(self),
1445
        };
1446
1447
0
        Ok(self.trie.node_by_index_inner(child_idx).unwrap())
1448
0
    }
Unexecuted instantiation: _RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE10into_childB9_
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE10into_childB9_
1449
1450
    /// Returns true if this node is the root node of the trie.
1451
402k
    pub fn is_root_node(&self) -> bool {
1452
402k
        self.trie.root_index == Some(self.node_index)
1453
402k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1451
399k
    pub fn is_root_node(&self) -> bool {
1452
399k
        self.trie.root_index == Some(self.node_index)
1453
399k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1451
2.57k
    pub fn is_root_node(&self) -> bool {
1452
2.57k
        self.trie.root_index == Some(self.node_index)
1453
2.57k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE12is_root_nodeB9_
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1451
70
    pub fn is_root_node(&self) -> bool {
1452
70
        self.trie.root_index == Some(self.node_index)
1453
70
    }
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1451
665
    pub fn is_root_node(&self) -> bool {
1452
665
        self.trie.root_index == Some(self.node_index)
1453
665
    }
1454
1455
    /// Returns the full key of the node.
1456
0
    pub fn full_key(&self) -> impl Iterator<Item = Nibble> {
1457
0
        self.trie.node_full_key(self.node_index)
1458
0
    }
Unexecuted instantiation: _RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE8full_keyB9_
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE8full_keyB9_
1459
1460
    /// Returns the partial key of the node.
1461
429k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
429k
        self.trie
1463
429k
            .nodes
1464
429k
            .get(self.node_index)
1465
429k
            .unwrap()
1466
429k
            .partial_key
1467
429k
            .iter()
1468
429k
            .cloned()
1469
429k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1461
5.14k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
5.14k
        self.trie
1463
5.14k
            .nodes
1464
5.14k
            .get(self.node_index)
1465
5.14k
            .unwrap()
1466
5.14k
            .partial_key
1467
5.14k
            .iter()
1468
5.14k
            .cloned()
1469
5.14k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1461
399k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
399k
        self.trie
1463
399k
            .nodes
1464
399k
            .get(self.node_index)
1465
399k
            .unwrap()
1466
399k
            .partial_key
1467
399k
            .iter()
1468
399k
            .cloned()
1469
399k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessuE11partial_keyB9_
Line
Count
Source
1461
23.5k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
23.5k
        self.trie
1463
23.5k
            .nodes
1464
23.5k
            .get(self.node_index)
1465
23.5k
            .unwrap()
1466
23.5k
            .partial_key
1467
23.5k
            .iter()
1468
23.5k
            .cloned()
1469
23.5k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE11partial_keyB9_
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1461
140
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
140
        self.trie
1463
140
            .nodes
1464
140
            .get(self.node_index)
1465
140
            .unwrap()
1466
140
            .partial_key
1467
140
            .iter()
1468
140
            .cloned()
1469
140
    }
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1461
1.33k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1462
1.33k
        self.trie
1463
1.33k
            .nodes
1464
1.33k
            .get(self.node_index)
1465
1.33k
            .unwrap()
1466
1.33k
            .partial_key
1467
1.33k
            .iter()
1468
1.33k
            .cloned()
1469
1.33k
    }
1470
1471
    /// Returns the user data associated to this node.
1472
483k
    pub fn into_user_data(self) -> &'a mut TUd {
1473
483k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1474
483k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1472
2.57k
    pub fn into_user_data(self) -> &'a mut TUd {
1473
2.57k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1474
2.57k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1472
479k
    pub fn into_user_data(self) -> &'a mut TUd {
1473
479k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1474
479k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE14into_user_dataB9_
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1472
70
    pub fn into_user_data(self) -> &'a mut TUd {
1473
70
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1474
70
    }
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1472
665
    pub fn into_user_data(self) -> &'a mut TUd {
1473
665
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1474
665
    }
1475
1476
    /// Returns the user data associated to this node.
1477
1.39M
    pub fn user_data(&mut self) -> &mut TUd {
1478
1.39M
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
1.39M
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
Line
Count
Source
1477
71.5k
    pub fn user_data(&mut self) -> &mut TUd {
1478
71.5k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
71.5k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1477
5.66k
    pub fn user_data(&mut self) -> &mut TUd {
1478
5.66k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
5.66k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1477
1.31M
    pub fn user_data(&mut self) -> &mut TUd {
1478
1.31M
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
1.31M
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1477
280
    pub fn user_data(&mut self) -> &mut TUd {
1478
280
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
280
    }
_RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1477
2.66k
    pub fn user_data(&mut self) -> &mut TUd {
1478
2.66k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1479
2.66k
    }
1480
1481
    /// Removes the storage value and returns what this changes in the trie structure.
1482
134k
    pub fn remove(self) -> Remove<'a, TUd> {
1483
        // If the removed node has 2 or more children, then the node continues as a branch node.
1484
        {
1485
134k
            let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1486
2.15M
            if 
node.children134k
.
iter134k
().
filter134k
(|c| c.is_some()).
count134k
() >= 2 {
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1j_NtNtB9_9trie_node17MerkleValueOutputEEE6remove0Bb_
Line
Count
Source
1486
1.37M
            if node.children.iter().filter(|c| c.is_some()).count() >= 2 {
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessuE6remove0Bb_
Line
Count
Source
1486
778k
            if node.children.iter().filter(|c| c.is_some()).count() >= 2 {
Unexecuted instantiation: _RNCNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_17StorageNodeAccesspE6remove0Bb_
1487
71.9k
                node.has_storage_value = false;
1488
71.9k
                return Remove::StorageToBranch(BranchNodeAccess {
1489
71.9k
                    trie: self.trie,
1490
71.9k
                    node_index: self.node_index,
1491
71.9k
                });
1492
62.6k
            }
1493
        }
1494
1495
62.6k
        let removed_node = self.trie.nodes.remove(self.node_index);
1496
62.6k
        debug_assert!(removed_node.has_storage_value);
1497
1498
        // We already know from above that the removed node has only 0 or 1 children. Let's
1499
        // determine which.
1500
62.6k
        let child_node_index: Option<usize> = removed_node.children.iter().find_map(|c| *c);
1501
1502
        // If relevant, update our single child's parent to point to `removed_node`'s parent.
1503
62.6k
        if let Some(
child_node_index12.2k
) = child_node_index {
1504
12.2k
            let child = self.trie.nodes.get_mut(child_node_index).unwrap();
1505
12.2k
            debug_assert_eq!(child.parent.as_ref().unwrap().0, self.node_index);
1506
12.2k
            insert_front(
1507
12.2k
                &mut child.partial_key,
1508
12.2k
                removed_node.partial_key,
1509
12.2k
                child.parent.unwrap().1,
1510
            );
1511
12.2k
            child.parent = removed_node.parent;
1512
50.4k
        }
1513
1514
        // At this point, we're almost done with removing `removed_node` from `self.trie`. However
1515
        // there is potentially another change to make: maybe `parent` has to be removed from the
1516
        // trie as well.
1517
1518
        // Update `parent`'s child to point to `child_node_index`.
1519
        // `single_remove` is true if we can keep `parent` in the trie.
1520
62.6k
        let single_remove = if let Some((
parent_index47.0k
,
parent_to_removed_child_index47.0k
)) =
1521
62.6k
            removed_node.parent
1522
        {
1523
            // Update `removed_node`'s parent to point to the child.
1524
47.0k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
1525
47.0k
            debug_assert_eq!(
1526
47.0k
                parent.children[usize::from(u8::from(parent_to_removed_child_index))],
1527
47.0k
                Some(self.node_index)
1528
            );
1529
47.0k
            parent.children[usize::from(u8::from(parent_to_removed_child_index))] =
1530
47.0k
                child_node_index;
1531
1532
            // If `parent` does *not* need to be removed, we can return early.
1533
370k
            
parent.has_storage_value47.0k
||
parent.children23.1k
.
iter23.1k
().
filter23.1k
(|c| c.is_some()).
count23.1k
() >= 2
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1j_NtNtB9_9trie_node17MerkleValueOutputEEE6removes0_0Bb_
Line
Count
Source
1533
2.32k
            parent.has_storage_value || parent.children.iter().filter(|c| c.is_some()).count() >= 2
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessuE6removes0_0Bb_
Line
Count
Source
1533
368k
            parent.has_storage_value || parent.children.iter().filter(|c| c.is_some()).count() >= 2
Unexecuted instantiation: _RNCNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_17StorageNodeAccesspE6removes0_0Bb_
1534
        } else {
1535
15.5k
            debug_assert_eq!(self.trie.root_index, Some(self.node_index));
1536
15.5k
            self.trie.root_index = child_node_index;
1537
15.5k
            true
1538
        };
1539
1540
        // If we keep the parent in the trie, return early with a `SingleRemove`.
1541
62.6k
        if single_remove {
1542
52.0k
            return if let Some(
child_node_index12.2k
) = child_node_index {
1543
12.2k
                Remove::SingleRemoveChild {
1544
12.2k
                    user_data: removed_node.user_data,
1545
12.2k
                    child: self.trie.node_by_index_inner(child_node_index).unwrap(),
1546
12.2k
                }
1547
39.8k
            } else if let Some((
parent_index33.3k
, _)) = removed_node.parent {
1548
33.3k
                Remove::SingleRemoveNoChild {
1549
33.3k
                    user_data: removed_node.user_data,
1550
33.3k
                    parent: self.trie.node_by_index_inner(parent_index).unwrap(),
1551
33.3k
                }
1552
            } else {
1553
6.49k
                debug_assert!(self.trie.nodes.is_empty());
1554
6.49k
                debug_assert!(self.trie.root_index.is_none());
1555
6.49k
                Remove::TrieNowEmpty {
1556
6.49k
                    user_data: removed_node.user_data,
1557
6.49k
                }
1558
            };
1559
10.5k
        }
1560
1561
        // If we reach here, then parent has to be removed from the trie as well.
1562
10.5k
        let parent_index = removed_node.parent.unwrap().0;
1563
10.5k
        debug_assert!(child_node_index.is_none());
1564
10.5k
        let removed_branch = self.trie.nodes.remove(parent_index);
1565
10.5k
        debug_assert!(!removed_branch.has_storage_value);
1566
1567
        // We already know from above that the removed branch has exactly 1 sibling. Let's
1568
        // determine which.
1569
10.5k
        debug_assert_eq!(
1570
10.5k
            removed_branch
1571
10.5k
                .children
1572
10.5k
                .iter()
1573
169k
                .
filter10.5k
(|c| c.is_some())
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1j_NtNtB9_9trie_node17MerkleValueOutputEEE6removes2_0Bb_
Line
Count
Source
1573
1.08k
                .filter(|c| c.is_some())
_RNCNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_17StorageNodeAccessuE6removes2_0Bb_
Line
Count
Source
1573
168k
                .filter(|c| c.is_some())
Unexecuted instantiation: _RNCNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_17StorageNodeAccesspE6removes2_0Bb_
1574
10.5k
                .count(),
1575
            1
1576
        );
1577
10.5k
        let sibling_node_index: usize = removed_branch.children.iter().find_map(|c| *c).unwrap();
1578
1579
        // Update the sibling to point to the parent's parent.
1580
        {
1581
10.5k
            let sibling = self.trie.nodes.get_mut(sibling_node_index).unwrap();
1582
10.5k
            debug_assert_eq!(sibling.parent.as_ref().unwrap().0, parent_index);
1583
10.5k
            insert_front(
1584
10.5k
                &mut sibling.partial_key,
1585
10.5k
                removed_branch.partial_key,
1586
10.5k
                sibling.parent.unwrap().1,
1587
            );
1588
10.5k
            sibling.parent = removed_branch.parent;
1589
        }
1590
1591
        // Update the parent's parent to point to the sibling.
1592
10.5k
        if let Some((
parent_parent_index9.77k
,
parent_to_sibling_index9.77k
)) = removed_branch.parent {
1593
            // Update the parent's parent to point to the sibling.
1594
9.77k
            let parent_parent = self.trie.nodes.get_mut(parent_parent_index).unwrap();
1595
9.77k
            debug_assert_eq!(
1596
9.77k
                parent_parent.children[usize::from(u8::from(parent_to_sibling_index))],
1597
9.77k
                Some(parent_index)
1598
            );
1599
9.77k
            parent_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
1600
9.77k
                Some(sibling_node_index);
1601
        } else {
1602
826
            debug_assert_eq!(self.trie.root_index, Some(parent_index));
1603
826
            self.trie.root_index = Some(sibling_node_index);
1604
        }
1605
1606
        // Success!
1607
10.5k
        Remove::BranchAlsoRemoved {
1608
10.5k
            sibling: self.trie.node_by_index_inner(sibling_node_index).unwrap(),
1609
10.5k
            storage_user_data: removed_node.user_data,
1610
10.5k
            branch_user_data: removed_branch.user_data,
1611
10.5k
        }
1612
134k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1h_NtNtB7_9trie_node17MerkleValueOutputEEE6removeB9_
Line
Count
Source
1482
85.9k
    pub fn remove(self) -> Remove<'a, TUd> {
1483
        // If the removed node has 2 or more children, then the node continues as a branch node.
1484
        {
1485
85.9k
            let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1486
85.9k
            if node.children.iter().filter(|c| c.is_some()).count() >= 2 {
1487
70.5k
                node.has_storage_value = false;
1488
70.5k
                return Remove::StorageToBranch(BranchNodeAccess {
1489
70.5k
                    trie: self.trie,
1490
70.5k
                    node_index: self.node_index,
1491
70.5k
                });
1492
15.4k
            }
1493
        }
1494
1495
15.4k
        let removed_node = self.trie.nodes.remove(self.node_index);
1496
15.4k
        debug_assert!(removed_node.has_storage_value);
1497
1498
        // We already know from above that the removed node has only 0 or 1 children. Let's
1499
        // determine which.
1500
15.4k
        let child_node_index: Option<usize> = removed_node.children.iter().find_map(|c| *c);
1501
1502
        // If relevant, update our single child's parent to point to `removed_node`'s parent.
1503
15.4k
        if let Some(
child_node_index9.06k
) = child_node_index {
1504
9.06k
            let child = self.trie.nodes.get_mut(child_node_index).unwrap();
1505
9.06k
            debug_assert_eq!(child.parent.as_ref().unwrap().0, self.node_index);
1506
9.06k
            insert_front(
1507
9.06k
                &mut child.partial_key,
1508
9.06k
                removed_node.partial_key,
1509
9.06k
                child.parent.unwrap().1,
1510
            );
1511
9.06k
            child.parent = removed_node.parent;
1512
6.38k
        }
1513
1514
        // At this point, we're almost done with removing `removed_node` from `self.trie`. However
1515
        // there is potentially another change to make: maybe `parent` has to be removed from the
1516
        // trie as well.
1517
1518
        // Update `parent`'s child to point to `child_node_index`.
1519
        // `single_remove` is true if we can keep `parent` in the trie.
1520
15.4k
        let single_remove = if let Some((
parent_index271
,
parent_to_removed_child_index271
)) =
1521
15.4k
            removed_node.parent
1522
        {
1523
            // Update `removed_node`'s parent to point to the child.
1524
271
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
1525
271
            debug_assert_eq!(
1526
271
                parent.children[usize::from(u8::from(parent_to_removed_child_index))],
1527
271
                Some(self.node_index)
1528
            );
1529
271
            parent.children[usize::from(u8::from(parent_to_removed_child_index))] =
1530
271
                child_node_index;
1531
1532
            // If `parent` does *not* need to be removed, we can return early.
1533
271
            parent.has_storage_value || 
parent.children145
.
iter145
().
filter145
(|c| c.is_some()).
count145
() >= 2
1534
        } else {
1535
15.1k
            debug_assert_eq!(self.trie.root_index, Some(self.node_index));
1536
15.1k
            self.trie.root_index = child_node_index;
1537
15.1k
            true
1538
        };
1539
1540
        // If we keep the parent in the trie, return early with a `SingleRemove`.
1541
15.4k
        if single_remove {
1542
15.3k
            return if let Some(
child_node_index9.06k
) = child_node_index {
1543
9.06k
                Remove::SingleRemoveChild {
1544
9.06k
                    user_data: removed_node.user_data,
1545
9.06k
                    child: self.trie.node_by_index_inner(child_node_index).unwrap(),
1546
9.06k
                }
1547
6.31k
            } else if let Some((
parent_index199
, _)) = removed_node.parent {
1548
199
                Remove::SingleRemoveNoChild {
1549
199
                    user_data: removed_node.user_data,
1550
199
                    parent: self.trie.node_by_index_inner(parent_index).unwrap(),
1551
199
                }
1552
            } else {
1553
6.12k
                debug_assert!(self.trie.nodes.is_empty());
1554
6.12k
                debug_assert!(self.trie.root_index.is_none());
1555
6.12k
                Remove::TrieNowEmpty {
1556
6.12k
                    user_data: removed_node.user_data,
1557
6.12k
                }
1558
            };
1559
68
        }
1560
1561
        // If we reach here, then parent has to be removed from the trie as well.
1562
68
        let parent_index = removed_node.parent.unwrap().0;
1563
68
        debug_assert!(child_node_index.is_none());
1564
68
        let removed_branch = self.trie.nodes.remove(parent_index);
1565
68
        debug_assert!(!removed_branch.has_storage_value);
1566
1567
        // We already know from above that the removed branch has exactly 1 sibling. Let's
1568
        // determine which.
1569
68
        debug_assert_eq!(
1570
68
            removed_branch
1571
68
                .children
1572
68
                .iter()
1573
68
                .filter(|c| c.is_some())
1574
68
                .count(),
1575
            1
1576
        );
1577
68
        let sibling_node_index: usize = removed_branch.children.iter().find_map(|c| *c).unwrap();
1578
1579
        // Update the sibling to point to the parent's parent.
1580
        {
1581
68
            let sibling = self.trie.nodes.get_mut(sibling_node_index).unwrap();
1582
68
            debug_assert_eq!(sibling.parent.as_ref().unwrap().0, parent_index);
1583
68
            insert_front(
1584
68
                &mut sibling.partial_key,
1585
68
                removed_branch.partial_key,
1586
68
                sibling.parent.unwrap().1,
1587
            );
1588
68
            sibling.parent = removed_branch.parent;
1589
        }
1590
1591
        // Update the parent's parent to point to the sibling.
1592
68
        if let Some((
parent_parent_index63
,
parent_to_sibling_index63
)) = removed_branch.parent {
1593
            // Update the parent's parent to point to the sibling.
1594
63
            let parent_parent = self.trie.nodes.get_mut(parent_parent_index).unwrap();
1595
63
            debug_assert_eq!(
1596
63
                parent_parent.children[usize::from(u8::from(parent_to_sibling_index))],
1597
63
                Some(parent_index)
1598
            );
1599
63
            parent_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
1600
63
                Some(sibling_node_index);
1601
        } else {
1602
5
            debug_assert_eq!(self.trie.root_index, Some(parent_index));
1603
5
            self.trie.root_index = Some(sibling_node_index);
1604
        }
1605
1606
        // Success!
1607
68
        Remove::BranchAlsoRemoved {
1608
68
            sibling: self.trie.node_by_index_inner(sibling_node_index).unwrap(),
1609
68
            storage_user_data: removed_node.user_data,
1610
68
            branch_user_data: removed_branch.user_data,
1611
68
        }
1612
85.9k
    }
_RNvMs5_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_17StorageNodeAccessuE6removeB9_
Line
Count
Source
1482
48.6k
    pub fn remove(self) -> Remove<'a, TUd> {
1483
        // If the removed node has 2 or more children, then the node continues as a branch node.
1484
        {
1485
48.6k
            let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1486
48.6k
            if node.children.iter().filter(|c| c.is_some()).count() >= 2 {
1487
1.45k
                node.has_storage_value = false;
1488
1.45k
                return Remove::StorageToBranch(BranchNodeAccess {
1489
1.45k
                    trie: self.trie,
1490
1.45k
                    node_index: self.node_index,
1491
1.45k
                });
1492
47.2k
            }
1493
        }
1494
1495
47.2k
        let removed_node = self.trie.nodes.remove(self.node_index);
1496
47.2k
        debug_assert!(removed_node.has_storage_value);
1497
1498
        // We already know from above that the removed node has only 0 or 1 children. Let's
1499
        // determine which.
1500
47.2k
        let child_node_index: Option<usize> = removed_node.children.iter().find_map(|c| *c);
1501
1502
        // If relevant, update our single child's parent to point to `removed_node`'s parent.
1503
47.2k
        if let Some(
child_node_index3.16k
) = child_node_index {
1504
3.16k
            let child = self.trie.nodes.get_mut(child_node_index).unwrap();
1505
3.16k
            debug_assert_eq!(child.parent.as_ref().unwrap().0, self.node_index);
1506
3.16k
            insert_front(
1507
3.16k
                &mut child.partial_key,
1508
3.16k
                removed_node.partial_key,
1509
3.16k
                child.parent.unwrap().1,
1510
            );
1511
3.16k
            child.parent = removed_node.parent;
1512
44.0k
        }
1513
1514
        // At this point, we're almost done with removing `removed_node` from `self.trie`. However
1515
        // there is potentially another change to make: maybe `parent` has to be removed from the
1516
        // trie as well.
1517
1518
        // Update `parent`'s child to point to `child_node_index`.
1519
        // `single_remove` is true if we can keep `parent` in the trie.
1520
47.2k
        let single_remove = if let Some((
parent_index46.7k
,
parent_to_removed_child_index46.7k
)) =
1521
47.2k
            removed_node.parent
1522
        {
1523
            // Update `removed_node`'s parent to point to the child.
1524
46.7k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
1525
46.7k
            debug_assert_eq!(
1526
46.7k
                parent.children[usize::from(u8::from(parent_to_removed_child_index))],
1527
46.7k
                Some(self.node_index)
1528
            );
1529
46.7k
            parent.children[usize::from(u8::from(parent_to_removed_child_index))] =
1530
46.7k
                child_node_index;
1531
1532
            // If `parent` does *not* need to be removed, we can return early.
1533
46.7k
            parent.has_storage_value || 
parent.children23.0k
.
iter23.0k
().
filter23.0k
(|c| c.is_some()).
count23.0k
() >= 2
1534
        } else {
1535
410
            debug_assert_eq!(self.trie.root_index, Some(self.node_index));
1536
410
            self.trie.root_index = child_node_index;
1537
410
            true
1538
        };
1539
1540
        // If we keep the parent in the trie, return early with a `SingleRemove`.
1541
47.2k
        if single_remove {
1542
36.6k
            return if let Some(
child_node_index3.16k
) = child_node_index {
1543
3.16k
                Remove::SingleRemoveChild {
1544
3.16k
                    user_data: removed_node.user_data,
1545
3.16k
                    child: self.trie.node_by_index_inner(child_node_index).unwrap(),
1546
3.16k
                }
1547
33.5k
            } else if let Some((
parent_index33.1k
, _)) = removed_node.parent {
1548
33.1k
                Remove::SingleRemoveNoChild {
1549
33.1k
                    user_data: removed_node.user_data,
1550
33.1k
                    parent: self.trie.node_by_index_inner(parent_index).unwrap(),
1551
33.1k
                }
1552
            } else {
1553
376
                debug_assert!(self.trie.nodes.is_empty());
1554
376
                debug_assert!(self.trie.root_index.is_none());
1555
376
                Remove::TrieNowEmpty {
1556
376
                    user_data: removed_node.user_data,
1557
376
                }
1558
            };
1559
10.5k
        }
1560
1561
        // If we reach here, then parent has to be removed from the trie as well.
1562
10.5k
        let parent_index = removed_node.parent.unwrap().0;
1563
10.5k
        debug_assert!(child_node_index.is_none());
1564
10.5k
        let removed_branch = self.trie.nodes.remove(parent_index);
1565
10.5k
        debug_assert!(!removed_branch.has_storage_value);
1566
1567
        // We already know from above that the removed branch has exactly 1 sibling. Let's
1568
        // determine which.
1569
10.5k
        debug_assert_eq!(
1570
10.5k
            removed_branch
1571
10.5k
                .children
1572
10.5k
                .iter()
1573
10.5k
                .filter(|c| c.is_some())
1574
10.5k
                .count(),
1575
            1
1576
        );
1577
10.5k
        let sibling_node_index: usize = removed_branch.children.iter().find_map(|c| *c).unwrap();
1578
1579
        // Update the sibling to point to the parent's parent.
1580
        {
1581
10.5k
            let sibling = self.trie.nodes.get_mut(sibling_node_index).unwrap();
1582
10.5k
            debug_assert_eq!(sibling.parent.as_ref().unwrap().0, parent_index);
1583
10.5k
            insert_front(
1584
10.5k
                &mut sibling.partial_key,
1585
10.5k
                removed_branch.partial_key,
1586
10.5k
                sibling.parent.unwrap().1,
1587
            );
1588
10.5k
            sibling.parent = removed_branch.parent;
1589
        }
1590
1591
        // Update the parent's parent to point to the sibling.
1592
10.5k
        if let Some((
parent_parent_index9.70k
,
parent_to_sibling_index9.70k
)) = removed_branch.parent {
1593
            // Update the parent's parent to point to the sibling.
1594
9.70k
            let parent_parent = self.trie.nodes.get_mut(parent_parent_index).unwrap();
1595
9.70k
            debug_assert_eq!(
1596
9.70k
                parent_parent.children[usize::from(u8::from(parent_to_sibling_index))],
1597
9.70k
                Some(parent_index)
1598
            );
1599
9.70k
            parent_parent.children[usize::from(u8::from(parent_to_sibling_index))] =
1600
9.70k
                Some(sibling_node_index);
1601
        } else {
1602
821
            debug_assert_eq!(self.trie.root_index, Some(parent_index));
1603
821
            self.trie.root_index = Some(sibling_node_index);
1604
        }
1605
1606
        // Success!
1607
10.5k
        Remove::BranchAlsoRemoved {
1608
10.5k
            sibling: self.trie.node_by_index_inner(sibling_node_index).unwrap(),
1609
10.5k
            storage_user_data: removed_node.user_data,
1610
10.5k
            branch_user_data: removed_branch.user_data,
1611
10.5k
        }
1612
48.6k
    }
Unexecuted instantiation: _RNvMs5_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_17StorageNodeAccesspE6removeB9_
1613
}
1614
1615
/// Outcome of the removal of a storage value.
1616
pub enum Remove<'a, TUd> {
1617
    /// Removing the storage value didn't change the structure of the trie. Contains a
1618
    /// [`BranchNodeAccess`] representing the same node as the [`StorageNodeAccess`] whose value
1619
    /// got removed.
1620
    StorageToBranch(BranchNodeAccess<'a, TUd>),
1621
1622
    /// Removing the storage value removed the node that contained the storage value. Apart from
1623
    /// this removal, the structure of the trie didn't change.
1624
    ///
1625
    /// The node that got removed had one single child. This child's parent becomes the parent
1626
    /// that the former node had.
1627
    ///
1628
    /// ```text
1629
    ///
1630
    ///
1631
    ///                +-+                                         +-+
1632
    ///           +--> +-+ <--+                         +--------> +-+ <--+
1633
    ///           |           |                         |                 |
1634
    ///           |           +                         |                 +
1635
    ///           |     (0 or more other children)      |           (0 or more other children)
1636
    ///           |                                     |
1637
    ///          +-+                                    |
1638
    ///     +--> +-+ removed node                       |
1639
    ///     |                                           |
1640
    ///     |                                           |
1641
    ///     |                                           |
1642
    ///     |                                           |
1643
    ///    +-+                                         +-+
1644
    ///    +-+                                         +-+  `child`
1645
    ///     ^                                           ^
1646
    ///     ++ (0 or more other children)               ++ (0 or more other children)
1647
    ///
1648
    /// ```
1649
    ///
1650
    SingleRemoveChild {
1651
        /// Unique child that the removed node had. The parent and partial key of this child has
1652
        /// been modified.
1653
        child: NodeAccess<'a, TUd>,
1654
1655
        /// User data that was in the removed node.
1656
        user_data: TUd,
1657
    },
1658
1659
    /// Removing the storage value removed the node that contained the storage value. Apart from
1660
    /// this removal, the structure of the trie didn't change.
1661
    ///
1662
    /// The node that got removed didn't have any children.
1663
    ///
1664
    /// ```text
1665
    ///
1666
    ///       Before                                       After
1667
    ///
1668
    ///                                                        `parent`
1669
    ///                +-+                                 +-+
1670
    ///           +--> +-+ <--+                            +-+ <--+
1671
    ///           |           |                                   |
1672
    ///           |           +                                   +
1673
    ///           |       (0 or more other children)          (0 or more other children)
1674
    ///           |
1675
    ///          +-+
1676
    ///          +-+ removed node
1677
    ///
1678
    /// ```
1679
    ///
1680
    SingleRemoveNoChild {
1681
        /// Parent that the removed node had.
1682
        parent: NodeAccess<'a, TUd>,
1683
1684
        /// User data that was in the removed node.
1685
        user_data: TUd,
1686
    },
1687
1688
    /// The trie was empty apart from this node. It is now completely empty.
1689
    TrieNowEmpty {
1690
        /// User data that was in the removed node.
1691
        user_data: TUd,
1692
    },
1693
1694
    /// Removing the storage value removed two nodes from the trie: the one that contained the
1695
    /// storage value and its parent, which was a branch node.
1696
    ///
1697
    /// This can only happen if the removed node had no children and only one sibling.
1698
    ///
1699
    /// ```text
1700
    ///
1701
    ///             Before                        After
1702
    ///
1703
    ///
1704
    ///               +                             +
1705
    ///               |                             |
1706
    ///              +-+                            |
1707
    ///         +--> +-+ <--+                       +-----+
1708
    ///         |           |                             |
1709
    ///         |           |                             |
1710
    ///        +-+         +-+                           +-+
1711
    ///        +-+         +-+                           +-+ `sibling`
1712
    ///                     ^                             ^
1713
    ///  removed node       |                             |
1714
    ///                     +                             +
1715
    ///                (0 or more other nodes)       (0 or more other nodes)
1716
    ///
1717
    /// ```
1718
    ///
1719
    BranchAlsoRemoved {
1720
        /// Sibling of the removed node. The parent and partial key of this sibling have been
1721
        /// modified.
1722
        sibling: NodeAccess<'a, TUd>,
1723
1724
        /// User data that was in the removed storage node.
1725
        storage_user_data: TUd,
1726
1727
        /// User data that was in the removed branch node (former parent of `storage_user_data`).
1728
        branch_user_data: TUd,
1729
    },
1730
}
1731
1732
/// Access to a node within the [`TrieStructure`] that is known to not have any storage value
1733
/// associated to it.
1734
pub struct BranchNodeAccess<'a, TUd> {
1735
    trie: &'a mut TrieStructure<TUd>,
1736
    node_index: usize,
1737
}
1738
1739
impl<'a, TUd> BranchNodeAccess<'a, TUd> {
1740
    /// Returns an opaque [`NodeIndex`] representing the node in the trie.
1741
    ///
1742
    /// It can later be used to retrieve this same node using [`TrieStructure::node_by_index`].
1743
0
    pub fn node_index(&self) -> NodeIndex {
1744
0
        NodeIndex(self.node_index)
1745
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE10node_indexB9_
1746
1747
    /// Returns the parent of this node, or `None` if this is the root node.
1748
0
    pub fn into_parent(self) -> Option<NodeAccess<'a, TUd>> {
1749
0
        let parent_idx = self.trie.nodes.get(self.node_index).unwrap().parent?.0;
1750
0
        Some(self.trie.node_by_index_inner(parent_idx).unwrap())
1751
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE11into_parentB9_
1752
1753
    /// Returns the parent of this node, or `None` if this is the root node.
1754
0
    pub fn parent(&mut self) -> Option<NodeAccess<TUd>> {
1755
0
        let parent_idx = self.trie.nodes.get(self.node_index).unwrap().parent?.0;
1756
0
        Some(self.trie.node_by_index_inner(parent_idx).unwrap())
1757
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE6parentB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE6parentB9_
1758
1759
    /// Returns the first child of this node.
1760
    ///
1761
    /// Returns back `self` if this node doesn't have any children.
1762
0
    pub fn into_first_child(self) -> Result<NodeAccess<'a, TUd>, Self> {
1763
0
        let first_child_idx = self
1764
0
            .trie
1765
0
            .nodes
1766
0
            .get(self.node_index)
1767
0
            .unwrap()
1768
0
            .children
1769
0
            .iter()
1770
0
            .find_map(|c| *c);
1771
1772
0
        let first_child_idx = match first_child_idx {
1773
0
            Some(fc) => fc,
1774
0
            None => return Err(self),
1775
        };
1776
1777
0
        Ok(self.trie.node_by_index_inner(first_child_idx).unwrap())
1778
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE16into_first_childB9_
1779
1780
    /// Returns the next sibling of this node.
1781
    ///
1782
    /// Returns back `self` if this node is the last child of its parent.
1783
0
    pub fn into_next_sibling(self) -> Result<NodeAccess<'a, TUd>, Self> {
1784
0
        let next_sibling_idx = match self.trie.next_sibling(self.node_index) {
1785
0
            Some(ns) => ns,
1786
0
            None => return Err(self),
1787
        };
1788
1789
0
        Ok(self.trie.node_by_index_inner(next_sibling_idx).unwrap())
1790
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE17into_next_siblingB9_
1791
1792
    /// Returns the child of this node at the given index.
1793
1.13M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1794
206k
        let child_idx =
1795
1.13M
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?932k
;
1796
206k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1797
1.13M
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1793
2.56k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1794
322
        let child_idx =
1795
2.56k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?2.23k
;
1796
322
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1797
2.56k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE5childB9_
Line
Count
Source
1793
1.12M
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1794
204k
        let child_idx =
1795
1.12M
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?923k
;
1796
204k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1797
1.12M
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE5childCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE5childB9_
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE5childCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1793
832
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1794
188
        let child_idx =
1795
832
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?644
;
1796
188
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1797
832
    }
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE5childCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1793
7.90k
    pub fn child(&mut self, index: Nibble) -> Option<NodeAccess<TUd>> {
1794
1.78k
        let child_idx =
1795
7.90k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?6.11k
;
1796
1.78k
        Some(self.trie.node_by_index_inner(child_idx).unwrap())
1797
7.90k
    }
1798
1799
    /// Returns the user data of the child at the given index.
1800
    ///
1801
    /// > **Note**: This method exists because it accepts `&self` rather than `&mut self`. A
1802
    /// >           cleaner alternative would be to split the [`NodeAccess`] struct into
1803
    /// >           `NodeAccessRef` and `NodeAccessMut`, but that's a lot of efforts compare to
1804
    /// >           this single method.
1805
91.1k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1806
15.7k
        let child_idx =
1807
91.1k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?75.4k
;
1808
15.7k
        Some(&self.trie.nodes.get(child_idx).unwrap().user_data)
1809
91.1k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessuE15child_user_dataB9_
Line
Count
Source
1805
91.1k
    pub fn child_user_data(&self, index: Nibble) -> Option<&TUd> {
1806
15.7k
        let child_idx =
1807
91.1k
            self.trie.nodes.get(self.node_index).unwrap().children[usize::from(u8::from(index))]
?75.4k
;
1808
15.7k
        Some(&self.trie.nodes.get(child_idx).unwrap().user_data)
1809
91.1k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15child_user_dataB9_
1810
1811
    /// Returns the child of this node given the given index.
1812
    ///
1813
    /// Returns back `self` if there is no such child at this index.
1814
0
    pub fn into_child(self, index: Nibble) -> Result<NodeAccess<'a, TUd>, Self> {
1815
0
        let child_idx = match self.trie.nodes.get(self.node_index).unwrap().children
1816
0
            [usize::from(u8::from(index))]
1817
        {
1818
0
            Some(c) => c,
1819
0
            None => return Err(self),
1820
        };
1821
1822
0
        Ok(self.trie.node_by_index_inner(child_idx).unwrap())
1823
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE10into_childB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE10into_childB9_
1824
1825
    /// Returns true if this node is the root node of the trie.
1826
70.7k
    pub fn is_root_node(&self) -> bool {
1827
70.7k
        self.trie.root_index == Some(self.node_index)
1828
70.7k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1826
70.4k
    pub fn is_root_node(&self) -> bool {
1827
70.4k
        self.trie.root_index == Some(self.node_index)
1828
70.4k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeB9_
Line
Count
Source
1826
80
    pub fn is_root_node(&self) -> bool {
1827
80
        self.trie.root_index == Some(self.node_index)
1828
80
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE12is_root_nodeB9_
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1826
26
    pub fn is_root_node(&self) -> bool {
1827
26
        self.trie.root_index == Some(self.node_index)
1828
26
    }
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE12is_root_nodeCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1826
247
    pub fn is_root_node(&self) -> bool {
1827
247
        self.trie.root_index == Some(self.node_index)
1828
247
    }
1829
1830
    /// Returns the full key of the node.
1831
0
    pub fn full_key(&self) -> impl Iterator<Item = Nibble> {
1832
0
        self.trie.node_full_key(self.node_index)
1833
0
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE8full_keyB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE8full_keyB9_
1834
1835
    /// Returns the partial key of the node.
1836
76.8k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
76.8k
        self.trie
1838
76.8k
            .nodes
1839
76.8k
            .get(self.node_index)
1840
76.8k
            .unwrap()
1841
76.8k
            .partial_key
1842
76.8k
            .iter()
1843
76.8k
            .copied()
1844
76.8k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1836
160
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
160
        self.trie
1838
160
            .nodes
1839
160
            .get(self.node_index)
1840
160
            .unwrap()
1841
160
            .partial_key
1842
160
            .iter()
1843
160
            .copied()
1844
160
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyB9_
Line
Count
Source
1836
70.4k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
70.4k
        self.trie
1838
70.4k
            .nodes
1839
70.4k
            .get(self.node_index)
1840
70.4k
            .unwrap()
1841
70.4k
            .partial_key
1842
70.4k
            .iter()
1843
70.4k
            .copied()
1844
70.4k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessuE11partial_keyB9_
Line
Count
Source
1836
5.69k
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
5.69k
        self.trie
1838
5.69k
            .nodes
1839
5.69k
            .get(self.node_index)
1840
5.69k
            .unwrap()
1841
5.69k
            .partial_key
1842
5.69k
            .iter()
1843
5.69k
            .copied()
1844
5.69k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE11partial_keyB9_
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1836
52
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
52
        self.trie
1838
52
            .nodes
1839
52
            .get(self.node_index)
1840
52
            .unwrap()
1841
52
            .partial_key
1842
52
            .iter()
1843
52
            .copied()
1844
52
    }
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE11partial_keyCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1836
494
    pub fn partial_key(&self) -> impl ExactSizeIterator<Item = Nibble> + Clone {
1837
494
        self.trie
1838
494
            .nodes
1839
494
            .get(self.node_index)
1840
494
            .unwrap()
1841
494
            .partial_key
1842
494
            .iter()
1843
494
            .copied()
1844
494
    }
1845
1846
    /// Adds a storage value to this node, turning it into a [`StorageNodeAccess`].
1847
    ///
1848
    /// The trie structure doesn't change.
1849
165k
    pub fn insert_storage_value(self) -> StorageNodeAccess<'a, TUd> {
1850
165k
        let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1851
165k
        debug_assert!(!node.has_storage_value);
1852
165k
        node.has_storage_value = true;
1853
1854
165k
        StorageNodeAccess {
1855
165k
            trie: self.trie,
1856
165k
            node_index: self.node_index,
1857
165k
        }
1858
165k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE20insert_storage_valueB9_
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE20insert_storage_valueB9_
Line
Count
Source
1849
66.6k
    pub fn insert_storage_value(self) -> StorageNodeAccess<'a, TUd> {
1850
66.6k
        let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1851
66.6k
        debug_assert!(!node.has_storage_value);
1852
66.6k
        node.has_storage_value = true;
1853
1854
66.6k
        StorageNodeAccess {
1855
66.6k
            trie: self.trie,
1856
66.6k
            node_index: self.node_index,
1857
66.6k
        }
1858
66.6k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessuE20insert_storage_valueB9_
Line
Count
Source
1849
98.4k
    pub fn insert_storage_value(self) -> StorageNodeAccess<'a, TUd> {
1850
98.4k
        let node = self.trie.nodes.get_mut(self.node_index).unwrap();
1851
98.4k
        debug_assert!(!node.has_storage_value);
1852
98.4k
        node.has_storage_value = true;
1853
1854
98.4k
        StorageNodeAccess {
1855
98.4k
            trie: self.trie,
1856
98.4k
            node_index: self.node_index,
1857
98.4k
        }
1858
98.4k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE20insert_storage_valueCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE20insert_storage_valueB9_
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE20insert_storage_valueCsjyNE3yDMkgA_14json_rpc_basic
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE20insert_storage_valueCs4VrkfB1pvQ3_25json_rpc_general_requests
1859
1860
    /// Returns the user data associated to this node.
1861
71.4k
    pub fn into_user_data(self) -> &'a mut TUd {
1862
71.4k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1863
71.4k
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1861
80
    pub fn into_user_data(self) -> &'a mut TUd {
1862
80
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1863
80
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataB9_
Line
Count
Source
1861
71.1k
    pub fn into_user_data(self) -> &'a mut TUd {
1862
71.1k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1863
71.1k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccesspE14into_user_dataB9_
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1861
26
    pub fn into_user_data(self) -> &'a mut TUd {
1862
26
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1863
26
    }
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE14into_user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1861
247
    pub fn into_user_data(self) -> &'a mut TUd {
1862
247
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1863
247
    }
1864
1865
    /// Returns the user data associated to this node.
1866
268k
    pub fn user_data(&mut self) -> &mut TUd {
1867
268k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1868
268k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1866
240
    pub fn user_data(&mut self) -> &mut TUd {
1867
240
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1868
240
    }
_RNvMs6_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataB9_
Line
Count
Source
1866
267k
    pub fn user_data(&mut self) -> &mut TUd {
1867
267k
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1868
267k
    }
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE9user_dataB9_
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1866
100
    pub fn user_data(&mut self) -> &mut TUd {
1867
100
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1868
100
    }
_RNvMs6_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16BranchNodeAccessTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE9user_dataCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1866
950
    pub fn user_data(&mut self) -> &mut TUd {
1867
950
        &mut self.trie.nodes.get_mut(self.node_index).unwrap().user_data
1868
950
    }
1869
}
1870
1871
/// Access to a non-existing node within the [`TrieStructure`].
1872
pub struct Vacant<'a, TUd, TKIter> {
1873
    trie: &'a mut TrieStructure<TUd>,
1874
    /// Full key of the node to insert.
1875
    key: TKIter,
1876
    /// Known closest ancestor that is in `trie`. Will become the parent of any newly-inserted
1877
    /// node.
1878
    closest_ancestor: Option<usize>,
1879
}
1880
1881
impl<'a, TUd, TKIter> Vacant<'a, TUd, TKIter>
1882
where
1883
    TKIter: Iterator<Item = Nibble> + Clone,
1884
{
1885
    /// Prepare the operation of creating the node in question.
1886
    ///
1887
    /// This method analyzes the trie to prepare for the operation, but doesn't actually perform
1888
    /// any insertion. To perform the insertion, use the returned [`PrepareInsert`].
1889
1.35M
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
1.35M
        let 
future_parent1.30M
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
1.28M
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
1.28M
                let key_len = self.trie.node_full_key(ancestor).count();
1898
1.28M
                debug_assert!(self.key.clone().count() > key_len);
1899
1.28M
                Some((ancestor, key_len))
1900
            }
1901
24.3k
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
53.7k
                return PrepareInsert::One(PrepareInsertOne {
1906
53.7k
                    trie: self.trie,
1907
53.7k
                    parent: None,
1908
53.7k
                    partial_key: self.key.collect(),
1909
53.7k
                    children: [None; 16],
1910
53.7k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
343k
        let existing_node_index =
1917
1.30M
            if let Some((
future_parent_index1.28M
,
future_parent_key_len1.28M
)) = future_parent {
1918
1.28M
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
1.28M
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
1.28M
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
319k
                    Some(i) => {
1922
319k
                        debug_assert_eq!(
1923
319k
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
319k
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
961k
                        return PrepareInsert::One(PrepareInsertOne {
1943
961k
                            trie: self.trie,
1944
961k
                            parent: Some((future_parent_index, new_child_index)),
1945
961k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
961k
                            children: [None; 16],
1947
961k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
24.3k
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
343k
        let existing_node_partial_key = &self
1957
343k
            .trie
1958
343k
            .nodes
1959
343k
            .get(existing_node_index)
1960
343k
            .unwrap()
1961
343k
            .partial_key;
1962
343k
        let new_node_partial_key = self
1963
343k
            .key
1964
343k
            .clone()
1965
343k
            .skip(future_parent.map_or(0, |(_, n)| 
n319k
+ 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB9_12proof_encode4NodeEINtNtNtNtB1a_4iter8adapters6copied6CopiedINtNtNtB1a_5slice4iter4IterNtNtB9_6nibble6NibbleEEE20insert_storage_value0Bb_
Line
Count
Source
1965
5.26k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB17_NtNtB9_9trie_node17MerkleValueOutputEEINtNtB9_6nibble14BytesToNibblesINtNtNtNtB1b_4iter8adapters6copied6CopiedINtNtNtB1b_5slice4iter4IterhEEEE20insert_storage_value0Bb_
Line
Count
Source
1965
81
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB17_NtNtB9_9trie_node17MerkleValueOutputEEINtNtB9_6nibble14BytesToNibblesINtNtNtNtB1b_4iter8adapters6copied6CopiedINtNtNtB1b_5slice4iter4IterhEEEE20insert_storage_value0Bb_
Line
Count
Source
1965
58.1k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantuINtNtB9_6nibble14BytesToNibblesINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1K_5slice4iter4IterhEEEE20insert_storage_value0Bb_
Line
Count
Source
1965
130
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantuINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB9_6nibble6NibbleEE20insert_storage_value0Bb_
Line
Count
Source
1965
242k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1f_5slice4iter4IterNtNtB9_6nibble6NibbleEEE20insert_storage_value0Bb_
Line
Count
Source
1965
4
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_6VacantuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1f_5slice4iter4IterNtNtB9_6nibble6NibbleEEE20insert_storage_value0Bb_
Line
Count
Source
1965
12.6k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
Unexecuted instantiation: _RNCNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB17_NtNtB9_9trie_node17MerkleValueOutputEEINtNtB9_6nibble14BytesToNibblesINtNtNtNtB1b_4iter8adapters6copied6CopiedINtNtNtB1b_5slice4iter4IterhEEEE20insert_storage_value0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_6VacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB9_12proof_encode4NodeEINtNtNtNtB1a_4iter8adapters6copied6CopiedINtNtNtB1a_5slice4iter4IterNtNtB9_6nibble6NibbleEEE20insert_storage_value0Bb_
_RNCNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB17_NtNtB9_9trie_node17MerkleValueOutputEEINtNtB9_6nibble14BytesToNibblesINtNtNtNtB1b_4iter8adapters6copied6CopiedINtNtNtB1b_5slice4iter4IterhEEEE20insert_storage_value0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1965
24
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
_RNCNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB17_NtNtB9_9trie_node17MerkleValueOutputEEINtNtB9_6nibble14BytesToNibblesINtNtNtNtB1b_4iter8adapters6copied6CopiedINtNtNtB1b_5slice4iter4IterhEEEE20insert_storage_value0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1965
228
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
343k
            .collect::<Vec<_>>();
1967
343k
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
343k
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
343k
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
100k
            let mut new_node_children = [None; 16];
2012
100k
            let existing_node_new_child_index =
2013
100k
                existing_node_partial_key[new_node_partial_key.len()];
2014
100k
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
100k
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
100k
                trie: self.trie,
2019
100k
                parent: if let Some((
future_parent_index92.7k
,
future_parent_key_len92.7k
)) = future_parent {
2020
92.7k
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
92.7k
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
8.09k
                    None
2024
                },
2025
100k
                partial_key: new_node_partial_key,
2026
100k
                children: new_node_children,
2027
            });
2028
242k
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
242k
        let branch_partial_key_len = {
2073
242k
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
242k
            let mut len = 0;
2075
242k
            let mut k1 = new_node_partial_key.iter();
2076
242k
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
365k
            while k1.next() == k2.next() {
2080
122k
                len += 1;
2081
122k
            }
2082
242k
            debug_assert!(len < new_node_partial_key.len());
2083
242k
            debug_assert!(len < existing_node_partial_key.len());
2084
242k
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
242k
        let branch_children = {
2090
242k
            let mut branch_children = [None; 16];
2091
242k
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
242k
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
242k
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
242k
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
242k
                Some(existing_node_index);
2098
242k
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
242k
            trie: self.trie,
2104
2105
242k
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
242k
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
242k
            branch_parent: if let Some((
future_parent_index226k
,
future_parent_key_len226k
)) = future_parent
2109
            {
2110
226k
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
226k
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
16.2k
                None
2114
            },
2115
242k
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
242k
            branch_children,
2117
        })
2118
1.35M
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEINtNtNtNtB18_4iter8adapters6copied6CopiedINtNtNtB18_5slice4iter4IterNtNtB7_6nibble6NibbleEEE20insert_storage_valueB9_
Line
Count
Source
1889
29.2k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
29.2k
        let 
future_parent27.7k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
25.6k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
25.6k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
25.6k
                debug_assert!(self.key.clone().count() > key_len);
1899
25.6k
                Some((ancestor, key_len))
1900
            }
1901
2.03k
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
1.50k
                return PrepareInsert::One(PrepareInsertOne {
1906
1.50k
                    trie: self.trie,
1907
1.50k
                    parent: None,
1908
1.50k
                    partial_key: self.key.collect(),
1909
1.50k
                    children: [None; 16],
1910
1.50k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
7.30k
        let existing_node_index =
1917
27.7k
            if let Some((
future_parent_index25.6k
,
future_parent_key_len25.6k
)) = future_parent {
1918
25.6k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
25.6k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
25.6k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
5.26k
                    Some(i) => {
1922
5.26k
                        debug_assert_eq!(
1923
5.26k
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
5.26k
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
20.4k
                        return PrepareInsert::One(PrepareInsertOne {
1943
20.4k
                            trie: self.trie,
1944
20.4k
                            parent: Some((future_parent_index, new_child_index)),
1945
20.4k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
20.4k
                            children: [None; 16],
1947
20.4k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
2.03k
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
7.30k
        let existing_node_partial_key = &self
1957
7.30k
            .trie
1958
7.30k
            .nodes
1959
7.30k
            .get(existing_node_index)
1960
7.30k
            .unwrap()
1961
7.30k
            .partial_key;
1962
7.30k
        let new_node_partial_key = self
1963
7.30k
            .key
1964
7.30k
            .clone()
1965
7.30k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
7.30k
            .collect::<Vec<_>>();
1967
7.30k
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
7.30k
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
7.30k
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
7.30k
            let mut new_node_children = [None; 16];
2012
7.30k
            let existing_node_new_child_index =
2013
7.30k
                existing_node_partial_key[new_node_partial_key.len()];
2014
7.30k
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
7.30k
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
7.30k
                trie: self.trie,
2019
7.30k
                parent: if let Some((
future_parent_index5.26k
,
future_parent_key_len5.26k
)) = future_parent {
2020
5.26k
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
5.26k
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
2.03k
                    None
2024
                },
2025
7.30k
                partial_key: new_node_partial_key,
2026
7.30k
                children: new_node_children,
2027
            });
2028
0
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
0
        let branch_partial_key_len = {
2073
0
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
0
            let mut len = 0;
2075
0
            let mut k1 = new_node_partial_key.iter();
2076
0
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
0
            while k1.next() == k2.next() {
2080
0
                len += 1;
2081
0
            }
2082
0
            debug_assert!(len < new_node_partial_key.len());
2083
0
            debug_assert!(len < existing_node_partial_key.len());
2084
0
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
0
        let branch_children = {
2090
0
            let mut branch_children = [None; 16];
2091
0
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
0
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
0
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
0
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
0
                Some(existing_node_index);
2098
0
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
0
            trie: self.trie,
2104
2105
0
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
0
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
0
            branch_parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent
2109
            {
2110
0
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
0
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
0
                None
2114
            },
2115
0
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
0
            branch_children,
2117
        })
2118
29.2k
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB15_NtNtB7_9trie_node17MerkleValueOutputEEINtNtB7_6nibble14BytesToNibblesINtNtNtNtB19_4iter8adapters6copied6CopiedINtNtNtB19_5slice4iter4IterhEEEE20insert_storage_valueB9_
Line
Count
Source
1889
2.57k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
2.57k
        let 
future_parent1.54k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
1.54k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
1.54k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
1.54k
                debug_assert!(self.key.clone().count() > key_len);
1899
1.54k
                Some((ancestor, key_len))
1900
            }
1901
0
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
1.02k
                return PrepareInsert::One(PrepareInsertOne {
1906
1.02k
                    trie: self.trie,
1907
1.02k
                    parent: None,
1908
1.02k
                    partial_key: self.key.collect(),
1909
1.02k
                    children: [None; 16],
1910
1.02k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
81
        let existing_node_index =
1917
1.54k
            if let Some((future_parent_index, future_parent_key_len)) = future_parent {
1918
1.54k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
1.54k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
1.54k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
81
                    Some(i) => {
1922
81
                        debug_assert_eq!(
1923
81
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
81
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
1.46k
                        return PrepareInsert::One(PrepareInsertOne {
1943
1.46k
                            trie: self.trie,
1944
1.46k
                            parent: Some((future_parent_index, new_child_index)),
1945
1.46k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
1.46k
                            children: [None; 16],
1947
1.46k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
0
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
81
        let existing_node_partial_key = &self
1957
81
            .trie
1958
81
            .nodes
1959
81
            .get(existing_node_index)
1960
81
            .unwrap()
1961
81
            .partial_key;
1962
81
        let new_node_partial_key = self
1963
81
            .key
1964
81
            .clone()
1965
81
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
81
            .collect::<Vec<_>>();
1967
81
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
81
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
81
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
1
            let mut new_node_children = [None; 16];
2012
1
            let existing_node_new_child_index =
2013
1
                existing_node_partial_key[new_node_partial_key.len()];
2014
1
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
1
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
1
                trie: self.trie,
2019
1
                parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent {
2020
1
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
1
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
0
                    None
2024
                },
2025
1
                partial_key: new_node_partial_key,
2026
1
                children: new_node_children,
2027
            });
2028
80
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
80
        let branch_partial_key_len = {
2073
80
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
80
            let mut len = 0;
2075
80
            let mut k1 = new_node_partial_key.iter();
2076
80
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
83
            while k1.next() == k2.next() {
2080
3
                len += 1;
2081
3
            }
2082
80
            debug_assert!(len < new_node_partial_key.len());
2083
80
            debug_assert!(len < existing_node_partial_key.len());
2084
80
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
80
        let branch_children = {
2090
80
            let mut branch_children = [None; 16];
2091
80
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
80
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
80
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
80
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
80
                Some(existing_node_index);
2098
80
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
80
            trie: self.trie,
2104
2105
80
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
80
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
80
            branch_parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent
2109
            {
2110
80
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
80
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
0
                None
2114
            },
2115
80
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
80
            branch_children,
2117
        })
2118
2.57k
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB15_NtNtB7_9trie_node17MerkleValueOutputEEINtNtB7_6nibble14BytesToNibblesINtNtNtNtB19_4iter8adapters6copied6CopiedINtNtNtB19_5slice4iter4IterhEEEE20insert_storage_valueB9_
Line
Count
Source
1889
336k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
336k
        let 
future_parent297k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
284k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
284k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
284k
                debug_assert!(self.key.clone().count() > key_len);
1899
284k
                Some((ancestor, key_len))
1900
            }
1901
13.0k
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
38.8k
                return PrepareInsert::One(PrepareInsertOne {
1906
38.8k
                    trie: self.trie,
1907
38.8k
                    parent: None,
1908
38.8k
                    partial_key: self.key.collect(),
1909
38.8k
                    children: [None; 16],
1910
38.8k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
71.2k
        let existing_node_index =
1917
297k
            if let Some((
future_parent_index284k
,
future_parent_key_len284k
)) = future_parent {
1918
284k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
284k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
284k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
58.1k
                    Some(i) => {
1922
58.1k
                        debug_assert_eq!(
1923
58.1k
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
58.1k
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
226k
                        return PrepareInsert::One(PrepareInsertOne {
1943
226k
                            trie: self.trie,
1944
226k
                            parent: Some((future_parent_index, new_child_index)),
1945
226k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
226k
                            children: [None; 16],
1947
226k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
13.0k
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
71.2k
        let existing_node_partial_key = &self
1957
71.2k
            .trie
1958
71.2k
            .nodes
1959
71.2k
            .get(existing_node_index)
1960
71.2k
            .unwrap()
1961
71.2k
            .partial_key;
1962
71.2k
        let new_node_partial_key = self
1963
71.2k
            .key
1964
71.2k
            .clone()
1965
71.2k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
71.2k
            .collect::<Vec<_>>();
1967
71.2k
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
71.2k
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
71.2k
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
6.85k
            let mut new_node_children = [None; 16];
2012
6.85k
            let existing_node_new_child_index =
2013
6.85k
                existing_node_partial_key[new_node_partial_key.len()];
2014
6.85k
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
6.85k
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
6.85k
                trie: self.trie,
2019
6.85k
                parent: if let Some((
future_parent_index1.15k
,
future_parent_key_len1.15k
)) = future_parent {
2020
1.15k
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
1.15k
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
5.70k
                    None
2024
                },
2025
6.85k
                partial_key: new_node_partial_key,
2026
6.85k
                children: new_node_children,
2027
            });
2028
64.4k
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
64.4k
        let branch_partial_key_len = {
2073
64.4k
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
64.4k
            let mut len = 0;
2075
64.4k
            let mut k1 = new_node_partial_key.iter();
2076
64.4k
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
66.5k
            while k1.next() == k2.next() {
2080
2.17k
                len += 1;
2081
2.17k
            }
2082
64.4k
            debug_assert!(len < new_node_partial_key.len());
2083
64.4k
            debug_assert!(len < existing_node_partial_key.len());
2084
64.4k
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
64.4k
        let branch_children = {
2090
64.4k
            let mut branch_children = [None; 16];
2091
64.4k
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
64.4k
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
64.4k
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
64.4k
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
64.4k
                Some(existing_node_index);
2098
64.4k
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
64.4k
            trie: self.trie,
2104
2105
64.4k
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
64.4k
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
64.4k
            branch_parent: if let Some((
future_parent_index57.0k
,
future_parent_key_len57.0k
)) = future_parent
2109
            {
2110
57.0k
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
57.0k
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
7.38k
                None
2114
            },
2115
64.4k
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
64.4k
            branch_children,
2117
        })
2118
336k
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantuINtNtB7_6nibble14BytesToNibblesINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1I_5slice4iter4IterhEEEE20insert_storage_valueB9_
Line
Count
Source
1889
5.18k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
5.18k
        let 
future_parent3.13k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
3.13k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
3.13k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
3.13k
                debug_assert!(self.key.clone().count() > key_len);
1899
3.13k
                Some((ancestor, key_len))
1900
            }
1901
0
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
2.04k
                return PrepareInsert::One(PrepareInsertOne {
1906
2.04k
                    trie: self.trie,
1907
2.04k
                    parent: None,
1908
2.04k
                    partial_key: self.key.collect(),
1909
2.04k
                    children: [None; 16],
1910
2.04k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
130
        let existing_node_index =
1917
3.13k
            if let Some((future_parent_index, future_parent_key_len)) = future_parent {
1918
3.13k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
3.13k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
3.13k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
130
                    Some(i) => {
1922
130
                        debug_assert_eq!(
1923
130
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
130
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
3.00k
                        return PrepareInsert::One(PrepareInsertOne {
1943
3.00k
                            trie: self.trie,
1944
3.00k
                            parent: Some((future_parent_index, new_child_index)),
1945
3.00k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
3.00k
                            children: [None; 16],
1947
3.00k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
0
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
130
        let existing_node_partial_key = &self
1957
130
            .trie
1958
130
            .nodes
1959
130
            .get(existing_node_index)
1960
130
            .unwrap()
1961
130
            .partial_key;
1962
130
        let new_node_partial_key = self
1963
130
            .key
1964
130
            .clone()
1965
130
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
130
            .collect::<Vec<_>>();
1967
130
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
130
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
130
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
0
            let mut new_node_children = [None; 16];
2012
0
            let existing_node_new_child_index =
2013
0
                existing_node_partial_key[new_node_partial_key.len()];
2014
0
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
0
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
0
                trie: self.trie,
2019
0
                parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent {
2020
0
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
0
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
0
                    None
2024
                },
2025
0
                partial_key: new_node_partial_key,
2026
0
                children: new_node_children,
2027
            });
2028
130
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
130
        let branch_partial_key_len = {
2073
130
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
130
            let mut len = 0;
2075
130
            let mut k1 = new_node_partial_key.iter();
2076
130
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
132
            while k1.next() == k2.next() {
2080
2
                len += 1;
2081
2
            }
2082
130
            debug_assert!(len < new_node_partial_key.len());
2083
130
            debug_assert!(len < existing_node_partial_key.len());
2084
130
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
130
        let branch_children = {
2090
130
            let mut branch_children = [None; 16];
2091
130
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
130
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
130
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
130
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
130
                Some(existing_node_index);
2098
130
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
130
            trie: self.trie,
2104
2105
130
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
130
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
130
            branch_parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent
2109
            {
2110
130
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
130
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
0
                None
2114
            },
2115
130
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
130
            branch_children,
2117
        })
2118
5.18k
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantuINtNtNtCsaFPxhswmqCN_5alloc3vec9into_iter8IntoIterNtNtB7_6nibble6NibbleEE20insert_storage_valueB9_
Line
Count
Source
1889
742k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
742k
        let 
future_parent736k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
727k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
727k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
727k
                debug_assert!(self.key.clone().count() > key_len);
1899
727k
                Some((ancestor, key_len))
1900
            }
1901
8.61k
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
6.21k
                return PrepareInsert::One(PrepareInsertOne {
1906
6.21k
                    trie: self.trie,
1907
6.21k
                    parent: None,
1908
6.21k
                    partial_key: self.key.collect(),
1909
6.21k
                    children: [None; 16],
1910
6.21k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
251k
        let existing_node_index =
1917
736k
            if let Some((
future_parent_index727k
,
future_parent_key_len727k
)) = future_parent {
1918
727k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
727k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
727k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
242k
                    Some(i) => {
1922
242k
                        debug_assert_eq!(
1923
242k
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
242k
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
485k
                        return PrepareInsert::One(PrepareInsertOne {
1943
485k
                            trie: self.trie,
1944
485k
                            parent: Some((future_parent_index, new_child_index)),
1945
485k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
485k
                            children: [None; 16],
1947
485k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
8.61k
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
251k
        let existing_node_partial_key = &self
1957
251k
            .trie
1958
251k
            .nodes
1959
251k
            .get(existing_node_index)
1960
251k
            .unwrap()
1961
251k
            .partial_key;
1962
251k
        let new_node_partial_key = self
1963
251k
            .key
1964
251k
            .clone()
1965
251k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
251k
            .collect::<Vec<_>>();
1967
251k
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
251k
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
251k
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
86.6k
            let mut new_node_children = [None; 16];
2012
86.6k
            let existing_node_new_child_index =
2013
86.6k
                existing_node_partial_key[new_node_partial_key.len()];
2014
86.6k
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
86.6k
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
86.6k
                trie: self.trie,
2019
86.6k
                parent: if let Some((
future_parent_index86.3k
,
future_parent_key_len86.3k
)) = future_parent {
2020
86.3k
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
86.3k
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
345
                    None
2024
                },
2025
86.6k
                partial_key: new_node_partial_key,
2026
86.6k
                children: new_node_children,
2027
            });
2028
164k
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
164k
        let branch_partial_key_len = {
2073
164k
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
164k
            let mut len = 0;
2075
164k
            let mut k1 = new_node_partial_key.iter();
2076
164k
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
280k
            while k1.next() == k2.next() {
2080
116k
                len += 1;
2081
116k
            }
2082
164k
            debug_assert!(len < new_node_partial_key.len());
2083
164k
            debug_assert!(len < existing_node_partial_key.len());
2084
164k
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
164k
        let branch_children = {
2090
164k
            let mut branch_children = [None; 16];
2091
164k
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
164k
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
164k
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
164k
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
164k
                Some(existing_node_index);
2098
164k
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
164k
            trie: self.trie,
2104
2105
164k
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
164k
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
164k
            branch_parent: if let Some((
future_parent_index156k
,
future_parent_key_len156k
)) = future_parent
2109
            {
2110
156k
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
156k
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
8.27k
                None
2114
            },
2115
164k
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
164k
            branch_children,
2117
        })
2118
742k
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6cloned6ClonedINtNtNtB1d_5slice4iter4IterNtNtB7_6nibble6NibbleEEE20insert_storage_valueB9_
Line
Count
Source
1889
30
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
30
        let 
future_parent19
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
14
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
14
                let key_len = self.trie.node_full_key(ancestor).count();
1898
14
                debug_assert!(self.key.clone().count() > key_len);
1899
14
                Some((ancestor, key_len))
1900
            }
1901
5
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
11
                return PrepareInsert::One(PrepareInsertOne {
1906
11
                    trie: self.trie,
1907
11
                    parent: None,
1908
11
                    partial_key: self.key.collect(),
1909
11
                    children: [None; 16],
1910
11
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
9
        let existing_node_index =
1917
19
            if let Some((
future_parent_index14
,
future_parent_key_len14
)) = future_parent {
1918
14
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
14
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
14
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
4
                    Some(i) => {
1922
4
                        debug_assert_eq!(
1923
4
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
4
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
10
                        return PrepareInsert::One(PrepareInsertOne {
1943
10
                            trie: self.trie,
1944
10
                            parent: Some((future_parent_index, new_child_index)),
1945
10
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
10
                            children: [None; 16],
1947
10
                        });
1948
                    }
1949
                }
1950
            } else {
1951
5
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
9
        let existing_node_partial_key = &self
1957
9
            .trie
1958
9
            .nodes
1959
9
            .get(existing_node_index)
1960
9
            .unwrap()
1961
9
            .partial_key;
1962
9
        let new_node_partial_key = self
1963
9
            .key
1964
9
            .clone()
1965
9
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
9
            .collect::<Vec<_>>();
1967
9
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
9
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
9
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
4
            let mut new_node_children = [None; 16];
2012
4
            let existing_node_new_child_index =
2013
4
                existing_node_partial_key[new_node_partial_key.len()];
2014
4
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
4
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
4
                trie: self.trie,
2019
4
                parent: if let Some((
future_parent_index1
,
future_parent_key_len1
)) = future_parent {
2020
1
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
1
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
3
                    None
2024
                },
2025
4
                partial_key: new_node_partial_key,
2026
4
                children: new_node_children,
2027
            });
2028
5
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
5
        let branch_partial_key_len = {
2073
5
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
5
            let mut len = 0;
2075
5
            let mut k1 = new_node_partial_key.iter();
2076
5
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
11
            while k1.next() == k2.next() {
2080
6
                len += 1;
2081
6
            }
2082
5
            debug_assert!(len < new_node_partial_key.len());
2083
5
            debug_assert!(len < existing_node_partial_key.len());
2084
5
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
5
        let branch_children = {
2090
5
            let mut branch_children = [None; 16];
2091
5
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
5
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
5
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
5
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
5
                Some(existing_node_index);
2098
5
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
5
            trie: self.trie,
2104
2105
5
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
5
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
5
            branch_parent: if let Some((
future_parent_index3
,
future_parent_key_len3
)) = future_parent
2109
            {
2110
3
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
3
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
2
                None
2114
            },
2115
5
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
5
            branch_children,
2117
        })
2118
30
    }
_RNvMs7_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_6VacantuINtNtNtNtCs1p5UDGgVI4d_4core4iter8adapters6copied6CopiedINtNtNtB1d_5slice4iter4IterNtNtB7_6nibble6NibbleEEE20insert_storage_valueB9_
Line
Count
Source
1889
241k
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
241k
        let 
future_parent237k
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
237k
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
237k
                let key_len = self.trie.node_full_key(ancestor).count();
1898
237k
                debug_assert!(self.key.clone().count() > key_len);
1899
237k
                Some((ancestor, key_len))
1900
            }
1901
585
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
4.09k
                return PrepareInsert::One(PrepareInsertOne {
1906
4.09k
                    trie: self.trie,
1907
4.09k
                    parent: None,
1908
4.09k
                    partial_key: self.key.collect(),
1909
4.09k
                    children: [None; 16],
1910
4.09k
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
13.2k
        let existing_node_index =
1917
237k
            if let Some((
future_parent_index237k
,
future_parent_key_len237k
)) = future_parent {
1918
237k
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
237k
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
237k
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
12.6k
                    Some(i) => {
1922
12.6k
                        debug_assert_eq!(
1923
12.6k
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
12.6k
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
224k
                        return PrepareInsert::One(PrepareInsertOne {
1943
224k
                            trie: self.trie,
1944
224k
                            parent: Some((future_parent_index, new_child_index)),
1945
224k
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
224k
                            children: [None; 16],
1947
224k
                        });
1948
                    }
1949
                }
1950
            } else {
1951
585
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
13.2k
        let existing_node_partial_key = &self
1957
13.2k
            .trie
1958
13.2k
            .nodes
1959
13.2k
            .get(existing_node_index)
1960
13.2k
            .unwrap()
1961
13.2k
            .partial_key;
1962
13.2k
        let new_node_partial_key = self
1963
13.2k
            .key
1964
13.2k
            .clone()
1965
13.2k
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
13.2k
            .collect::<Vec<_>>();
1967
13.2k
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
13.2k
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
13.2k
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
0
            let mut new_node_children = [None; 16];
2012
0
            let existing_node_new_child_index =
2013
0
                existing_node_partial_key[new_node_partial_key.len()];
2014
0
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
0
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
0
                trie: self.trie,
2019
0
                parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent {
2020
0
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
0
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
0
                    None
2024
                },
2025
0
                partial_key: new_node_partial_key,
2026
0
                children: new_node_children,
2027
            });
2028
13.2k
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
13.2k
        let branch_partial_key_len = {
2073
13.2k
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
13.2k
            let mut len = 0;
2075
13.2k
            let mut k1 = new_node_partial_key.iter();
2076
13.2k
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
13.4k
            while k1.next() == k2.next() {
2080
233
                len += 1;
2081
233
            }
2082
13.2k
            debug_assert!(len < new_node_partial_key.len());
2083
13.2k
            debug_assert!(len < existing_node_partial_key.len());
2084
13.2k
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
13.2k
        let branch_children = {
2090
13.2k
            let mut branch_children = [None; 16];
2091
13.2k
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
13.2k
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
13.2k
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
13.2k
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
13.2k
                Some(existing_node_index);
2098
13.2k
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
13.2k
            trie: self.trie,
2104
2105
13.2k
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
13.2k
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
13.2k
            branch_parent: if let Some((
future_parent_index12.6k
,
future_parent_key_len12.6k
)) = future_parent
2109
            {
2110
12.6k
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
12.6k
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
585
                None
2114
            },
2115
13.2k
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
13.2k
            branch_children,
2117
        })
2118
241k
    }
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB15_NtNtB7_9trie_node17MerkleValueOutputEEINtNtB7_6nibble14BytesToNibblesINtNtNtNtB19_4iter8adapters6copied6CopiedINtNtNtB19_5slice4iter4IterhEEEE20insert_storage_valueCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_6VacantINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEINtNtNtNtB18_4iter8adapters6copied6CopiedINtNtNtB18_5slice4iter4IterNtNtB7_6nibble6NibbleEEE20insert_storage_valueB9_
_RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB15_NtNtB7_9trie_node17MerkleValueOutputEEINtNtB7_6nibble14BytesToNibblesINtNtNtNtB19_4iter8adapters6copied6CopiedINtNtNtB19_5slice4iter4IterhEEEE20insert_storage_valueCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
1889
70
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
70
        let 
future_parent68
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
66
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
66
                let key_len = self.trie.node_full_key(ancestor).count();
1898
66
                debug_assert!(self.key.clone().count() > key_len);
1899
66
                Some((ancestor, key_len))
1900
            }
1901
2
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
2
                return PrepareInsert::One(PrepareInsertOne {
1906
2
                    trie: self.trie,
1907
2
                    parent: None,
1908
2
                    partial_key: self.key.collect(),
1909
2
                    children: [None; 16],
1910
2
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
26
        let existing_node_index =
1917
68
            if let Some((
future_parent_index66
,
future_parent_key_len66
)) = future_parent {
1918
66
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
66
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
66
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
24
                    Some(i) => {
1922
24
                        debug_assert_eq!(
1923
24
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
24
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
42
                        return PrepareInsert::One(PrepareInsertOne {
1943
42
                            trie: self.trie,
1944
42
                            parent: Some((future_parent_index, new_child_index)),
1945
42
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
42
                            children: [None; 16],
1947
42
                        });
1948
                    }
1949
                }
1950
            } else {
1951
2
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
26
        let existing_node_partial_key = &self
1957
26
            .trie
1958
26
            .nodes
1959
26
            .get(existing_node_index)
1960
26
            .unwrap()
1961
26
            .partial_key;
1962
26
        let new_node_partial_key = self
1963
26
            .key
1964
26
            .clone()
1965
26
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
26
            .collect::<Vec<_>>();
1967
26
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
26
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
26
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
0
            let mut new_node_children = [None; 16];
2012
0
            let existing_node_new_child_index =
2013
0
                existing_node_partial_key[new_node_partial_key.len()];
2014
0
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
0
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
0
                trie: self.trie,
2019
0
                parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent {
2020
0
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
0
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
0
                    None
2024
                },
2025
0
                partial_key: new_node_partial_key,
2026
0
                children: new_node_children,
2027
            });
2028
26
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
26
        let branch_partial_key_len = {
2073
26
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
26
            let mut len = 0;
2075
26
            let mut k1 = new_node_partial_key.iter();
2076
26
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
398
            while k1.next() == k2.next() {
2080
372
                len += 1;
2081
372
            }
2082
26
            debug_assert!(len < new_node_partial_key.len());
2083
26
            debug_assert!(len < existing_node_partial_key.len());
2084
26
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
26
        let branch_children = {
2090
26
            let mut branch_children = [None; 16];
2091
26
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
26
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
26
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
26
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
26
                Some(existing_node_index);
2098
26
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
26
            trie: self.trie,
2104
2105
26
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
26
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
26
            branch_parent: if let Some((
future_parent_index24
,
future_parent_key_len24
)) = future_parent
2109
            {
2110
24
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
24
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
2
                None
2114
            },
2115
26
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
26
            branch_children,
2117
        })
2118
70
    }
_RNvMs7_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_6VacantTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB15_NtNtB7_9trie_node17MerkleValueOutputEEINtNtB7_6nibble14BytesToNibblesINtNtNtNtB19_4iter8adapters6copied6CopiedINtNtNtB19_5slice4iter4IterhEEEE20insert_storage_valueCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
1889
665
    pub fn insert_storage_value(mut self) -> PrepareInsert<'a, TUd> {
1890
        // Retrieve what will be the parent after we insert the new node, not taking branching
1891
        // into account yet.
1892
        // If `Some`, contains its index and number of nibbles in its key.
1893
665
        let 
future_parent646
= match (self.closest_ancestor, self.trie.root_index) {
1894
0
            (Some(_), None) => unreachable!(),
1895
627
            (Some(ancestor), Some(_)) => {
1896
                // TODO: could be optimized by passing in the Vacant the key remainder
1897
627
                let key_len = self.trie.node_full_key(ancestor).count();
1898
627
                debug_assert!(self.key.clone().count() > key_len);
1899
627
                Some((ancestor, key_len))
1900
            }
1901
19
            (None, Some(_)) => None,
1902
            (None, None) => {
1903
                // Situation where the trie is empty. This is kind of a special case that we
1904
                // handle by returning early.
1905
19
                return PrepareInsert::One(PrepareInsertOne {
1906
19
                    trie: self.trie,
1907
19
                    parent: None,
1908
19
                    partial_key: self.key.collect(),
1909
19
                    children: [None; 16],
1910
19
                });
1911
            }
1912
        };
1913
1914
        // Get the existing child of `future_parent` that points towards the newly-inserted node,
1915
        // or a successful early-return if none.
1916
247
        let existing_node_index =
1917
646
            if let Some((
future_parent_index627
,
future_parent_key_len627
)) = future_parent {
1918
627
                let new_child_index = self.key.clone().nth(future_parent_key_len).unwrap();
1919
627
                let future_parent = self.trie.nodes.get(future_parent_index).unwrap();
1920
627
                match future_parent.children[usize::from(u8::from(new_child_index))] {
1921
228
                    Some(i) => {
1922
228
                        debug_assert_eq!(
1923
228
                            self.trie.nodes.get(i).unwrap().parent.unwrap().0,
1924
                            future_parent_index
1925
                        );
1926
228
                        i
1927
                    }
1928
                    None => {
1929
                        // There is an empty slot in `future_parent` for our new node.
1930
                        //
1931
                        //
1932
                        //           `future_parent`
1933
                        //                 +-+
1934
                        //             +-> +-+  <---------+
1935
                        //             |        <----+    |
1936
                        //             |     ^       |    |
1937
                        //            +-+    |       |    |
1938
                        //   New node +-+    +-+-+  +-+  +-+  0 or more existing children
1939
                        //                     +-+  +-+  +-+
1940
                        //
1941
                        //
1942
399
                        return PrepareInsert::One(PrepareInsertOne {
1943
399
                            trie: self.trie,
1944
399
                            parent: Some((future_parent_index, new_child_index)),
1945
399
                            partial_key: self.key.skip(future_parent_key_len + 1).collect(),
1946
399
                            children: [None; 16],
1947
399
                        });
1948
                    }
1949
                }
1950
            } else {
1951
19
                self.trie.root_index.unwrap()
1952
            };
1953
1954
        // `existing_node_idx` and the new node are known to either have the same parent and the
1955
        // same child index, or to both have no parent. Now let's compare their partial key.
1956
247
        let existing_node_partial_key = &self
1957
247
            .trie
1958
247
            .nodes
1959
247
            .get(existing_node_index)
1960
247
            .unwrap()
1961
247
            .partial_key;
1962
247
        let new_node_partial_key = self
1963
247
            .key
1964
247
            .clone()
1965
247
            .skip(future_parent.map_or(0, |(_, n)| n + 1))
1966
247
            .collect::<Vec<_>>();
1967
247
        debug_assert_ne!(*existing_node_partial_key, new_node_partial_key);
1968
247
        debug_assert!(!new_node_partial_key.starts_with(existing_node_partial_key));
1969
1970
        // If `existing_node_partial_key` starts with `new_node_partial_key`, then the new node
1971
        // will be inserted in-between the parent and the existing node.
1972
247
        if existing_node_partial_key.starts_with(&new_node_partial_key) {
1973
            // The new node is to be inserted in-between `future_parent` and
1974
            // `existing_node_index`.
1975
            //
1976
            // If `future_parent` is `Some`:
1977
            //
1978
            //
1979
            //                         +-+
1980
            //        `future_parent`  +-+ <---------+
1981
            //                          ^            |
1982
            //                          |            +
1983
            //                         +-+         (0 or more existing children)
1984
            //               New node  +-+
1985
            //                          ^
1986
            //                          |
1987
            //                         +-+
1988
            //  `existing_node_index`  +-+
1989
            //                          ^
1990
            //                          |
1991
            //                          +
1992
            //                    (0 or more existing children)
1993
            //
1994
            //
1995
            //
1996
            // If `future_parent` is `None`:
1997
            //
1998
            //
1999
            //            New node    +-+
2000
            //    (becomes the root)  +-+
2001
            //                         ^
2002
            //                         |
2003
            // `existing_node_index`  +-+
2004
            //     (current root)     +-+
2005
            //                         ^
2006
            //                         |
2007
            //                         +
2008
            //                   (0 or more existing children)
2009
            //
2010
2011
0
            let mut new_node_children = [None; 16];
2012
0
            let existing_node_new_child_index =
2013
0
                existing_node_partial_key[new_node_partial_key.len()];
2014
0
            new_node_children[usize::from(u8::from(existing_node_new_child_index))] =
2015
0
                Some(existing_node_index);
2016
2017
            return PrepareInsert::One(PrepareInsertOne {
2018
0
                trie: self.trie,
2019
0
                parent: if let Some((future_parent_index, future_parent_key_len)) = future_parent {
2020
0
                    let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2021
0
                    Some((future_parent_index, new_child_index))
2022
                } else {
2023
0
                    None
2024
                },
2025
0
                partial_key: new_node_partial_key,
2026
0
                children: new_node_children,
2027
            });
2028
247
        }
2029
2030
        // If we reach here, we know that we will need to create a new branch node in addition to
2031
        // the new storage node.
2032
        //
2033
        // If `future_parent` is `Some`:
2034
        //
2035
        //
2036
        //                  `future_parent`
2037
        //
2038
        //                        +-+
2039
        //                        +-+ <--------+  (0 or more existing children)
2040
        //                         ^
2041
        //                         |
2042
        //       New branch node  +-+
2043
        //                        +-+ <-------+
2044
        //                         ^          |
2045
        //                         |          |
2046
        //                        +-+        +-+
2047
        // `existing_node_index`  +-+        +-+  New storage node
2048
        //                         ^
2049
        //                         |
2050
        //
2051
        //                 (0 or more existing children)
2052
        //
2053
        //
2054
        //
2055
        // If `future_parent` is `None`:
2056
        //
2057
        //
2058
        //     New branch node    +-+
2059
        //     (becomes root)     +-+ <-------+
2060
        //                         ^          |
2061
        //                         |          |
2062
        // `existing_node_index`  +-+        +-+
2063
        //     (current root)     +-+        +-+  New storage node
2064
        //                         ^
2065
        //                         |
2066
        //
2067
        //                 (0 or more existing children)
2068
        //
2069
        //
2070
2071
        // Find the common ancestor between `new_node_partial_key` and `existing_node_partial_key`.
2072
247
        let branch_partial_key_len = {
2073
247
            debug_assert_ne!(new_node_partial_key, &**existing_node_partial_key);
2074
247
            let mut len = 0;
2075
247
            let mut k1 = new_node_partial_key.iter();
2076
247
            let mut k2 = existing_node_partial_key.iter();
2077
            // Since `new_node_partial_key` is different from `existing_node_partial_key`, we know
2078
            // that `k1.next()` and `k2.next()` won't both be `None`.
2079
3.78k
            while k1.next() == k2.next() {
2080
3.53k
                len += 1;
2081
3.53k
            }
2082
247
            debug_assert!(len < new_node_partial_key.len());
2083
247
            debug_assert!(len < existing_node_partial_key.len());
2084
247
            len
2085
        };
2086
2087
        // Table of children for the new branch node, not including the new storage node.
2088
        // It therefore contains only one entry: `existing_node_index`.
2089
247
        let branch_children = {
2090
247
            let mut branch_children = [None; 16];
2091
247
            let existing_node_new_child_index = existing_node_partial_key[branch_partial_key_len];
2092
247
            debug_assert_ne!(
2093
                existing_node_new_child_index,
2094
247
                new_node_partial_key[branch_partial_key_len]
2095
            );
2096
247
            branch_children[usize::from(u8::from(existing_node_new_child_index))] =
2097
247
                Some(existing_node_index);
2098
247
            branch_children
2099
        };
2100
2101
        // Success!
2102
        PrepareInsert::Two(PrepareInsertTwo {
2103
247
            trie: self.trie,
2104
2105
247
            storage_child_index: new_node_partial_key[branch_partial_key_len],
2106
247
            storage_partial_key: new_node_partial_key[branch_partial_key_len + 1..].to_owned(),
2107
2108
247
            branch_parent: if let Some((
future_parent_index228
,
future_parent_key_len228
)) = future_parent
2109
            {
2110
228
                let new_child_index = self.key.nth(future_parent_key_len).unwrap();
2111
228
                Some((future_parent_index, new_child_index))
2112
            } else {
2113
19
                None
2114
            },
2115
247
            branch_partial_key: new_node_partial_key[..branch_partial_key_len].to_owned(),
2116
247
            branch_children,
2117
        })
2118
665
    }
2119
}
2120
2121
/// Preparation for a new node insertion.
2122
///
2123
/// The trie hasn't been modified yet and you can safely drop this object.
2124
#[must_use]
2125
pub enum PrepareInsert<'a, TUd> {
2126
    /// One node will be inserted in the trie.
2127
    One(PrepareInsertOne<'a, TUd>),
2128
    /// Two nodes will be inserted in the trie.
2129
    Two(PrepareInsertTwo<'a, TUd>),
2130
}
2131
2132
impl<'a, TUd> PrepareInsert<'a, TUd> {
2133
    /// Insert the new node. `branch_node_user_data` is discarded if `self` is
2134
    /// a [`PrepareInsert::One`].
2135
1.32M
    pub fn insert(
2136
1.32M
        self,
2137
1.32M
        storage_node_user_data: TUd,
2138
1.32M
        branch_node_user_data: TUd,
2139
1.32M
    ) -> StorageNodeAccess<'a, TUd> {
2140
1.32M
        match self {
2141
1.08M
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
242k
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
1.32M
    }
_RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13PrepareInsertTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2135
2.57k
    pub fn insert(
2136
2.57k
        self,
2137
2.57k
        storage_node_user_data: TUd,
2138
2.57k
        branch_node_user_data: TUd,
2139
2.57k
    ) -> StorageNodeAccess<'a, TUd> {
2140
2.57k
        match self {
2141
2.49k
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
80
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
2.57k
    }
_RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13PrepareInsertTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2135
336k
    pub fn insert(
2136
336k
        self,
2137
336k
        storage_node_user_data: TUd,
2138
336k
        branch_node_user_data: TUd,
2139
336k
    ) -> StorageNodeAccess<'a, TUd> {
2140
336k
        match self {
2141
272k
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
64.4k
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
336k
    }
_RNvMs8_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_13PrepareInsertuE6insertB9_
Line
Count
Source
2135
989k
    pub fn insert(
2136
989k
        self,
2137
989k
        storage_node_user_data: TUd,
2138
989k
        branch_node_user_data: TUd,
2139
989k
    ) -> StorageNodeAccess<'a, TUd> {
2140
989k
        match self {
2141
812k
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
177k
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
989k
    }
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13PrepareInsertTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13PrepareInsertpE6insertB9_
_RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13PrepareInsertTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
2135
70
    pub fn insert(
2136
70
        self,
2137
70
        storage_node_user_data: TUd,
2138
70
        branch_node_user_data: TUd,
2139
70
    ) -> StorageNodeAccess<'a, TUd> {
2140
70
        match self {
2141
44
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
26
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
70
    }
_RNvMs8_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_13PrepareInsertTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1d_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
2135
665
    pub fn insert(
2136
665
        self,
2137
665
        storage_node_user_data: TUd,
2138
665
        branch_node_user_data: TUd,
2139
665
    ) -> StorageNodeAccess<'a, TUd> {
2140
665
        match self {
2141
418
            PrepareInsert::One(n) => n.insert(storage_node_user_data),
2142
247
            PrepareInsert::Two(n) => n.insert(storage_node_user_data, branch_node_user_data),
2143
        }
2144
665
    }
2145
}
2146
2147
/// One node will be inserted in the trie.
2148
pub struct PrepareInsertOne<'a, TUd> {
2149
    trie: &'a mut TrieStructure<TUd>,
2150
2151
    /// Value of [`Node::parent`] for the newly-created node.
2152
    /// If `None`, we also set the root of the trie to the new node.
2153
    parent: Option<(usize, Nibble)>,
2154
    /// Value of [`Node::partial_key`] for the newly-created node.
2155
    partial_key: Vec<Nibble>,
2156
    /// Value of [`Node::children`] for the newly-created node.
2157
    children: [Option<usize>; 16],
2158
}
2159
2160
impl<'a, TUd> PrepareInsertOne<'a, TUd> {
2161
    /// Insert the new node.
2162
1.11M
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
1.11M
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
1.11M
        let new_node_index = self.trie.nodes.insert(Node {
2166
1.11M
            parent: self.parent,
2167
1.11M
            partial_key: self.partial_key,
2168
1.11M
            children: self.children,
2169
1.11M
            has_storage_value: true,
2170
1.11M
            user_data,
2171
1.11M
        });
2172
2173
        // Update the children node to point to their new parent.
2174
17.8M
        for (child_index, child) in 
self.children1.11M
.
iter1.11M
().
enumerate1.11M
() {
2175
17.8M
            let 
child100k
= match child {
2176
100k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
17.7M
                None => continue,
2178
            };
2179
2180
100k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
100k
            child.parent = Some((new_node_index, child_index));
2182
100k
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
1.11M
        if let Some((
parent_index1.05M
,
child_index1.05M
)) = self.parent {
2187
1.05M
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
1.05M
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
1.05M
        } else {
2190
61.8k
            self.trie.root_index = Some(new_node_index);
2191
61.8k
        }
2192
2193
        // Success!
2194
1.11M
        StorageNodeAccess {
2195
1.11M
            trie: self.trie,
2196
1.11M
            node_index: new_node_index,
2197
1.11M
        }
2198
1.11M
    }
_RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE6insertB9_
Line
Count
Source
2162
29.2k
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
29.2k
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
29.2k
        let new_node_index = self.trie.nodes.insert(Node {
2166
29.2k
            parent: self.parent,
2167
29.2k
            partial_key: self.partial_key,
2168
29.2k
            children: self.children,
2169
29.2k
            has_storage_value: true,
2170
29.2k
            user_data,
2171
29.2k
        });
2172
2173
        // Update the children node to point to their new parent.
2174
467k
        for (child_index, child) in 
self.children29.2k
.
iter29.2k
().
enumerate29.2k
() {
2175
467k
            let 
child7.30k
= match child {
2176
7.30k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
460k
                None => continue,
2178
            };
2179
2180
7.30k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
7.30k
            child.parent = Some((new_node_index, child_index));
2182
7.30k
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
29.2k
        if let Some((
parent_index25.6k
,
child_index25.6k
)) = self.parent {
2187
25.6k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
25.6k
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
25.6k
        } else {
2190
3.54k
            self.trie.root_index = Some(new_node_index);
2191
3.54k
        }
2192
2193
        // Success!
2194
29.2k
        StorageNodeAccess {
2195
29.2k
            trie: self.trie,
2196
29.2k
            node_index: new_node_index,
2197
29.2k
        }
2198
29.2k
    }
_RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2162
2.49k
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
2.49k
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
2.49k
        let new_node_index = self.trie.nodes.insert(Node {
2166
2.49k
            parent: self.parent,
2167
2.49k
            partial_key: self.partial_key,
2168
2.49k
            children: self.children,
2169
2.49k
            has_storage_value: true,
2170
2.49k
            user_data,
2171
2.49k
        });
2172
2173
        // Update the children node to point to their new parent.
2174
39.8k
        for (child_index, child) in 
self.children2.49k
.
iter2.49k
().
enumerate2.49k
() {
2175
39.8k
            let 
child1
= match child {
2176
1
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
39.8k
                None => continue,
2178
            };
2179
2180
1
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
1
            child.parent = Some((new_node_index, child_index));
2182
1
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
2.49k
        if let Some((
parent_index1.46k
,
child_index1.46k
)) = self.parent {
2187
1.46k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
1.46k
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
1.46k
        } else {
2190
1.02k
            self.trie.root_index = Some(new_node_index);
2191
1.02k
        }
2192
2193
        // Success!
2194
2.49k
        StorageNodeAccess {
2195
2.49k
            trie: self.trie,
2196
2.49k
            node_index: new_node_index,
2197
2.49k
        }
2198
2.49k
    }
_RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2162
272k
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
272k
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
272k
        let new_node_index = self.trie.nodes.insert(Node {
2166
272k
            parent: self.parent,
2167
272k
            partial_key: self.partial_key,
2168
272k
            children: self.children,
2169
272k
            has_storage_value: true,
2170
272k
            user_data,
2171
272k
        });
2172
2173
        // Update the children node to point to their new parent.
2174
4.35M
        for (child_index, child) in 
self.children272k
.
iter272k
().
enumerate272k
() {
2175
4.35M
            let 
child6.85k
= match child {
2176
6.85k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
4.35M
                None => continue,
2178
            };
2179
2180
6.85k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
6.85k
            child.parent = Some((new_node_index, child_index));
2182
6.85k
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
272k
        if let Some((
parent_index227k
,
child_index227k
)) = self.parent {
2187
227k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
227k
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
227k
        } else {
2190
44.5k
            self.trie.root_index = Some(new_node_index);
2191
44.5k
        }
2192
2193
        // Success!
2194
272k
        StorageNodeAccess {
2195
272k
            trie: self.trie,
2196
272k
            node_index: new_node_index,
2197
272k
        }
2198
272k
    }
_RNvMs9_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneuE6insertB9_
Line
Count
Source
2162
812k
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
812k
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
812k
        let new_node_index = self.trie.nodes.insert(Node {
2166
812k
            parent: self.parent,
2167
812k
            partial_key: self.partial_key,
2168
812k
            children: self.children,
2169
812k
            has_storage_value: true,
2170
812k
            user_data,
2171
812k
        });
2172
2173
        // Update the children node to point to their new parent.
2174
12.9M
        for (child_index, child) in 
self.children812k
.
iter812k
().
enumerate812k
() {
2175
12.9M
            let 
child86.6k
= match child {
2176
86.6k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
12.9M
                None => continue,
2178
            };
2179
2180
86.6k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
86.6k
            child.parent = Some((new_node_index, child_index));
2182
86.6k
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
812k
        if let Some((
parent_index799k
,
child_index799k
)) = self.parent {
2187
799k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
799k
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
799k
        } else {
2190
12.7k
            self.trie.root_index = Some(new_node_index);
2191
12.7k
        }
2192
2193
        // Success!
2194
812k
        StorageNodeAccess {
2195
812k
            trie: self.trie,
2196
812k
            node_index: new_node_index,
2197
812k
        }
2198
812k
    }
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE6insertB9_
_RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
2162
44
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
44
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
44
        let new_node_index = self.trie.nodes.insert(Node {
2166
44
            parent: self.parent,
2167
44
            partial_key: self.partial_key,
2168
44
            children: self.children,
2169
44
            has_storage_value: true,
2170
44
            user_data,
2171
44
        });
2172
2173
        // Update the children node to point to their new parent.
2174
704
        for (child_index, child) in 
self.children44
.
iter44
().
enumerate44
() {
2175
704
            let 
child0
= match child {
2176
0
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
704
                None => continue,
2178
            };
2179
2180
0
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
0
            child.parent = Some((new_node_index, child_index));
2182
0
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
44
        if let Some((
parent_index42
,
child_index42
)) = self.parent {
2187
42
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
42
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
42
        } else {
2190
2
            self.trie.root_index = Some(new_node_index);
2191
2
        }
2192
2193
        // Success!
2194
44
        StorageNodeAccess {
2195
44
            trie: self.trie,
2196
44
            node_index: new_node_index,
2197
44
        }
2198
44
    }
_RNvMs9_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertOneTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
2162
418
    pub fn insert(self, user_data: TUd) -> StorageNodeAccess<'a, TUd> {
2163
418
        let new_node_partial_key_len = self.partial_key.len();
2164
2165
418
        let new_node_index = self.trie.nodes.insert(Node {
2166
418
            parent: self.parent,
2167
418
            partial_key: self.partial_key,
2168
418
            children: self.children,
2169
418
            has_storage_value: true,
2170
418
            user_data,
2171
418
        });
2172
2173
        // Update the children node to point to their new parent.
2174
6.68k
        for (child_index, child) in 
self.children418
.
iter418
().
enumerate418
() {
2175
6.68k
            let 
child0
= match child {
2176
0
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2177
6.68k
                None => continue,
2178
            };
2179
2180
0
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2181
0
            child.parent = Some((new_node_index, child_index));
2182
0
            truncate_first_elems(&mut child.partial_key, new_node_partial_key_len + 1);
2183
        }
2184
2185
        // Update the parent to point to its new child.
2186
418
        if let Some((
parent_index399
,
child_index399
)) = self.parent {
2187
399
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2188
399
            parent.children[usize::from(u8::from(child_index))] = Some(new_node_index);
2189
399
        } else {
2190
19
            self.trie.root_index = Some(new_node_index);
2191
19
        }
2192
2193
        // Success!
2194
418
        StorageNodeAccess {
2195
418
            trie: self.trie,
2196
418
            node_index: new_node_index,
2197
418
        }
2198
418
    }
2199
}
2200
2201
/// Two nodes will be inserted in the trie.
2202
pub struct PrepareInsertTwo<'a, TUd> {
2203
    trie: &'a mut TrieStructure<TUd>,
2204
2205
    /// Value of the child index in [`Node::parent`] for the newly-created storage node.
2206
    storage_child_index: Nibble,
2207
    /// Value of [`Node::partial_key`] for the newly-created storage node.
2208
    storage_partial_key: Vec<Nibble>,
2209
2210
    /// Value of [`Node::parent`] for the newly-created branch node.
2211
    /// If `None`, we also set the root of the trie to the new branch node.
2212
    branch_parent: Option<(usize, Nibble)>,
2213
    /// Value of [`Node::partial_key`] for the newly-created branch node.
2214
    branch_partial_key: Vec<Nibble>,
2215
    /// Value of [`Node::children`] for the newly-created branch node. Does not include the entry
2216
    /// that must be filled with the new storage node.
2217
    branch_children: [Option<usize>; 16],
2218
}
2219
2220
impl<'a, TUd> PrepareInsertTwo<'a, TUd> {
2221
    /// Key of the branch node that will be inserted.
2222
0
    pub fn branch_node_key(&self) -> impl Iterator<Item = Nibble> {
2223
0
        if let Some((parent_index, child_index)) = self.branch_parent {
2224
0
            let parent = self.trie.node_full_key(parent_index);
2225
0
            let iter = parent
2226
0
                .chain(iter::once(child_index))
2227
0
                .chain(self.branch_partial_key.iter().cloned());
2228
0
            Either::Left(iter)
2229
        } else {
2230
0
            Either::Right(self.branch_partial_key.iter().cloned())
2231
        }
2232
0
    }
Unexecuted instantiation: _RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15branch_node_keyB9_
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE15branch_node_keyB9_
2233
2234
    /// Insert the new node.
2235
242k
    pub fn insert(
2236
242k
        self,
2237
242k
        storage_node_user_data: TUd,
2238
242k
        branch_node_user_data: TUd,
2239
242k
    ) -> StorageNodeAccess<'a, TUd> {
2240
242k
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
242k
        debug_assert_eq!(
2243
3.88M
            
self.branch_children242k
.
iter242k
().
filter242k
(|c| c.is_some()).
count242k
(),
Unexecuted instantiation: _RNCNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB9_12proof_encode4NodeEE6insert0Bb_
_RNCNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1i_NtNtB9_9trie_node17MerkleValueOutputEEE6insert0Bb_
Line
Count
Source
2243
1.28k
            self.branch_children.iter().filter(|c| c.is_some()).count(),
_RNCNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB9_16TrieEntryVersionEEIB1i_NtNtB9_9trie_node17MerkleValueOutputEEE6insert0Bb_
Line
Count
Source
2243
1.03M
            self.branch_children.iter().filter(|c| c.is_some()).count(),
_RNCNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwouE6insert0Bb_
Line
Count
Source
2243
2.84M
            self.branch_children.iter().filter(|c| c.is_some()).count(),
Unexecuted instantiation: _RNCNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1i_NtNtB9_9trie_node17MerkleValueOutputEEE6insert0CscoAnRPySggw_6author
Unexecuted instantiation: _RNCNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB9_12proof_encode4NodeEE6insert0Bb_
_RNCNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1i_NtNtB9_9trie_node17MerkleValueOutputEEE6insert0CsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
2243
416
            self.branch_children.iter().filter(|c| c.is_some()).count(),
_RNCNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB7_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1i_NtNtB9_9trie_node17MerkleValueOutputEEE6insert0Cs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
2243
3.95k
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
242k
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
242k
            parent: self.branch_parent,
2249
242k
            partial_key: self.branch_partial_key,
2250
242k
            children: self.branch_children,
2251
242k
            has_storage_value: false,
2252
242k
            user_data: branch_node_user_data,
2253
242k
        });
2254
2255
242k
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
242k
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
242k
            partial_key: self.storage_partial_key,
2258
242k
            children: [None; 16],
2259
242k
            has_storage_value: true,
2260
242k
            user_data: storage_node_user_data,
2261
242k
        });
2262
2263
242k
        self.trie
2264
242k
            .nodes
2265
242k
            .get_mut(new_branch_node_index)
2266
242k
            .unwrap()
2267
242k
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
242k
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
3.88M
        for (child_index, child) in 
self.branch_children242k
.
iter242k
().
enumerate242k
() {
2272
3.88M
            let 
child242k
= match child {
2273
242k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
3.63M
                None => continue,
2275
            };
2276
2277
242k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
242k
            child.parent = Some((new_branch_node_index, child_index));
2279
242k
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
242k
        if let Some((
parent_index226k
,
child_index226k
)) = self.branch_parent {
2284
226k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
226k
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
226k
        } else {
2287
16.2k
            self.trie.root_index = Some(new_branch_node_index);
2288
16.2k
        }
2289
2290
        // Success!
2291
242k
        StorageNodeAccess {
2292
242k
            trie: self.trie,
2293
242k
            node_index: new_storage_node_index,
2294
242k
        }
2295
242k
    }
Unexecuted instantiation: _RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE6insertB9_
_RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionINtNtCsaFPxhswmqCN_5alloc3vec3VechEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2235
80
    pub fn insert(
2236
80
        self,
2237
80
        storage_node_user_data: TUd,
2238
80
        branch_node_user_data: TUd,
2239
80
    ) -> StorageNodeAccess<'a, TUd> {
2240
80
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
80
        debug_assert_eq!(
2243
80
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
80
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
80
            parent: self.branch_parent,
2249
80
            partial_key: self.branch_partial_key,
2250
80
            children: self.branch_children,
2251
80
            has_storage_value: false,
2252
80
            user_data: branch_node_user_data,
2253
80
        });
2254
2255
80
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
80
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
80
            partial_key: self.storage_partial_key,
2258
80
            children: [None; 16],
2259
80
            has_storage_value: true,
2260
80
            user_data: storage_node_user_data,
2261
80
        });
2262
2263
80
        self.trie
2264
80
            .nodes
2265
80
            .get_mut(new_branch_node_index)
2266
80
            .unwrap()
2267
80
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
80
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
1.28k
        for (child_index, child) in 
self.branch_children80
.
iter80
().
enumerate80
() {
2272
1.28k
            let 
child80
= match child {
2273
80
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
1.20k
                None => continue,
2275
            };
2276
2277
80
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
80
            child.parent = Some((new_branch_node_index, child_index));
2279
80
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
80
        if let Some((parent_index, child_index)) = self.branch_parent {
2284
80
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
80
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
80
        } else {
2287
0
            self.trie.root_index = Some(new_branch_node_index);
2288
0
        }
2289
2290
        // Success!
2291
80
        StorageNodeAccess {
2292
80
            trie: self.trie,
2293
80
            node_index: new_storage_node_index,
2294
80
        }
2295
80
    }
_RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionTINtNtCsaFPxhswmqCN_5alloc3vec3VechENtB7_16TrieEntryVersionEEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertB9_
Line
Count
Source
2235
64.4k
    pub fn insert(
2236
64.4k
        self,
2237
64.4k
        storage_node_user_data: TUd,
2238
64.4k
        branch_node_user_data: TUd,
2239
64.4k
    ) -> StorageNodeAccess<'a, TUd> {
2240
64.4k
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
64.4k
        debug_assert_eq!(
2243
64.4k
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
64.4k
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
64.4k
            parent: self.branch_parent,
2249
64.4k
            partial_key: self.branch_partial_key,
2250
64.4k
            children: self.branch_children,
2251
64.4k
            has_storage_value: false,
2252
64.4k
            user_data: branch_node_user_data,
2253
64.4k
        });
2254
2255
64.4k
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
64.4k
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
64.4k
            partial_key: self.storage_partial_key,
2258
64.4k
            children: [None; 16],
2259
64.4k
            has_storage_value: true,
2260
64.4k
            user_data: storage_node_user_data,
2261
64.4k
        });
2262
2263
64.4k
        self.trie
2264
64.4k
            .nodes
2265
64.4k
            .get_mut(new_branch_node_index)
2266
64.4k
            .unwrap()
2267
64.4k
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
64.4k
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
1.03M
        for (child_index, child) in 
self.branch_children64.4k
.
iter64.4k
().
enumerate64.4k
() {
2272
1.03M
            let 
child64.4k
= match child {
2273
64.4k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
966k
                None => continue,
2275
            };
2276
2277
64.4k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
64.4k
            child.parent = Some((new_branch_node_index, child_index));
2279
64.4k
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
64.4k
        if let Some((
parent_index57.0k
,
child_index57.0k
)) = self.branch_parent {
2284
57.0k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
57.0k
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
57.0k
        } else {
2287
7.38k
            self.trie.root_index = Some(new_branch_node_index);
2288
7.38k
        }
2289
2290
        // Success!
2291
64.4k
        StorageNodeAccess {
2292
64.4k
            trie: self.trie,
2293
64.4k
            node_index: new_storage_node_index,
2294
64.4k
        }
2295
64.4k
    }
_RNvMsa_NtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwouE6insertB9_
Line
Count
Source
2235
177k
    pub fn insert(
2236
177k
        self,
2237
177k
        storage_node_user_data: TUd,
2238
177k
        branch_node_user_data: TUd,
2239
177k
    ) -> StorageNodeAccess<'a, TUd> {
2240
177k
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
177k
        debug_assert_eq!(
2243
177k
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
177k
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
177k
            parent: self.branch_parent,
2249
177k
            partial_key: self.branch_partial_key,
2250
177k
            children: self.branch_children,
2251
177k
            has_storage_value: false,
2252
177k
            user_data: branch_node_user_data,
2253
177k
        });
2254
2255
177k
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
177k
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
177k
            partial_key: self.storage_partial_key,
2258
177k
            children: [None; 16],
2259
177k
            has_storage_value: true,
2260
177k
            user_data: storage_node_user_data,
2261
177k
        });
2262
2263
177k
        self.trie
2264
177k
            .nodes
2265
177k
            .get_mut(new_branch_node_index)
2266
177k
            .unwrap()
2267
177k
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
177k
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
2.84M
        for (child_index, child) in 
self.branch_children177k
.
iter177k
().
enumerate177k
() {
2272
2.84M
            let 
child177k
= match child {
2273
177k
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
2.66M
                None => continue,
2275
            };
2276
2277
177k
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
177k
            child.parent = Some((new_branch_node_index, child_index));
2279
177k
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
177k
        if let Some((
parent_index168k
,
child_index168k
)) = self.branch_parent {
2284
168k
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
168k
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
168k
        } else {
2287
8.85k
            self.trie.root_index = Some(new_branch_node_index);
2288
8.85k
        }
2289
2290
        // Success!
2291
177k
        StorageNodeAccess {
2292
177k
            trie: self.trie,
2293
177k
            node_index: new_storage_node_index,
2294
177k
        }
2295
177k
    }
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCscoAnRPySggw_6author
Unexecuted instantiation: _RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoINtNtCs1p5UDGgVI4d_4core6option6OptionNtNtB7_12proof_encode4NodeEE6insertB9_
_RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCsjyNE3yDMkgA_14json_rpc_basic
Line
Count
Source
2235
26
    pub fn insert(
2236
26
        self,
2237
26
        storage_node_user_data: TUd,
2238
26
        branch_node_user_data: TUd,
2239
26
    ) -> StorageNodeAccess<'a, TUd> {
2240
26
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
26
        debug_assert_eq!(
2243
26
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
26
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
26
            parent: self.branch_parent,
2249
26
            partial_key: self.branch_partial_key,
2250
26
            children: self.branch_children,
2251
26
            has_storage_value: false,
2252
26
            user_data: branch_node_user_data,
2253
26
        });
2254
2255
26
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
26
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
26
            partial_key: self.storage_partial_key,
2258
26
            children: [None; 16],
2259
26
            has_storage_value: true,
2260
26
            user_data: storage_node_user_data,
2261
26
        });
2262
2263
26
        self.trie
2264
26
            .nodes
2265
26
            .get_mut(new_branch_node_index)
2266
26
            .unwrap()
2267
26
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
26
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
416
        for (child_index, child) in 
self.branch_children26
.
iter26
().
enumerate26
() {
2272
416
            let 
child26
= match child {
2273
26
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
390
                None => continue,
2275
            };
2276
2277
26
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
26
            child.parent = Some((new_branch_node_index, child_index));
2279
26
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
26
        if let Some((
parent_index24
,
child_index24
)) = self.branch_parent {
2284
24
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
24
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
24
        } else {
2287
2
            self.trie.root_index = Some(new_branch_node_index);
2288
2
        }
2289
2290
        // Success!
2291
26
        StorageNodeAccess {
2292
26
            trie: self.trie,
2293
26
            node_index: new_storage_node_index,
2294
26
        }
2295
26
    }
_RNvMsa_NtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structureINtB5_16PrepareInsertTwoTINtNtCs1p5UDGgVI4d_4core6option6OptionRShEIB1g_NtNtB7_9trie_node17MerkleValueOutputEEE6insertCs4VrkfB1pvQ3_25json_rpc_general_requests
Line
Count
Source
2235
247
    pub fn insert(
2236
247
        self,
2237
247
        storage_node_user_data: TUd,
2238
247
        branch_node_user_data: TUd,
2239
247
    ) -> StorageNodeAccess<'a, TUd> {
2240
247
        let new_branch_node_partial_key_len = self.branch_partial_key.len();
2241
2242
247
        debug_assert_eq!(
2243
247
            self.branch_children.iter().filter(|c| c.is_some()).count(),
2244
            1
2245
        );
2246
2247
247
        let new_branch_node_index = self.trie.nodes.insert(Node {
2248
247
            parent: self.branch_parent,
2249
247
            partial_key: self.branch_partial_key,
2250
247
            children: self.branch_children,
2251
247
            has_storage_value: false,
2252
247
            user_data: branch_node_user_data,
2253
247
        });
2254
2255
247
        let new_storage_node_index = self.trie.nodes.insert(Node {
2256
247
            parent: Some((new_branch_node_index, self.storage_child_index)),
2257
247
            partial_key: self.storage_partial_key,
2258
247
            children: [None; 16],
2259
247
            has_storage_value: true,
2260
247
            user_data: storage_node_user_data,
2261
247
        });
2262
2263
247
        self.trie
2264
247
            .nodes
2265
247
            .get_mut(new_branch_node_index)
2266
247
            .unwrap()
2267
247
            .children[usize::from(u8::from(self.storage_child_index))] =
2268
247
            Some(new_storage_node_index);
2269
2270
        // Update the branch node's children to point to their new parent.
2271
3.95k
        for (child_index, child) in 
self.branch_children247
.
iter247
().
enumerate247
() {
2272
3.95k
            let 
child247
= match child {
2273
247
                Some(c) => self.trie.nodes.get_mut(*c).unwrap(),
2274
3.70k
                None => continue,
2275
            };
2276
2277
247
            let child_index = Nibble::try_from(u8::try_from(child_index).unwrap()).unwrap();
2278
247
            child.parent = Some((new_branch_node_index, child_index));
2279
247
            truncate_first_elems(&mut child.partial_key, new_branch_node_partial_key_len + 1);
2280
        }
2281
2282
        // Update the branch node's parent to point to its new child.
2283
247
        if let Some((
parent_index228
,
child_index228
)) = self.branch_parent {
2284
228
            let parent = self.trie.nodes.get_mut(parent_index).unwrap();
2285
228
            parent.children[usize::from(u8::from(child_index))] = Some(new_branch_node_index);
2286
228
        } else {
2287
19
            self.trie.root_index = Some(new_branch_node_index);
2288
19
        }
2289
2290
        // Success!
2291
247
        StorageNodeAccess {
2292
247
            trie: self.trie,
2293
247
            node_index: new_storage_node_index,
2294
247
        }
2295
247
    }
2296
}
2297
2298
/// Inserts `first` and `second` at the beginning of `vec`.
2299
24.7k
fn insert_front(vec: &mut Vec<Nibble>, first: Vec<Nibble>, next: Nibble) {
2300
24.7k
    let shift = first.len() + 1;
2301
24.7k
    let previous_len = vec.len();
2302
24.7k
    vec.resize(vec.len() + shift, Nibble::try_from(0).unwrap());
2303
50.7k
    for n in 
(0..previous_len)24.7k
.
rev24.7k
() {
2304
50.7k
        vec[n + shift] = vec[n];
2305
50.7k
    }
2306
24.7k
    vec[0..first.len()].copy_from_slice(&first);
2307
24.7k
    vec[first.len()] = next;
2308
24.7k
}
_RNvNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structure12insert_front
Line
Count
Source
2299
24.7k
fn insert_front(vec: &mut Vec<Nibble>, first: Vec<Nibble>, next: Nibble) {
2300
24.7k
    let shift = first.len() + 1;
2301
24.7k
    let previous_len = vec.len();
2302
24.7k
    vec.resize(vec.len() + shift, Nibble::try_from(0).unwrap());
2303
50.7k
    for n in 
(0..previous_len)24.7k
.
rev24.7k
() {
2304
50.7k
        vec[n + shift] = vec[n];
2305
50.7k
    }
2306
24.7k
    vec[0..first.len()].copy_from_slice(&first);
2307
24.7k
    vec[first.len()] = next;
2308
24.7k
}
Unexecuted instantiation: _RNvNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structure12insert_front
2309
2310
/// Removes the first `num` elements of `vec`.
2311
343k
fn truncate_first_elems(vec: &mut Vec<Nibble>, num: usize) {
2312
343k
    debug_assert!(num <= vec.len());
2313
570k
    for n in 
num343k
..
vec343k
.
len343k
() {
2314
570k
        vec[n - num] = vec[n];
2315
570k
    }
2316
343k
    vec.truncate(vec.len() - num);
2317
343k
}
_RNvNtNtCsjlkOsLH0Zfj_7smoldot4trie14trie_structure20truncate_first_elems
Line
Count
Source
2311
343k
fn truncate_first_elems(vec: &mut Vec<Nibble>, num: usize) {
2312
343k
    debug_assert!(num <= vec.len());
2313
558k
    for n in 
num343k
..
vec343k
.
len343k
() {
2314
558k
        vec[n - num] = vec[n];
2315
558k
    }
2316
343k
    vec.truncate(vec.len() - num);
2317
343k
}
_RNvNtNtCsc1ywvx6YAnK_7smoldot4trie14trie_structure20truncate_first_elems
Line
Count
Source
2311
273
fn truncate_first_elems(vec: &mut Vec<Nibble>, num: usize) {
2312
273
    debug_assert!(num <= vec.len());
2313
12.4k
    for n in 
num273
..
vec273
.
len273
() {
2314
12.4k
        vec[n - num] = vec[n];
2315
12.4k
    }
2316
273
    vec.truncate(vec.len() - num);
2317
273
}