I am working on a project involving Rust and WebAssembly using the "wasm32-unknown-unknown" target. It is no problem to return a Vec<i32>
from my Rust code using this function:
#[no_mangle]
pub extern "C" fn calc_vector() -> usize {
unsafe {
vec_len = 0;
}
let mut data: Vec<i32> = Vec::new();
for i in 0..1000 {
data.push(i);
}
unsafe {
vec_len = data.len();
}
data.as_mut_ptr() as usize
}
This returns an offset and I call another function from JS to get the length of my Vec
. I then build the Vector in JavaScript again (knowing that i32
-> 4x uint8
):
let vec_addr = exports.calc_vector();
let vec_len = exports.get_vec_len();
while(arr.length < vec_len) {
let numberUint8 = new DataView(view.buffer, vec_addr, 4);
let number = numberUint8.getInt32(0, true);
arr.push(number)
// move to next value in vector
vec_addr += 4;
}
Using this, I want to create a Rust function that returns a Vec<Vec<i32>>
, however, it just won't work:
#[no_mangle]
pub extern "C" fn calc_vector_in_vector() -> usize {
unsafe {
vec_len = 0;
elements_in_vect = 0;
}
let mut outer_vec: Vec<*mut i32> = Vec::new();
let mut inner_vec: Vec<i32> = Vec::new();
for i in 0..100 {
inner_vec.push(i);
unsafe {
elements_in_vect += 1;
}
}
outer_vec.push(inner_vec.as_mut_ptr());
unsafe {
vec_len = outer_vec.len();
}
outer_vec.as_mut_ptr() as usize
}
I thought that I could use the same logic as with a single Vec
: at the address returned from calc_vector_in_vector()
is the first entry of the outer vector, containing the address as i32
of the first element of the inner vector (an actual number). However, on this address there seems to be nonsense. What I am doing wrong here?