Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub enum Error {
IntraRebaseZeroHash,
IntraRebaseZeroDepth,
IntraRebaseRepeatVisit,
InvalidLengthFromParts { length: usize, max_length: usize },
}

impl Display for Error {
Expand Down
30 changes: 15 additions & 15 deletions src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,30 @@ impl<T: Value, N: Unsigned, U: UpdateMap<T>> List<T, N, U> {
Self::try_from_iter(vec)
}

pub(crate) fn from_parts(tree: Arc<Tree<T>>, depth: usize, length: Length) -> Self {
pub(crate) fn from_parts(tree: Arc<Tree<T>>, length: Length) -> Result<Self, Error> {
if length.as_usize() > N::to_usize() {
return Err(Error::InvalidLengthFromParts {
length: length.as_usize(),
max_length: N::to_usize(),
});
}
let packing_depth = opt_packing_depth::<T>().unwrap_or(0);
Self {
Ok(Self {
interface: Interface::new(ListInner {
tree,
length,
depth,
depth: Self::depth(),
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, depth, Length(0))
Self::from_parts(tree, Length(0)).expect("empty list should be valid")
}

pub fn repeat(elem: T, n: usize) -> Result<Self, Error> {
Expand All @@ -92,15 +98,9 @@ impl<T: Value, N: Unsigned, U: UpdateMap<T>> List<T, N, U> {
builder.push(item)?;
}

let (tree, depth, 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);
}
let (tree, _, length) = builder.finish()?;

Ok(Self::from_parts(tree, depth, length))
Self::from_parts(tree, length)
}

/// This method exists for testing purposes.
Expand Down Expand Up @@ -239,8 +239,8 @@ impl<T: Value, N: Unsigned, U: UpdateMap<T>> List<T, N, U> {
}
}

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(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,5 @@ where
return Err(Error::BuilderStackLeftover);
}

Ok(List::from_parts(root, tree_depth, Length(n)))
List::from_parts(root, Length(n))
}
3 changes: 1 addition & 2 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,8 @@ impl<T: Value, N: Unsigned, U: UpdateMap<T>> From<Vector<T, N, U>> for List<T, N
fn from(vector: Vector<T, N, U>) -> Self {
let mut list = List::from_parts(
vector.interface.backing.tree,
vector.interface.backing.depth,
Length(N::to_usize()),
);
).expect("vector length should be within List capacity");
list.interface.updates = vector.interface.updates;
list
}
Expand Down