2个版本
0.1.2 | 2023年6月21日 |
---|---|
0.1.0 | 2023年6月19日 |
#184 in 数据库实现
6MB
123K SLoC
tugraph-plugin-util
是一个辅助crate,它可以将具有以下签名的任何Rust函数
fn (_: &mut Graph, _: &str) -> Result<String>
转换为插件入口点。
示例
use tugraph::{db::Graph, Result, txn::TxnRead};
use tugraph_plugin_util::tugraph_plugin;
#[tugraph_plugin]
fn user_process_func(graph: &mut Graph, request: &str) -> Result<String> {
// user process code
Ok("Process Result".to_string())
}
它导出一个属性proc-macro #[tugraph_plugin]
,它可以装饰一个Rust函数并使函数扩展为
use tugraph_plugin_util::lgraph_api_graph_db_t;
use tugraph_plugin_util::CxxString;
use tugraph_plugin_util::Graph as TuGraph;
#[no_mangle]
pub unsafe extern "C" fn Process(
graph_db: *mut lgraph_api_graph_db_t,
request: *const CxxString,
response: *mut CxxString) -> bool {
let mut graph = TuGraph::from_ptr(graph_db);
let request = if request.is_null() {
""
} else {
(*request).to_str().unwrap()
};
// user defined process function are nested here
fn user_process_func(graph: &mut Graph, request: &str) -> Result<String> {
// ...
}
let result = user_process_func(&mut graph, request);
::std::mem::forget(graph);
match result {
Ok(val) => {
if !response.is_null() {
let mut reponse = ::std::pin::Pin::new_unchecked(&mut *response);
reponse.as_mut().clear();
reponse.as_mut().push_str(val.as_str())
}
true
}
Err(e) => {
eprintln!("run rust plugin failed: {:?}", e);
false
}
}
}
依赖关系
~2.4–5MB
~95K SLoC