Skip to content

Commit

Permalink
Small vec cleanup: (stellar#1070)
Browse files Browse the repository at this point in the history
- Add coverage for inserting in the middle
- Remove unused function
  • Loading branch information
dmkozh authored Sep 15, 2023
1 parent cf5c856 commit 6460cf6
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 16 deletions.
16 changes: 0 additions & 16 deletions soroban-env-host/src/host/metered_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,22 +302,6 @@ where
}
}

pub fn retain_mut<F>(&mut self, mut f: F, budget: &Budget) -> Result<Self, HostError>
where
F: FnMut(usize, &mut A) -> Result<bool, HostError>,
{
// The closure evaluation is not metered here, it is assumed to be taken care of outside.
// Here just covers the cost of cloning a Vec.
self.charge_deep_clone(budget)?;
let mut vec = Vec::with_capacity(self.len());
for (i, v) in self.vec.iter_mut().enumerate() {
if f(i, v)? {
vec.push(v.clone());
}
}
Self::from_vec(vec)
}

pub fn iter(&self) -> std::slice::Iter<'_, A> {
self.vec.iter()
}
Expand Down
4 changes: 4 additions & 0 deletions soroban-env-host/src/test/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ fn vec_insert_and_cmp() -> Result<(), HostError> {
let obj2 = host.vec_insert(obj1, 2u32.into(), 3u32.into())?;
let obj_ref = host.test_vec_obj::<u32>(&[1, 2, 3])?;
assert_eq!(host.obj_cmp(obj2.into(), obj_ref.into())?, 0);

let obj3 = host.vec_insert(obj2, 2u32.into(), 4u32.into())?;
let obj_ref = host.test_vec_obj::<u32>(&[1, 2, 4, 3])?;
assert_eq!(host.obj_cmp(obj3.into(), obj_ref.into())?, 0);
Ok(())
}

Expand Down

0 comments on commit 6460cf6

Please sign in to comment.