trie_db/
node.rs

1// Copyright 2017, 2021 Parity Technologies
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use core::iter;
16
17use crate::{
18	nibble::{self, nibble_ops, NibbleSlice, NibbleVec},
19	node_codec::NodeCodec,
20	Bytes, CError, ChildReference, Result, TrieError, TrieHash, TrieLayout,
21};
22#[cfg(not(feature = "std"))]
23use alloc::{boxed::Box, vec::Vec};
24use hash_db::Hasher;
25
26use crate::rstd::{borrow::Borrow, mem, ops::Range};
27
28/// Partial node key type: offset and owned value of a nibbleslice.
29/// Offset is applied on first byte of array (bytes are right aligned).
30pub type NodeKey = (usize, nibble::BackingByteVec);
31
32/// A reference to a trie node which may be stored within another trie node.
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum NodeHandle<'a> {
35	Hash(&'a [u8]),
36	Inline(&'a [u8]),
37}
38
39impl NodeHandle<'_> {
40	/// Converts this node handle into a [`NodeHandleOwned`].
41	pub fn to_owned_handle<L: TrieLayout>(
42		&self,
43	) -> Result<NodeHandleOwned<TrieHash<L>>, TrieHash<L>, CError<L>> {
44		match self {
45			Self::Hash(h) => decode_hash::<L::Hash>(h)
46				.ok_or_else(|| Box::new(TrieError::InvalidHash(Default::default(), h.to_vec())))
47				.map(NodeHandleOwned::Hash),
48			Self::Inline(i) => match L::Codec::decode(i) {
49				Ok(node) => Ok(NodeHandleOwned::Inline(Box::new(node.to_owned_node::<L>()?))),
50				Err(e) => Err(Box::new(TrieError::DecoderError(Default::default(), e))),
51			},
52		}
53	}
54}
55
56/// Owned version of [`NodeHandleOwned`].
57#[derive(Clone, PartialEq, Eq)]
58#[cfg_attr(feature = "std", derive(Debug))]
59pub enum NodeHandleOwned<H> {
60	Hash(H),
61	Inline(Box<NodeOwned<H>>),
62}
63
64impl<H> NodeHandleOwned<H>
65where
66	H: Default + AsRef<[u8]> + AsMut<[u8]> + Copy,
67{
68	/// Returns `self` as a [`ChildReference`].
69	///
70	/// # Panic
71	///
72	/// This function panics if `self == Self::Inline(_)` and the inline node encoded length is
73	/// greater then the length of the hash.
74	fn as_child_reference<C: NodeCodec<HashOut = H>>(&self) -> ChildReference<H> {
75		match self {
76			NodeHandleOwned::Hash(h) => ChildReference::Hash(*h),
77			NodeHandleOwned::Inline(n) => {
78				let encoded = n.to_encoded::<C>();
79				let mut store = H::default();
80				assert!(store.as_ref().len() > encoded.len(), "Invalid inline node handle");
81
82				store.as_mut()[..encoded.len()].copy_from_slice(&encoded);
83				ChildReference::Inline(store, encoded.len())
84			},
85		}
86	}
87}
88
89impl<H> NodeHandleOwned<H> {
90	/// Returns `self` as inline node.
91	pub fn as_inline(&self) -> Option<&NodeOwned<H>> {
92		match self {
93			Self::Hash(_) => None,
94			Self::Inline(node) => Some(&*node),
95		}
96	}
97}
98
99/// Read a hash from a slice into a Hasher output. Returns None if the slice is the wrong length.
100pub fn decode_hash<H: Hasher>(data: &[u8]) -> Option<H::Out> {
101	if data.len() != H::LENGTH {
102		return None
103	}
104	let mut hash = H::Out::default();
105	hash.as_mut().copy_from_slice(data);
106	Some(hash)
107}
108
109/// Value representation in `Node`.
110#[derive(Eq, PartialEq, Clone)]
111#[cfg_attr(feature = "std", derive(Debug))]
112pub enum Value<'a> {
113	/// Value byte slice as stored in a trie node.
114	Inline(&'a [u8]),
115	/// Hash byte slice as stored in a trie node.
116	Node(&'a [u8]),
117}
118
119impl<'a> Value<'a> {
120	pub(crate) fn new_inline(value: &'a [u8], threshold: Option<u32>) -> Option<Self> {
121		if let Some(threshold) = threshold {
122			if value.len() >= threshold as usize {
123				return None
124			} else {
125				Some(Value::Inline(value))
126			}
127		} else {
128			Some(Value::Inline(value))
129		}
130	}
131
132	pub fn to_owned_value<L: TrieLayout>(&self) -> ValueOwned<TrieHash<L>> {
133		match self {
134			Self::Inline(data) => ValueOwned::Inline(Bytes::from(*data), L::Hash::hash(data)),
135			Self::Node(hash) => {
136				let mut res = TrieHash::<L>::default();
137				res.as_mut().copy_from_slice(hash);
138
139				ValueOwned::Node(res)
140			},
141		}
142	}
143}
144
145/// Owned value representation in `Node`.
146#[derive(Eq, PartialEq, Clone)]
147#[cfg_attr(feature = "std", derive(Debug))]
148pub enum ValueOwned<H> {
149	/// Value bytes as stored in a trie node and its hash.
150	Inline(Bytes, H),
151	/// Hash byte slice as stored in a trie node.
152	Node(H),
153}
154
155impl<H: AsRef<[u8]> + Copy> ValueOwned<H> {
156	/// Returns self as [`Value`].
157	pub fn as_value(&self) -> Value {
158		match self {
159			Self::Inline(data, _) => Value::Inline(&data),
160			Self::Node(hash) => Value::Node(hash.as_ref()),
161		}
162	}
163
164	/// Returns the hash of the data stored in self.
165	pub fn data_hash(&self) -> Option<H> {
166		match self {
167			Self::Inline(_, hash) => Some(*hash),
168			Self::Node(hash) => Some(*hash),
169		}
170	}
171}
172
173impl<H> ValueOwned<H> {
174	/// Returns the data stored in self.
175	pub fn data(&self) -> Option<&Bytes> {
176		match self {
177			Self::Inline(data, _) => Some(data),
178			Self::Node(_) => None,
179		}
180	}
181}
182
183/// Type of node in the trie and essential information thereof.
184#[derive(Eq, PartialEq, Clone)]
185#[cfg_attr(feature = "std", derive(Debug))]
186pub enum Node<'a> {
187	/// Null trie node; could be an empty root or an empty branch entry.
188	Empty,
189	/// Leaf node; has key slice and value. Value may not be empty.
190	Leaf(NibbleSlice<'a>, Value<'a>),
191	/// Extension node; has key slice and node data. Data may not be null.
192	Extension(NibbleSlice<'a>, NodeHandle<'a>),
193	/// Branch node; has slice of child nodes (each possibly null)
194	/// and an optional immediate node data.
195	Branch([Option<NodeHandle<'a>>; nibble_ops::NIBBLE_LENGTH], Option<Value<'a>>),
196	/// Branch node with support for a nibble (when extension nodes are not used).
197	NibbledBranch(
198		NibbleSlice<'a>,
199		[Option<NodeHandle<'a>>; nibble_ops::NIBBLE_LENGTH],
200		Option<Value<'a>>,
201	),
202}
203
204impl Node<'_> {
205	/// Converts this node into a [`NodeOwned`].
206	pub fn to_owned_node<L: TrieLayout>(
207		&self,
208	) -> Result<NodeOwned<TrieHash<L>>, TrieHash<L>, CError<L>> {
209		match self {
210			Self::Empty => Ok(NodeOwned::Empty),
211			Self::Leaf(n, d) => Ok(NodeOwned::Leaf((*n).into(), d.to_owned_value::<L>())),
212			Self::Extension(n, h) =>
213				Ok(NodeOwned::Extension((*n).into(), h.to_owned_handle::<L>()?)),
214			Self::Branch(childs, data) => {
215				let childs_owned = if let Some(last_existing_child_index) =
216					childs.iter().rposition(Option::is_some)
217				{
218					let mut childs_owned = Vec::with_capacity(last_existing_child_index + 1);
219
220					childs
221						.iter()
222						.enumerate()
223						.map(|(i, c)| {
224							if i <= last_existing_child_index {
225								childs_owned.push(
226									c.as_ref().map(|c| c.to_owned_handle::<L>()).transpose()?,
227								);
228							}
229							Ok(())
230						})
231						.collect::<Result<_, _, _>>()?;
232					childs_owned
233				} else {
234					Vec::with_capacity(0)
235				};
236
237				Ok(NodeOwned::Branch(
238					ChildrenNodesOwned(childs_owned),
239					data.as_ref().map(|d| d.to_owned_value::<L>()),
240				))
241			},
242			Self::NibbledBranch(n, childs, data) => {
243				let childs_owned = if let Some(last_existing_child_index) =
244					childs.iter().rposition(Option::is_some)
245				{
246					let mut childs_owned = Vec::with_capacity(last_existing_child_index + 1);
247
248					childs
249						.iter()
250						.enumerate()
251						.map(|(i, c)| {
252							if i <= last_existing_child_index {
253								childs_owned.push(
254									c.as_ref().map(|c| c.to_owned_handle::<L>()).transpose()?,
255								);
256							}
257							Ok(())
258						})
259						.collect::<Result<_, _, _>>()?;
260					childs_owned
261				} else {
262					Vec::with_capacity(0)
263				};
264
265				Ok(NodeOwned::NibbledBranch(
266					(*n).into(),
267					ChildrenNodesOwned(childs_owned),
268					data.as_ref().map(|d| d.to_owned_value::<L>()),
269				))
270			},
271		}
272	}
273}
274
275/// Owned version of [`Node`].
276#[derive(Eq, PartialEq, Clone)]
277#[cfg_attr(feature = "std", derive(Debug))]
278pub enum NodeOwned<H> {
279	/// Null trie node; could be an empty root or an empty branch entry.
280	Empty,
281	/// Leaf node; has key slice and value. Value may not be empty.
282	Leaf(NibbleVec, ValueOwned<H>),
283	/// Extension node; has key slice and node data. Data may not be null.
284	Extension(NibbleVec, NodeHandleOwned<H>),
285	/// Branch node; has slice of child nodes (each possibly null)
286	/// and an optional immediate node data.
287	Branch(ChildrenNodesOwned<H>, Option<ValueOwned<H>>),
288	/// Branch node with support for a nibble (when extension nodes are not used).
289	NibbledBranch(NibbleVec, ChildrenNodesOwned<H>, Option<ValueOwned<H>>),
290	/// Node that represents a value.
291	///
292	/// This variant is only constructed when working with a [`crate::TrieCache`]. It is only
293	/// used to cache a raw value.
294	Value(Bytes, H),
295}
296
297/// Owned children of a node.
298///
299/// The maximum size of the children is `nibble_ops::NIBBLE_LENGTH`, in order to save memory we
300/// represent only the children until the last missing one. For example, if the children are
301/// `[Some(_), None, Some(_), None, None, None, None, None None, None, None, None, None, None, None,
302/// None]` we will only store in the vector `[Some(_), None, Some(_)]`.
303///
304/// This is useful to save space in the TrieCache when the children are not fully populated.
305///
306/// Before this struct was introduced, the children were stored in a fixed size array of length
307/// `nibble_ops::NIBBLE_LENGTH`, to make sure there aren't any direct accesses to the array that
308/// we've missed, wrap it in its own struct, rather than directly use the `Vec`.
309#[derive(Eq, PartialEq, Clone)]
310#[cfg_attr(feature = "std", derive(Debug))]
311pub struct ChildrenNodesOwned<H>(Vec<Option<NodeHandleOwned<H>>>);
312
313impl<H> ChildrenNodesOwned<H> {
314	/// Returns the child at the given index.
315	pub fn get(&self, index: usize) -> Option<&NodeHandleOwned<H>> {
316		self.0.get(index).and_then(|node| node.as_ref())
317	}
318
319	/// Returns an iterator over all children.
320	///
321	/// The size of the iterator is always constant goes up to `nibble_ops::NIBBLE_LENGTH`, with
322	/// None representing the missing children.
323	pub fn iter(&self) -> impl Iterator<Item = &Option<NodeHandleOwned<H>>> {
324		self.0.iter().chain(iter::repeat(&None)).take(nibble_ops::NIBBLE_LENGTH)
325	}
326
327	/// Returns the number of allocated children.
328	pub fn allocated_len(&self) -> usize {
329		self.0.len()
330	}
331}
332
333impl<H> NodeOwned<H>
334where
335	H: Default + AsRef<[u8]> + AsMut<[u8]> + Copy,
336{
337	/// Convert to its encoded format.
338	pub fn to_encoded<C>(&self) -> Vec<u8>
339	where
340		C: NodeCodec<HashOut = H>,
341	{
342		match self {
343			Self::Empty => C::empty_node().to_vec(),
344			Self::Leaf(partial, value) =>
345				C::leaf_node(partial.right_iter(), partial.len(), value.as_value()),
346			Self::Extension(partial, child) => C::extension_node(
347				partial.right_iter(),
348				partial.len(),
349				child.as_child_reference::<C>(),
350			),
351			Self::Branch(children, value) => C::branch_node(
352				children.iter().map(|child| child.as_ref().map(|c| c.as_child_reference::<C>())),
353				value.as_ref().map(|v| v.as_value()),
354			),
355			Self::NibbledBranch(partial, children, value) => C::branch_node_nibbled(
356				partial.right_iter(),
357				partial.len(),
358				children.iter().map(|child| child.as_ref().map(|c| c.as_child_reference::<C>())),
359				value.as_ref().map(|v| v.as_value()),
360			),
361			Self::Value(data, _) => data.to_vec(),
362		}
363	}
364
365	/// Returns an iterator over all existing children with their optional nibble.
366	pub fn child_iter(&self) -> impl Iterator<Item = (Option<u8>, &NodeHandleOwned<H>)> {
367		enum ChildIter<'a, H> {
368			Empty,
369			Single(&'a NodeHandleOwned<H>, bool),
370			Array(&'a ChildrenNodesOwned<H>, usize),
371		}
372
373		impl<'a, H> Iterator for ChildIter<'a, H> {
374			type Item = (Option<u8>, &'a NodeHandleOwned<H>);
375
376			fn next(&mut self) -> Option<Self::Item> {
377				loop {
378					match self {
379						Self::Empty => break None,
380						Self::Single(child, returned) =>
381							break if *returned {
382								None
383							} else {
384								*returned = true;
385								Some((None, child))
386							},
387						Self::Array(childs, index) =>
388							if *index >= childs.allocated_len() {
389								break None
390							} else {
391								*index += 1;
392
393								// Ignore non-existing childs.
394								if let Some(ref child) = childs.get(*index - 1) {
395									break Some((Some(*index as u8 - 1), child))
396								}
397							},
398					}
399				}
400			}
401		}
402
403		match self {
404			Self::Leaf(_, _) | Self::Empty | Self::Value(_, _) => ChildIter::Empty,
405			Self::Extension(_, child) => ChildIter::Single(child, false),
406			Self::Branch(children, _) | Self::NibbledBranch(_, children, _) =>
407				ChildIter::Array(children, 0),
408		}
409	}
410
411	/// Returns the hash of the data attached to this node.
412	pub fn data_hash(&self) -> Option<H> {
413		match &self {
414			Self::Empty => None,
415			Self::Leaf(_, value) => value.data_hash(),
416			Self::Extension(_, _) => None,
417			Self::Branch(_, value) => value.as_ref().and_then(|v| v.data_hash()),
418			Self::NibbledBranch(_, _, value) => value.as_ref().and_then(|v| v.data_hash()),
419			Self::Value(_, hash) => Some(*hash),
420		}
421	}
422}
423
424impl<H> NodeOwned<H> {
425	/// Returns the data attached to this node.
426	pub fn data(&self) -> Option<&Bytes> {
427		match &self {
428			Self::Empty => None,
429			Self::Leaf(_, value) => value.data(),
430			Self::Extension(_, _) => None,
431			Self::Branch(_, value) => value.as_ref().and_then(|v| v.data()),
432			Self::NibbledBranch(_, _, value) => value.as_ref().and_then(|v| v.data()),
433			Self::Value(data, _) => Some(data),
434		}
435	}
436
437	/// Returns the partial key of this node.
438	pub fn partial_key(&self) -> Option<&NibbleVec> {
439		match self {
440			Self::Branch(_, _) | Self::Value(_, _) | Self::Empty => None,
441			Self::Extension(partial, _) |
442			Self::Leaf(partial, _) |
443			Self::NibbledBranch(partial, _, _) => Some(partial),
444		}
445	}
446
447	/// Returns the size in bytes of this node in memory.
448	///
449	/// This also includes the size of any inline child nodes.
450	pub fn size_in_bytes(&self) -> usize {
451		let self_size = mem::size_of::<Self>();
452
453		fn childs_size<'a, H: 'a>(
454			childs: impl Iterator<Item = &'a Option<NodeHandleOwned<H>>>,
455		) -> usize {
456			// If a `child` isn't an inline node, its size is already taken account for by
457			// `self_size`.
458			childs
459				.filter_map(|c| c.as_ref())
460				.map(|c| c.as_inline().map_or(0, |n| n.size_in_bytes()))
461				.sum()
462		}
463
464		// As `self_size` only represents the static size of `Self`, we also need
465		// to add the size of any dynamically allocated data.
466		let dynamic_size = match self {
467			Self::Empty => 0,
468			Self::Leaf(nibbles, value) =>
469				nibbles.inner().len() + value.data().map_or(0, |b| b.len()),
470			Self::Value(bytes, _) => bytes.len(),
471			Self::Extension(nibbles, child) => {
472				// If the `child` isn't an inline node, its size is already taken account for by
473				// `self_size`.
474				nibbles.inner().len() + child.as_inline().map_or(0, |n| n.size_in_bytes())
475			},
476			Self::Branch(childs, value) =>
477				childs_size(childs.iter()) +
478					value.as_ref().and_then(|v| v.data()).map_or(0, |b| b.len()),
479			Self::NibbledBranch(nibbles, childs, value) =>
480				nibbles.inner().len() +
481					childs_size(childs.iter()) +
482					value.as_ref().and_then(|v| v.data()).map_or(0, |b| b.len()),
483		};
484
485		self_size + dynamic_size
486	}
487}
488
489/// A `NodeHandlePlan` is a decoding plan for constructing a `NodeHandle` from an encoded trie
490/// node. This is used as a substructure of `NodePlan`. See `NodePlan` for details.
491#[derive(Debug, Clone, PartialEq, Eq)]
492pub enum NodeHandlePlan {
493	Hash(Range<usize>),
494	Inline(Range<usize>),
495}
496
497impl NodeHandlePlan {
498	/// Build a node handle by decoding a byte slice according to the node handle plan. It is the
499	/// responsibility of the caller to ensure that the node plan was created for the argument
500	/// data, otherwise the call may decode incorrectly or panic.
501	pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> NodeHandle<'b> {
502		match self {
503			NodeHandlePlan::Hash(range) => NodeHandle::Hash(&data[range.clone()]),
504			NodeHandlePlan::Inline(range) => NodeHandle::Inline(&data[range.clone()]),
505		}
506	}
507}
508
509/// A `NibbleSlicePlan` is a blueprint for decoding a nibble slice from a byte slice. The
510/// `NibbleSlicePlan` is created by parsing a byte slice and can be reused multiple times.
511#[derive(Eq, PartialEq, Clone)]
512#[cfg_attr(feature = "std", derive(Debug))]
513pub struct NibbleSlicePlan {
514	bytes: Range<usize>,
515	offset: usize,
516}
517
518impl NibbleSlicePlan {
519	/// Construct a nibble slice decode plan.
520	pub fn new(bytes: Range<usize>, offset: usize) -> Self {
521		NibbleSlicePlan { bytes, offset }
522	}
523
524	/// Returns the nibble length of the slice.
525	pub fn len(&self) -> usize {
526		(self.bytes.end - self.bytes.start) * nibble_ops::NIBBLE_PER_BYTE - self.offset
527	}
528
529	/// Build a nibble slice by decoding a byte slice according to the plan. It is the
530	/// responsibility of the caller to ensure that the node plan was created for the argument
531	/// data, otherwise the call may decode incorrectly or panic.
532	pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> NibbleSlice<'b> {
533		NibbleSlice::new_offset(&data[self.bytes.clone()], self.offset)
534	}
535}
536
537/// Plan for value representation in `NodePlan`.
538#[derive(Eq, PartialEq, Clone)]
539#[cfg_attr(feature = "std", derive(Debug))]
540pub enum ValuePlan {
541	/// Range for byte representation in encoded node.
542	Inline(Range<usize>),
543	/// Range for hash in encoded node and original
544	/// value size.
545	Node(Range<usize>),
546}
547
548impl ValuePlan {
549	/// Build a value slice by decoding a byte slice according to the plan.
550	pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> Value<'b> {
551		match self {
552			ValuePlan::Inline(range) => Value::Inline(&data[range.clone()]),
553			ValuePlan::Node(range) => Value::Node(&data[range.clone()]),
554		}
555	}
556}
557
558/// A `NodePlan` is a blueprint for decoding a node from a byte slice. The `NodePlan` is created
559/// by parsing an encoded node and can be reused multiple times. This is useful as a `Node` borrows
560/// from a byte slice and this struct does not.
561///
562/// The enum values mirror those of `Node` except that instead of byte slices, this struct stores
563/// ranges that can be used to index into a large byte slice.
564#[derive(Eq, PartialEq, Clone)]
565#[cfg_attr(feature = "std", derive(Debug))]
566pub enum NodePlan {
567	/// Null trie node; could be an empty root or an empty branch entry.
568	Empty,
569	/// Leaf node; has a partial key plan and value.
570	Leaf { partial: NibbleSlicePlan, value: ValuePlan },
571	/// Extension node; has a partial key plan and child data.
572	Extension { partial: NibbleSlicePlan, child: NodeHandlePlan },
573	/// Branch node; has slice of child nodes (each possibly null)
574	/// and an optional immediate node data.
575	Branch {
576		value: Option<ValuePlan>,
577		children: [Option<NodeHandlePlan>; nibble_ops::NIBBLE_LENGTH],
578	},
579	/// Branch node with support for a nibble (when extension nodes are not used).
580	NibbledBranch {
581		partial: NibbleSlicePlan,
582		value: Option<ValuePlan>,
583		children: [Option<NodeHandlePlan>; nibble_ops::NIBBLE_LENGTH],
584	},
585}
586
587impl NodePlan {
588	/// Build a node by decoding a byte slice according to the node plan. It is the responsibility
589	/// of the caller to ensure that the node plan was created for the argument data, otherwise the
590	/// call may decode incorrectly or panic.
591	pub fn build<'a, 'b>(&'a self, data: &'b [u8]) -> Node<'b> {
592		match self {
593			NodePlan::Empty => Node::Empty,
594			NodePlan::Leaf { partial, value } => Node::Leaf(partial.build(data), value.build(data)),
595			NodePlan::Extension { partial, child } =>
596				Node::Extension(partial.build(data), child.build(data)),
597			NodePlan::Branch { value, children } => {
598				let mut child_slices = [None; nibble_ops::NIBBLE_LENGTH];
599				for i in 0..nibble_ops::NIBBLE_LENGTH {
600					child_slices[i] = children[i].as_ref().map(|child| child.build(data));
601				}
602				Node::Branch(child_slices, value.as_ref().map(|v| v.build(data)))
603			},
604			NodePlan::NibbledBranch { partial, value, children } => {
605				let mut child_slices = [None; nibble_ops::NIBBLE_LENGTH];
606				for i in 0..nibble_ops::NIBBLE_LENGTH {
607					child_slices[i] = children[i].as_ref().map(|child| child.build(data));
608				}
609				Node::NibbledBranch(
610					partial.build(data),
611					child_slices,
612					value.as_ref().map(|v| v.build(data)),
613				)
614			},
615		}
616	}
617
618	/// Access value plan from node plan, return `None` for
619	/// node that cannot contain a `ValuePlan`.
620	pub fn value_plan(&self) -> Option<&ValuePlan> {
621		match self {
622			NodePlan::Extension { .. } | NodePlan::Empty => None,
623			NodePlan::Leaf { value, .. } => Some(value),
624			NodePlan::Branch { value, .. } | NodePlan::NibbledBranch { value, .. } =>
625				value.as_ref(),
626		}
627	}
628
629	/// Mutable ccess value plan from node plan, return `None` for
630	/// node that cannot contain a `ValuePlan`.
631	pub fn value_plan_mut(&mut self) -> Option<&mut ValuePlan> {
632		match self {
633			NodePlan::Extension { .. } | NodePlan::Empty => None,
634			NodePlan::Leaf { value, .. } => Some(value),
635			NodePlan::Branch { value, .. } | NodePlan::NibbledBranch { value, .. } =>
636				value.as_mut(),
637		}
638	}
639}
640
641/// An `OwnedNode` is an owned type from which a `Node` can be constructed which borrows data from
642/// the `OwnedNode`. This is useful for trie iterators.
643#[cfg_attr(feature = "std", derive(Debug))]
644#[derive(PartialEq, Eq)]
645pub struct OwnedNode<D: Borrow<[u8]>> {
646	data: D,
647	plan: NodePlan,
648}
649
650impl<D: Borrow<[u8]>> OwnedNode<D> {
651	/// Construct an `OwnedNode` by decoding an owned data source according to some codec.
652	pub fn new<C: NodeCodec>(data: D) -> core::result::Result<Self, C::Error> {
653		let plan = C::decode_plan(data.borrow())?;
654		Ok(OwnedNode { data, plan })
655	}
656
657	/// Returns a reference to the backing data.
658	pub fn data(&self) -> &[u8] {
659		self.data.borrow()
660	}
661
662	/// Returns a reference to the node decode plan.
663	pub fn node_plan(&self) -> &NodePlan {
664		&self.plan
665	}
666
667	/// Returns a mutable reference to the node decode plan.
668	pub fn node_plan_mut(&mut self) -> &mut NodePlan {
669		&mut self.plan
670	}
671
672	/// Construct a `Node` by borrowing data from this struct.
673	pub fn node(&self) -> Node {
674		self.plan.build(self.data.borrow())
675	}
676}