英文:
return a rust-wasm vector in angular application
问题
我有一个基本的Angular应用和一个Rust wasm包,我想在Angular中调用Rust编译的代码中的函数。
Rust代码模板来自于lib.rs
中的rust-pack-template-repo:
mod utils;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add_one(number: u32) -> u32
{
number + 1
}
#[wasm_bindgen]
pub fn test_vec_output(num_samples: u32) -> Vec<u32>
{
let my_vec: Vec<u32> = vec![12; num_samples as usize];
my_vec
}
在Angular应用中,我使用了以下代码样本来调用Rust-wasm函数:
async testRustWasmFunctions()
{
import('../wasm-test-pack/pkg/myDummyPackage_bg.wasm').then((response) => {
this.myFirstResult = response.add_one(1);
console.log("Test dummy add: " + this.myFirstResult); // 正常工作!返回2
console.log("Test dummy vector: " + response.test_vec_output(1)); // 只有在我传入两个参数时才起作用
});
}
在HTML部分,函数被这样调用:
<button (click)="testRustWasmFunctions()"></button>
response.test_vec_output(1)
函数调用在传入两个参数时不会产生错误(在Angular中),但控制台结果返回undefined。而add_one
函数正常工作并返回正确的结果。
有没有更好的方法来将Rust-wasm包导入到Angular中,以便更容易处理向量?
英文:
I have a basic angular app and a Rust wasm pack, and I want to call functions in the Rust compiled code in angular.
The rust code template is taken from the rust-pack-template-repo in lib.rs
is the following:
mod utils;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn add_one(number: u32) -> u32
{
number + 1
}
#[wasm_bindgen]
pub fn test_vec_output(num_samples: u32) -> Vec<u32>
{
let my_vec: Vec<u32> = vec![12; num_samples as usize];
my_vec
}
In the Angular app side, I used the following code sample to call the rust-wasm function:
async testRustWasmFunctions()
{
import('../wasm-test-pack/pkg/myDummyPackage_bg.wasm').then((response) => {
this.myFirstResult = response.add_one(1);
console.log("Test dummy add: " + this.myFirstResult); // Works fine !! returns 2
console.log("Test dummy vector: " + response.test_vec_output(1,1)); // Only works if I take two arguments
});
}
In the HTML part the function is called like this:
<button (click)="testRustWasmFunctions()"><\button>
The response.test_vec_output(1,1))
function call returns no error (in angular) when called with two arguments, and the console result returns undefined. While the function add_one
works just fine and returns the correct result.
Is there a better way to import rust-wasm pack to angular than this, such that it is easy to process vectors?
答案1
得分: 1
wasm-bindgen
生成一个js文件(应该被称为myDummyPackage.js
),该文件负责设置一堆东西(如堆和包装器)。
您可以尝试导入生成的.js
文件,如果不起作用,我建议查看以下教程:
英文:
wasm-bindgen
generates a js file (should be called myDummyPackage.js
) which is responsible for setting up a bunch of stuff (like heap and wrappers).
You can try to import generated .js
file, if it doesn't work I recommend looking at these tutorials:
答案2
得分: 0
#[wasm_bindgen]
pub fn test_vec_output(num_samples: u32) -> js_sys::Uint32Array {
let my_vec: Vec<u32> = vec![12; num_samples as usize];
return js_sys::Uint32Array::from(&my_vec[..]);
}
英文:
You likely need to convert your Vec<u32>
to a js_sys::Uint32Array. So your test_vec_output
would look like:
#[wasm_bindgen]
pub fn test_vec_output(num_samples: u32) -> js_sys::Uint32Array {
let my_vec: Vec<u32> = vec![12; num_samples as usize];
return js_sys::Uint32Array::from(&my_vec[..]);
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论