2020-12-09 20:47:42 +00:00
|
|
|
#![feature(const_fn)]
|
|
|
|
|
|
|
|
|
2020-12-12 00:19:20 +00:00
|
|
|
pub extern crate plugin_api_derive;
|
2020-12-11 23:37:22 +00:00
|
|
|
pub extern crate common_api;
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
pub use common_api::*;
|
|
|
|
|
2020-12-12 00:19:20 +00:00
|
|
|
pub use plugin_api_derive::*;
|
2020-12-11 23:37:22 +00:00
|
|
|
|
|
|
|
use serde::de::DeserializeOwned;
|
|
|
|
use serde::Serialize;
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
extern "C" {
|
|
|
|
fn send_action(ptr: *const u8, len: usize);
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
pub fn send_actions(action: Vec<Action>) {
|
|
|
|
let ret = bincode::serialize(&action).unwrap();
|
|
|
|
unsafe {
|
|
|
|
send_action(ret.as_ptr(), ret.len());
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
2020-12-11 23:37:22 +00:00
|
|
|
}
|
2020-12-09 20:47:42 +00:00
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
pub fn read_input<T>(ptr: i32, len: u32) -> Result<T, &'static str> where T: DeserializeOwned{
|
|
|
|
let slice = unsafe {
|
|
|
|
::std::slice::from_raw_parts(ptr as _, len as _)
|
|
|
|
};
|
|
|
|
bincode::deserialize(slice).map_err(|_|"Failed to deserialize function input")
|
2020-12-09 20:47:42 +00:00
|
|
|
}
|
|
|
|
|
2020-12-11 23:37:22 +00:00
|
|
|
pub fn write_output(value: impl Serialize) -> i32 {
|
|
|
|
let ret = bincode::serialize(&value).unwrap();
|
|
|
|
let len = ret.len() as u32;
|
|
|
|
unsafe {
|
|
|
|
::std::ptr::write(1 as _, len);
|
|
|
|
}
|
|
|
|
ret.as_ptr() as _
|
|
|
|
}
|