我想为sn_api库编写FFI包装器,其中包含
我发现,简单的方法是在每个导出函数中使用
是否可能在一个共同的
async
函数。它将用于在Red中编写的单线程非异步代码中。我发现,简单的方法是在每个导出函数中使用
Runtime::new().unwrap().block_on(...)
,尽管这涉及创建许多新的Tokio运行时,似乎太重以至于无法在每次调用时运行:use std::os::raw::c_char;
use std::ffi::{CString, CStr};
use sn_api::{BootstrapConfig, Safe};
use tokio::runtime::Runtime;
#[no_mangle]
pub extern "C" _safe_connect(ptr: *const Safe, bootstrap_contact: *const c_char) {
assert!(!ptr.is_null());
let _safe = unsafe {
&*ptr
};
let bootstrap_contact = unsafe {
CStr::from_ptr(bootstrap_contact)
}
let mut bootstrap_contacts = BootstrapConfig::default();
bootstrap_contacts.insert(bootstrap_contact.parse().expect("Invalid bootstrap address"));
// how to reuse the Runtime in other functions?
Runtime::new().unwrap().block_on(_safe.connect(None, None, Some(bootstrap_contacts)));
}
是否可能在一个共同的
Runtime
上运行所有异步函数?我想这需要创建一些单例/全局变量,但我的库是使用crate-type = ["cdylib"]
编译的,这似乎不是全局变量的好地方。最好的方法是什么?