lua使用rust代码(第三期)--Vec<struct>

[toc]

前言

在C中,string类型可以看作char的数组。保存的是数组第一个元素的地址。那么对于真正的数组,也可以如lua使用rust代码--第二期 中处理字符串一样,处理对象数组。

示例

lib.rs

use std::ffi::CString;
use std::os::raw::{c_char, c_int};

//rust struct
#[derive(Deserialize,Debug)]
struct HostInfo {
	ip:String,
	port:u32,
}

#[derive(Debug)]
#[repr(C)]
pub struct CHostInfo {
    ip: *const c_char,
    ip_len: c_int,
    port: c_int,
}

fn get_info() -> Result<Vec<HostInfo>, reqwest::Error>{
//get data through api by reqwest
	let res: Vec<HostInfo> = reqwest::Client::new()
			.get("http://example.com/api/host/")
			.send()?
			.json()?;
		Ok(res)
}

#[no_mangle]
pub extern "C" fn get_all_data(len: *mut u32) -> *mut CHostInfo{
	let res = get_info().expect("http request fail");
	let mut data:Vec<CHostInfo> = res.into_iter()
		.map(|info| {
			let ip_cs = CString::new(info.ip.as_str()).unwrap();
			let ip = ip_cs.as_ptr();
			let ip_len = info.ip.len() as c_int;
			std::mem::forget(ip_cs);
			CHostInfo {
				ip: ip,
				ip_len:ip_len,
				port: info.port as c_int,
			}
		})
		.collect();
	let ptr = data.as_mut_ptr();
	unsafe {
		*len = data.len() as u32;
	}
	std::mem::forget(data);
	ptr
}

lua

ffi = require("ffi")
ffi.cdef[[
	typedef struct {
		char * ip;
		int    ip_len;
		int    port;
	} hostinfo_t;
	hostinfo_t * get_all_data(int*);
]]
rust_lib = ffi.load("./libexample.so")
function get_all_data()
	local intPtr = ffi.typeof("int[1]")
	local len = intPtr()
	local data = rust_lib.get_all_data(len)
	local res ={}
	for i=0,len[0]-1 do
		res[i] = {}
		res[i]["ip"] = ffi.string(data[i].ip,data[i].ip_len)
		res[i]["port"] = data[i].port
	end
	return res
end
03-31 00:51