I've been stuck on this issue for a while, and was wondering if any of you had any insight. I have a Tree struct, which has a method contains(x: &T)
, which returns true if one of the leaves of the tree is equal to x
. So I create a Vec<T>
in a test function and make sure that every element of this vector is in my Tree. Okay good, Calling MerkleTree::contains
on each element of x
returned true. Now I pass this vector into MerkleTree::prune
where the exact same test is performed, only this time it returns false.
This issue occurs even when I don't call contains
on all the elements before passing them to prune
, ruling out my suspicion that contains
was somehow mutating the elements of my vector. I'm sort of at a loss now, and so was wondering if any of you had encountered a similiar issue. I snipped my code and copied it below so you could get a better look:
#[test]
fn test_func() {
/*let m_tree = MerkleTree::construct(...)
...
Snip */
let to_check = vec!(11.to_string(), 101.to_string());
/* This returns true! */
for element in to_check {
assert!(m_tree.contains(&element).unwrap());
}
/* This method does the same thing as above, but it panics at the assert!*/
m_tree.prune(&to_check)
}
impl<T: Ord + Clone> MerkleTree<T> {
/* Snip */
pub fn prune(&mut self, to_keep: &Vec<T>) -> bool {
if to_keep.len() <= 0 { debug_assert!(false, "To_keep has length less than 1"); return false; }
/*This returns false!*/
for element in to_keep {
if !self.contains(&element).unwrap() {
assert!(false);
return false;
}
}
self._prune(to_keep)
}
/* Snip */
}
Here is the link to my Repo where the non-snipped code is if any of you want more info: https://github.com/rileylyman/newton/tree/master/src