\n\n\n由于一些建议而更新
\n
系统\xef\xbc\x9amacOS 10.14.6
\n\n这里我想问的问题是如何使用rust调用编译好的.so文件,抱歉,我对这部分是新手。
\n\n我有一个非常简单的 c 文件:
\n\n#include "add.h"\n\nint add(int a, int b) {\n return a + b;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n然后我习惯gcc-fPIC -shared -o libadd.so add.c
编译成.so文件放到lib目录下
然后我在 rust 的 build.rs 文件中写入了以下内容:
\n\n\nuse std::env;\nuse std::path::{Path};\n\nfn main() {\n let pwd_dir = env::var("CARGO_MANIFEST_DIR").unwrap();\n let path = Path::new(&*pwd_dir).join("lib");\n println!("cargo:rustc-link-search=native={}", path.to_str().unwrap());\n println!("cargo:rustc-link-lib=dylib=add");\n // println!("cargo:rustc-link-lib=static=add");\n // println!("cargo:rerun-if-changed=src/hello.c");\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n我希望我可以得到并使用这个函数,main.rs 是:
\n\nextern { fn add(a: i32, b: i32) -> i32; }\n\nfn main() {\n let c = unsafe …
Run Code Online (Sandbox Code Playgroud) 我尝试使用panic::catch_unwind
捕获一些错误,但似乎没有用,并且有一个例子:
锈:
use std::{sync::Mutex};
use wasm_bindgen::prelude::*;
use std::sync::PoisonError;
use std::panic;
pub struct CurrentStatus {
pub index: i32,
}
#[wasm_bindgen]
extern {
pub fn alert(s: &str);
}
impl CurrentStatus {
fn new() -> Self {
CurrentStatus {
index: 1,
}
}
fn get_index(&mut self) -> i32 {
self.index += 1;
self.index.clone()
}
fn add_index(&mut self) {
self.index += 2;
}
}
lazy_static! {
pub static ref FOO: Mutex<CurrentStatus> = Mutex::new(CurrentStatus::new());
}
unsafe impl Send for CurrentStatus {}
#[wasm_bindgen]
pub …
Run Code Online (Sandbox Code Playgroud) 我想在OS X 10.12中运行来自facebook/react-native/Examples的示例.
首先我使用npm install
然后使用npm start
:
有一个错误:
Error watching file for changes: EMFILE
{"code":"EMFILE","errno":"EMFILE","syscall":"Error watching file for changes:","filename":null}
Error: Error watching file for changes: EMFILE
at exports._errnoException (util.js:1008:11)
at FSEvent.FSWatcher._handle.onchange (fs.js:1406:11)
Run Code Online (Sandbox Code Playgroud)
然后我使用Xcode运行代码,但有相同的错误.
我需要帮助或建议.非常感谢.
我目前正在尝试使用Rust编译为wasm部分(有类似的框架,例如yew等)来开发Web应用程序的一部分,但是我发现使用webassembly可能会消耗更多,例如,我必须单击按钮调用JS函数。JS函数执行一些计算吗?非常简单的计算吗?并将结果呈现给dom
仅使用js的解决方案:
使用Rust + Webassembly的解决方案:
这里我们不考虑使用React或Vue,仅使用WebAssembly可能会降低性能,这主要体现在:
Webassembly的优点可能是计算速度更快,但是由于上面提到的开销增加,该优点很可能不会节省总时间,并且当涉及DOM操作时,它显然很慢。
但是我仍然对DOM操作进行了比较测试:
JS创建10,000个p-tag,耗时约120ms:
function web_bench() {
let container = document.getElementById("container");
let begin = Date.now();
for(let i = 0; i < 10000; i += 1) {
let str = "1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
"123456789012345678901234567890123456abcd123456789012345678901234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789012345678??1234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789??2345678901234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890" +
"123456789012345678901234567890123456789012345678901234567890123??67890123456789012345678901234567890" +
"12345678901234567890123456789012345678901234???012345678901234567890123456789012345678901234567890";
let p = document.createElement("p");
p.innerHTML = str;
container.appendChild(p);
}
let time = Date.now() - begin;
console.log('cost time:', time);
}
Run Code Online (Sandbox Code Playgroud)
如果我使用生锈:花费180毫秒 …
我试图运行官方文档中的FnBox
示例,但它抛出了一个错误:
error[E0432]: unresolved import `std::boxed::FnBox`
--> src/main.rs:4:5
|
4 | use std::boxed::FnBox;
| ^^^^^^^^^^^^^^^^^ no `FnBox` in `boxed`
Run Code Online (Sandbox Code Playgroud)
使用 rust playground很容易得到这个错误,我在本地也得到了同样的错误。
实际上我在本地找到了一些来自 std 的声明:
#[rustc_paren_sugar]
#[unstable(feature = "fnbox",
reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue = "28796")]
pub trait FnBox<A>: FnOnce<A> {
/// Performs the call operation.
fn call_box(self: Box<Self>, args: A) -> Self::Output;
}
#[unstable(feature = "fnbox",
reason = "will be deprecated if and when `Box<FnOnce>` becomes usable", issue …
Run Code Online (Sandbox Code Playgroud) 我在使用 Rust 和 WebAssembly 进行开发时遇到了僵局。
由于使用了一些全局访问的变量,我选择了lazy_static
一个Mutex(使用thread_local
回调会导致嵌套问题)。我已经声明 JavaScript 通过#[wasm_bindgen]
. 他们读取和写入lazy_static
变量。
其中一个函数发生恐慌后,互斥锁将无法释放,从而导致其他函数在需要使用相同互斥量时发生恐慌。
我知道恐慌问题是意外的并且需要修复,但是这些功能彼此相对独立。虽然变量的读写lazy_static
有交叉,但某个bug不一定会影响其他部分。
如何在 Wasm 发生恐慌后触发释放Mutex
以允许其他调用正常?对于此类问题有没有更好的做法呢?
锈:
use std::sync::Mutex;
use std::sync::PoisonError;
use wasm_bindgen::prelude::*;
pub struct CurrentStatus {
pub index: i32,
}
impl CurrentStatus {
fn new() -> Self {
CurrentStatus { index: 1 }
}
fn get_index(&mut self) -> i32 {
self.index += 1;
self.index.clone()
}
fn add_index(&mut self) {
self.index += 2;
}
}
lazy_static! {
pub static ref …
Run Code Online (Sandbox Code Playgroud)