如何使用Ctypes获取从Rust到Python的自定义对象列表?

如何使用Ctypes获取从Rust到Python的自定义对象列表?,python,rust,ctypes,ffi,Python,Rust,Ctypes,Ffi,我试图通过函数调用将一组自定义对象从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::Resul

我试图通过函数调用将一组自定义对象从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代码时,我得到一个malloc异常:

malloc: *** mach_vm_map(size=140734736883712) failed (error code=3)

可能缺少一些资料。

&str
结果
Vec
不是ffi安全类型。您可能希望签出。虽然我目前没有返回项目集合,但在此之前,您需要做很多更简单的事情。看起来非常有用,谢谢!