From 2c1a947bfe088e95a568e450340137d95cc8be16 Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Fri, 5 Dec 2025 02:45:15 +0530 Subject: [PATCH 1/3] feat: drop depth parameter from List::from_parts Signed-off-by: Nikhil Sharma --- src/list.rs | 14 +++++++------- src/repeat.rs | 2 +- src/vector.rs | 1 - 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/list.rs b/src/list.rs index ad96b01..dc9e6c8 100644 --- a/src/list.rs +++ b/src/list.rs @@ -53,13 +53,13 @@ impl> List { Self::try_from_iter(vec) } - pub(crate) fn from_parts(tree: Arc>, depth: usize, length: Length) -> Self { + pub(crate) fn from_parts(tree: Arc>, length: Length) -> Self { let packing_depth = opt_packing_depth::().unwrap_or(0); Self { interface: Interface::new(ListInner { tree, length, - depth, + depth: Self::depth(), packing_depth, _phantom: PhantomData, }), @@ -70,7 +70,7 @@ impl> List { // If the leaves are packed then they reduce the depth let depth = Self::depth(); let tree = Tree::empty(depth); - Self::from_parts(tree, depth, Length(0)) + Self::from_parts(tree, Length(0)) } pub fn repeat(elem: T, n: usize) -> Result { @@ -92,7 +92,7 @@ impl> List { builder.push(item)?; } - let (tree, depth, length) = builder.finish()?; + let (tree, _, length) = builder.finish()?; // Check the length to cover the case where the capacity implied by packing_depth is // greater than N. E.g. the builder might pack up to 32 u8s, even if N is < 32. @@ -100,7 +100,7 @@ impl> List { return Err(Error::BuilderFull); } - Ok(Self::from_parts(tree, depth, length)) + Ok(Self::from_parts(tree, length)) } /// This method exists for testing purposes. @@ -239,8 +239,8 @@ impl> List { } } - let (tree, depth, length) = builder.finish()?; - *self = Self::from_parts(tree, depth, length); + let (tree, _, length) = builder.finish()?; + *self = Self::from_parts(tree, length); Ok(()) } diff --git a/src/repeat.rs b/src/repeat.rs index ebaaa8f..a3874cc 100644 --- a/src/repeat.rs +++ b/src/repeat.rs @@ -108,5 +108,5 @@ where return Err(Error::BuilderStackLeftover); } - Ok(List::from_parts(root, tree_depth, Length(n))) + Ok(List::from_parts(root, Length(n))) } diff --git a/src/vector.rs b/src/vector.rs index 2cb91e5..8704688 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -195,7 +195,6 @@ impl> From> for List) -> Self { let mut list = List::from_parts( vector.interface.backing.tree, - vector.interface.backing.depth, Length(N::to_usize()), ); list.interface.updates = vector.interface.updates; From 286b6d87b9a36186741c307cce93f13daeffcc2d Mon Sep 17 00:00:00 2001 From: Nikhil Sharma Date: Fri, 5 Dec 2025 03:00:55 +0530 Subject: [PATCH 2/3] fix: add check in List::from_parts that length passed should be less than capacity N Signed-off-by: Nikhil Sharma --- src/list.rs | 21 +++++++++------------ src/repeat.rs | 2 +- src/vector.rs | 2 +- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/list.rs b/src/list.rs index dc9e6c8..285a206 100644 --- a/src/list.rs +++ b/src/list.rs @@ -53,9 +53,12 @@ impl> List { Self::try_from_iter(vec) } - pub(crate) fn from_parts(tree: Arc>, length: Length) -> Self { + pub(crate) fn from_parts(tree: Arc>, length: Length) -> Result { + if length.as_usize() > N::to_usize() { + return Err(Error::BuilderFull); + } let packing_depth = opt_packing_depth::().unwrap_or(0); - Self { + Ok(Self { interface: Interface::new(ListInner { tree, length, @@ -63,14 +66,14 @@ impl> List { packing_depth, _phantom: PhantomData, }), - } + }) } pub fn empty() -> Self { // If the leaves are packed then they reduce the depth let depth = Self::depth(); let tree = Tree::empty(depth); - Self::from_parts(tree, Length(0)) + Self::from_parts(tree, Length(0)).expect("empty list should be valid") } pub fn repeat(elem: T, n: usize) -> Result { @@ -94,13 +97,7 @@ impl> List { let (tree, _, length) = builder.finish()?; - // Check the length to cover the case where the capacity implied by packing_depth is - // greater than N. E.g. the builder might pack up to 32 u8s, even if N is < 32. - if length.as_usize() > N::to_usize() { - return Err(Error::BuilderFull); - } - - Ok(Self::from_parts(tree, length)) + Self::from_parts(tree, length) } /// This method exists for testing purposes. @@ -240,7 +237,7 @@ impl> List { } let (tree, _, length) = builder.finish()?; - *self = Self::from_parts(tree, length); + *self = Self::from_parts(tree, length)?; Ok(()) } diff --git a/src/repeat.rs b/src/repeat.rs index a3874cc..2e458cd 100644 --- a/src/repeat.rs +++ b/src/repeat.rs @@ -108,5 +108,5 @@ where return Err(Error::BuilderStackLeftover); } - Ok(List::from_parts(root, Length(n))) + List::from_parts(root, Length(n)) } diff --git a/src/vector.rs b/src/vector.rs index 8704688..26c5e28 100644 --- a/src/vector.rs +++ b/src/vector.rs @@ -196,7 +196,7 @@ impl> From> for List Date: Mon, 8 Dec 2025 20:10:49 +0530 Subject: [PATCH 3/3] fix: add new error enum InvalidLengthFromParts and use in List::from_parts Signed-off-by: Nikhil Sharma --- src/error.rs | 1 + src/list.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/error.rs b/src/error.rs index 499712a..03fbc8d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,6 +34,7 @@ pub enum Error { IntraRebaseZeroHash, IntraRebaseZeroDepth, IntraRebaseRepeatVisit, + InvalidLengthFromParts { length: usize, max_length: usize }, } impl Display for Error { diff --git a/src/list.rs b/src/list.rs index 285a206..b7a762c 100644 --- a/src/list.rs +++ b/src/list.rs @@ -55,7 +55,10 @@ impl> List { pub(crate) fn from_parts(tree: Arc>, length: Length) -> Result { if length.as_usize() > N::to_usize() { - return Err(Error::BuilderFull); + return Err(Error::InvalidLengthFromParts { + length: length.as_usize(), + max_length: N::to_usize(), + }); } let packing_depth = opt_packing_depth::().unwrap_or(0); Ok(Self {