From 400c0ac4bc7d9668451ed6cc0e3b5fe4361b6cf8 Mon Sep 17 00:00:00 2001 From: Philipp Gackstatter Date: Fri, 1 Nov 2024 20:25:49 +0100 Subject: [PATCH] chore(core): Add doc comments for `node_count` --- core/src/mast/mod.rs | 13 +++---------- core/src/mast/serialization/info.rs | 4 ++++ 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/core/src/mast/mod.rs b/core/src/mast/mod.rs index 0b661ee32..773a85958 100644 --- a/core/src/mast/mod.rs +++ b/core/src/mast/mod.rs @@ -520,15 +520,7 @@ impl MastNodeId { value: u32, mast_forest: &MastForest, ) -> Result { - if (value as usize) < mast_forest.nodes.len() { - Ok(Self(value)) - } else { - Err(DeserializationError::InvalidValue(format!( - "Invalid deserialized MAST node ID '{}', but only {} nodes in the forest", - value, - mast_forest.nodes.len(), - ))) - } + Self::from_u32_with_node_count(value, mast_forest.nodes.len()) } /// Returns a new [`MastNodeId`] from the given `value` without checking its validity. @@ -537,7 +529,8 @@ impl MastNodeId { } /// Returns a new [`MastNodeId`] with the provided `id`, or an error if `id` is greater or equal - /// to `node_count`. + /// to `node_count`. The `node_count` is the total number of nodes in the [`MastForest`] for + /// which this ID is being constructed. /// /// This function can be used when deserializing an id whose corresponding node is not yet in /// the forest and [`Self::from_u32_safe`] would fail. For instance, when deserializing the ids diff --git a/core/src/mast/serialization/info.rs b/core/src/mast/serialization/info.rs index bf9e417d0..1d26177db 100644 --- a/core/src/mast/serialization/info.rs +++ b/core/src/mast/serialization/info.rs @@ -42,6 +42,10 @@ impl MastNodeInfo { Self { ty, digest: mast_node.digest() } } + /// Attempts to convert this [`MastNodeInfo`] into a [`MastNode`] for the given `mast_forest`. + /// + /// The `node_count` is the total expected number of nodes in the [`MastForest`] **after + /// deserialization**. pub fn try_into_mast_node( self, mast_forest: &MastForest,