我试图通过函数调用将一个自定义对象数组从Rust发送到Python:
pub struct Item {
name: String,
description: String,
tags: Vec<String>
}
pub struct SearchResults {
count: usize,
results: Vec<Item>
}
fn get_content(url: &str) -> hyper::Result<String> {
let client = Client::new();
let mut response = try!(client.get(url).send());
let mut buf = String::new();
try!(response.read_to_string(&mut buf));
Ok(buf)
}
#[no_mangle]
pub unsafe extern fn get_search_results(search: &str) -> SearchResults {
let mut url = String::from("http://localhost:8080/search?q=");
url.push_str(&search);
let content = get_content(&url).unwrap();
let j: Vec<SearchResult> = json::decode(&content).unwrap();
return SearchResults {count: j.len(), results: j};
}
我的Python代码:
from ctypes import cdll, Structure, c_wchar_p, c_int, POINTER
class SearchResult(Structure):
_fields_ = [("name", c_wchar_p), ("description", c_wchar_p), ("tags", POINTER(c_wchar_p))]
class SearchResults(Structure):
_fields_ = [("count", c_int), ("results", POINTER(SearchResult))]
lib = cdll.LoadLibrary("target/release/libplugin_core.dylib")
get_search_results = lib.get_search_results
get_search_results.restype = SearchResults
print(get_search_results("test"))
当我运行Python代码时,我得到一个malloc异常:
malloc: *** mach_vm_map(size=140734736883712) failed (error code=3)
可能在那里遗漏了一堆东西 .