Skip to content

Commit

Permalink
Add Vec::to_vals (#1087)
Browse files Browse the repository at this point in the history
### What
Add Vec::to_vals function that converts a Vec<T> to a Vec<Val>.

### Why
It's reasonably common, at least in tests and internally, that we need
to construct Vec's of Val's. There are different ways to do this by
constructing a Vec of Val::from calls, to using tuples with always
convert to Vec<Val>. We should have a function to help us do it.

I hope to use the function in #1023.
  • Loading branch information
leighmcculloch authored Sep 15, 2023
1 parent c48d4f4 commit 94f29fc
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions soroban-sdk/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ impl<T> Vec<T> {
pub fn to_object(&self) -> VecObject {
self.obj
}

pub fn to_vals(&self) -> Vec<Val> {
unsafe { Vec::<Val>::unchecked_new(self.env().clone(), self.obj) }
}
}

impl<T> Vec<T>
Expand Down Expand Up @@ -1074,6 +1078,24 @@ mod test {
assert!(vec == vec_copy);
}

#[test]
fn test_vec_to_vals() {
let env = Env::default();
let vec = vec![&env, 0, 1, 2, 3, 4];
let vals = vec.to_vals();
assert_eq!(
vals,
vec![
&env,
Val::from_i32(0).to_val(),
Val::from_i32(1).to_val(),
Val::from_i32(2).to_val(),
Val::from_i32(3).to_val(),
Val::from_i32(4).to_val(),
]
);
}

#[test]
fn test_vec_recursive() {
let env = Env::default();
Expand Down

0 comments on commit 94f29fc

Please sign in to comment.