Skip to content

Commit 4aa7bfd

Browse files
committed
feat: make TrieNode sized
1 parent 7d6fdba commit 4aa7bfd

File tree

4 files changed

+128
-134
lines changed

4 files changed

+128
-134
lines changed

storage/src/tries/iter.rs

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,47 +22,47 @@ pub struct IterDescending;
2222
/// respectively.
2323
#[derive(Debug)]
2424
#[must_use = "iterators are lazy and do nothing unless consumed"]
25-
pub struct TrieEdgeIter<'a, N: ?Sized, V: ?Sized, D> {
25+
pub struct TrieEdgeIter<'root, N, V: ?Sized, D> {
2626
leading_path: PathBuf,
27-
stack: Vec<Frame<'a, N, V>>,
27+
stack: Vec<Frame<'root, N, V>>,
2828
marker: std::marker::PhantomData<D>,
2929
}
3030

3131
/// An iterator over the key-value pairs in a key-value trie.
3232
#[derive(Debug)]
3333
#[must_use = "iterators are lazy and do nothing unless consumed"]
34-
pub struct TrieValueIter<'a, N: ?Sized, V: ?Sized, D> {
35-
edges: TrieEdgeIter<'a, N, V, D>,
34+
pub struct TrieValueIter<'root, N, V: ?Sized, D> {
35+
edges: TrieEdgeIter<'root, N, V, D>,
3636
}
3737

3838
/// An iterator over the edges along a specified path in a key-value trie
3939
/// terminating at the node corresponding to the path, if it exists; otherwise,
4040
/// terminating at the deepest existing edge along the path.
4141
#[derive(Debug)]
4242
#[must_use = "iterators are lazy and do nothing unless consumed"]
43-
pub struct TriePathIter<'a, P, N: ?Sized, V: ?Sized> {
43+
pub struct TriePathIter<'root, P, N, V: ?Sized> {
4444
needle: P,
4545
current_path: PathBuf,
46-
current_edge: Option<TrieEdgeState<'a, N>>,
46+
current_edge: Option<TrieEdgeState<'root, N>>,
4747
marker: std::marker::PhantomData<V>,
4848
}
4949

5050
#[derive(Debug)]
51-
struct Frame<'a, N: ?Sized, V: ?Sized> {
52-
node: &'a N,
53-
hash: Option<&'a HashType>,
51+
struct Frame<'root, N, V: ?Sized> {
52+
node: N,
53+
hash: Option<&'root HashType>,
5454
leading_path_len: usize,
5555
children: Option<std::array::IntoIter<PathComponent, { PathComponent::LEN }>>,
5656
marker: std::marker::PhantomData<V>,
5757
}
5858

59-
impl<'a, N, V, D> TrieEdgeIter<'a, N, V, D>
59+
impl<'root, N, V, D> TrieEdgeIter<'root, N, V, D>
6060
where
61-
N: TrieNode<V> + ?Sized,
62-
V: AsRef<[u8]> + ?Sized,
61+
N: TrieNode<'root, V>,
62+
V: AsRef<[u8]> + ?Sized + 'root,
6363
{
6464
/// Creates a new iterator over the given key-value trie.
65-
pub fn new(root: &'a N, root_hash: Option<&'a HashType>) -> Self {
65+
pub fn new(root: N, root_hash: Option<&'root HashType>) -> Self {
6666
let mut this = Self {
6767
leading_path: PathBuf::new_const(),
6868
stack: Vec::new(),
@@ -74,15 +74,15 @@ where
7474

7575
/// Transforms this iterator into an iterator over the key-value pairs in
7676
/// the trie.
77-
pub const fn node_values(self) -> TrieValueIter<'a, N, V, D> {
77+
pub const fn node_values(self) -> TrieValueIter<'root, N, V, D> {
7878
TrieValueIter { edges: self }
7979
}
8080

8181
fn push_frame(
8282
&mut self,
8383
leading_component: Option<PathComponent>,
84-
node: &'a N,
85-
hash: Option<&'a HashType>,
84+
node: N,
85+
hash: Option<&'root HashType>,
8686
) {
8787
let frame = Frame {
8888
node,
@@ -97,15 +97,15 @@ where
9797
}
9898
}
9999

100-
impl<'a, P, N, V> TriePathIter<'a, P, N, V>
100+
impl<'root, P, N, V> TriePathIter<'root, P, N, V>
101101
where
102102
P: SplitPath,
103-
N: TrieNode<V> + ?Sized,
104-
V: AsRef<[u8]> + ?Sized,
103+
N: TrieNode<'root, V>,
104+
V: AsRef<[u8]> + ?Sized + 'root,
105105
{
106106
/// Creates a new iterator over the edges along the given path in the
107107
/// specified key-value trie.
108-
pub const fn new(root: &'a N, root_hash: Option<&'a HashType>, path: P) -> Self {
108+
pub const fn new(root: N, root_hash: Option<&'root HashType>, path: P) -> Self {
109109
let mut this = Self {
110110
needle: path,
111111
current_path: PathBuf::new_const(),
@@ -149,12 +149,12 @@ macro_rules! descend {
149149
};
150150
}
151151

152-
impl<'a, N, V> Iterator for TrieEdgeIter<'a, N, V, IterAscending>
152+
impl<'root, N, V> Iterator for TrieEdgeIter<'root, N, V, IterAscending>
153153
where
154-
N: TrieNode<V> + ?Sized,
155-
V: AsRef<[u8]> + ?Sized,
154+
N: TrieNode<'root, V>,
155+
V: AsRef<[u8]> + ?Sized + 'root,
156156
{
157-
type Item = (PathBuf, TrieEdgeState<'a, N>);
157+
type Item = (PathBuf, TrieEdgeState<'root, N>);
158158

159159
fn next(&mut self) -> Option<Self::Item> {
160160
while let Some(&mut Frame {
@@ -191,12 +191,12 @@ where
191191
}
192192
}
193193

194-
impl<'a, N, V> Iterator for TrieEdgeIter<'a, N, V, IterDescending>
194+
impl<'root, N, V> Iterator for TrieEdgeIter<'root, N, V, IterDescending>
195195
where
196-
N: TrieNode<V> + ?Sized,
197-
V: AsRef<[u8]> + ?Sized,
196+
N: TrieNode<'root, V>,
197+
V: AsRef<[u8]> + ?Sized + 'root,
198198
{
199-
type Item = (PathBuf, TrieEdgeState<'a, N>);
199+
type Item = (PathBuf, TrieEdgeState<'root, N>);
200200

201201
fn next(&mut self) -> Option<Self::Item> {
202202
while let Some(&mut Frame {
@@ -226,39 +226,39 @@ where
226226
}
227227
}
228228

229-
impl<'a, N, V> Iterator for TrieValueIter<'a, N, V, IterAscending>
229+
impl<'root, N, V> Iterator for TrieValueIter<'root, N, V, IterAscending>
230230
where
231-
N: TrieNode<V> + ?Sized,
232-
V: AsRef<[u8]> + ?Sized + 'a,
231+
N: TrieNode<'root, V>,
232+
V: AsRef<[u8]> + ?Sized + 'root,
233233
{
234-
type Item = (PathBuf, &'a V);
234+
type Item = (PathBuf, &'root V);
235235

236236
fn next(&mut self) -> Option<Self::Item> {
237237
self.edges
238238
.find_map(|(path, node)| node.value().map(|v| (path, v)))
239239
}
240240
}
241241

242-
impl<'a, N, V> Iterator for TrieValueIter<'a, N, V, IterDescending>
242+
impl<'root, N, V> Iterator for TrieValueIter<'root, N, V, IterDescending>
243243
where
244-
N: TrieNode<V> + ?Sized,
245-
V: AsRef<[u8]> + ?Sized + 'a,
244+
N: TrieNode<'root, V>,
245+
V: AsRef<[u8]> + ?Sized + 'root,
246246
{
247-
type Item = (PathBuf, &'a V);
247+
type Item = (PathBuf, &'root V);
248248

249249
fn next(&mut self) -> Option<Self::Item> {
250250
self.edges
251251
.find_map(|(path, node)| node.value().map(|v| (path, v)))
252252
}
253253
}
254254

255-
impl<'a, P, N, V> Iterator for TriePathIter<'a, P, N, V>
255+
impl<'root, P, N, V> Iterator for TriePathIter<'root, P, N, V>
256256
where
257257
P: SplitPath,
258-
N: TrieNode<V> + ?Sized,
259-
V: AsRef<[u8]> + ?Sized,
258+
N: TrieNode<'root, V>,
259+
V: AsRef<[u8]> + ?Sized + 'root,
260260
{
261-
type Item = (PathBuf, TrieEdgeState<'a, N>);
261+
type Item = (PathBuf, TrieEdgeState<'root, N>);
262262

263263
fn next(&mut self) -> Option<Self::Item> {
264264
// qualified path to `Option::take` because rust-analyzer thinks
@@ -293,7 +293,7 @@ where
293293

294294
// auto-derived implementations would require N: Clone, V: Clone which is too much
295295

296-
impl<N: ?Sized, V: ?Sized, D> Clone for TrieEdgeIter<'_, N, V, D> {
296+
impl<N: Copy, V: ?Sized, D> Clone for TrieEdgeIter<'_, N, V, D> {
297297
fn clone(&self) -> Self {
298298
Self {
299299
leading_path: self.leading_path.clone(),
@@ -308,7 +308,7 @@ impl<N: ?Sized, V: ?Sized, D> Clone for TrieEdgeIter<'_, N, V, D> {
308308
}
309309
}
310310

311-
impl<N: ?Sized, V: ?Sized, D> Clone for TrieValueIter<'_, N, V, D> {
311+
impl<N: Copy, V: ?Sized, D> Clone for TrieValueIter<'_, N, V, D> {
312312
fn clone(&self) -> Self {
313313
Self {
314314
edges: self.edges.clone(),
@@ -320,7 +320,7 @@ impl<N: ?Sized, V: ?Sized, D> Clone for TrieValueIter<'_, N, V, D> {
320320
}
321321
}
322322

323-
impl<P: SplitPath, N: ?Sized, V: ?Sized> Clone for TriePathIter<'_, P, N, V> {
323+
impl<P: Copy, N: Copy, V: ?Sized> Clone for TriePathIter<'_, P, N, V> {
324324
fn clone(&self) -> Self {
325325
Self {
326326
needle: self.needle,
@@ -337,7 +337,7 @@ impl<P: SplitPath, N: ?Sized, V: ?Sized> Clone for TriePathIter<'_, P, N, V> {
337337
}
338338
}
339339

340-
impl<N: ?Sized, V: ?Sized> Clone for Frame<'_, N, V> {
340+
impl<N: Copy, V: ?Sized> Clone for Frame<'_, N, V> {
341341
fn clone(&self) -> Self {
342342
Self {
343343
node: self.node,

storage/src/tries/kvp.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -275,67 +275,70 @@ impl<'a, T: AsRef<[u8]> + ?Sized> HashedKeyValueTrieRoot<'a, T> {
275275
}
276276
}
277277

278-
impl<T: AsRef<[u8]> + ?Sized> TrieNode<T> for KeyValueTrieRoot<'_, T> {
279-
type PartialPath<'a>
280-
= PackedPathRef<'a>
281-
where
282-
Self: 'a;
278+
impl<'root, T> TrieNode<'root, T> for &'root KeyValueTrieRoot<'root, T>
279+
where
280+
T: AsRef<[u8]> + ?Sized + 'root,
281+
{
282+
type PartialPath = PackedPathRef<'root>;
283283

284-
fn partial_path(&self) -> Self::PartialPath<'_> {
284+
fn partial_path(self) -> Self::PartialPath {
285285
self.partial_path
286286
}
287287

288-
fn value(&self) -> Option<&T> {
288+
fn value(self) -> Option<&'root T> {
289289
self.value
290290
}
291291

292-
fn child_hash(&self, pc: PathComponent) -> Option<&HashType> {
292+
fn child_hash(self, pc: PathComponent) -> Option<&'root HashType> {
293293
let _ = pc;
294294
None
295295
}
296296

297-
fn child_node(&self, pc: PathComponent) -> Option<&Self> {
297+
fn child_node(self, pc: PathComponent) -> Option<Self> {
298298
self.children[pc].as_deref()
299299
}
300300

301-
fn child_state(&self, pc: PathComponent) -> Option<super::TrieEdgeState<'_, Self>> {
301+
fn child_state(self, pc: PathComponent) -> Option<super::TrieEdgeState<'root, Self>> {
302302
self.children[pc]
303303
.as_deref()
304304
.map(|node| super::TrieEdgeState::UnhashedChild { node })
305305
}
306306
}
307307

308-
impl<T: AsRef<[u8]> + ?Sized> TrieNode<T> for HashedKeyValueTrieRoot<'_, T> {
309-
type PartialPath<'a>
310-
= PackedPathRef<'a>
311-
where
312-
Self: 'a;
308+
impl<'root, T> TrieNode<'root, T> for &'root HashedKeyValueTrieRoot<'root, T>
309+
where
310+
T: AsRef<[u8]> + ?Sized + 'root,
311+
{
312+
type PartialPath = PackedPathRef<'root>;
313313

314-
fn partial_path(&self) -> Self::PartialPath<'_> {
314+
fn partial_path(self) -> Self::PartialPath {
315315
self.partial_path
316316
}
317317

318-
fn value(&self) -> Option<&T> {
318+
fn value(self) -> Option<&'root T> {
319319
self.value
320320
}
321321

322-
fn child_hash(&self, pc: PathComponent) -> Option<&HashType> {
322+
fn child_hash(self, pc: PathComponent) -> Option<&'root HashType> {
323323
self.children[pc].as_deref().map(|c| &c.computed)
324324
}
325325

326-
fn child_node(&self, pc: PathComponent) -> Option<&Self> {
326+
fn child_node(self, pc: PathComponent) -> Option<Self> {
327327
self.children[pc].as_deref()
328328
}
329329

330-
fn child_state(&self, pc: PathComponent) -> Option<super::TrieEdgeState<'_, Self>> {
330+
fn child_state(self, pc: PathComponent) -> Option<super::TrieEdgeState<'root, Self>> {
331331
self.children[pc]
332332
.as_deref()
333333
.map(|node| super::TrieEdgeState::from_node(node, Some(&node.computed)))
334334
}
335335
}
336336

337-
impl<T: AsRef<[u8]> + ?Sized> HashedTrieNode<T> for HashedKeyValueTrieRoot<'_, T> {
338-
fn computed(&self) -> &HashType {
337+
impl<'root, T> HashedTrieNode<'root, T> for &'root HashedKeyValueTrieRoot<'root, T>
338+
where
339+
T: AsRef<[u8]> + ?Sized + 'root,
340+
{
341+
fn computed(self) -> &'root HashType {
339342
&self.computed
340343
}
341344
}

0 commit comments

Comments
 (0)