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

我试图通过函数调用将一个自定义对象数组从Rust发送到Python:

pub struct Item {

name: String,

description: String,

tags: Vec

}

pub struct SearchResults {

count: usize,

results: Vec

}

fn get_content(url: &str) -> hyper::Result {

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 = 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)

可能在那里遗漏了一堆东西 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值