mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
c95f57cafd
- Would be better to remove the iterator and just collect with a loop to avoid extra allocations - tructure - A HashSet is probably better - Usefull -> Useful - I'd have thought plugin-api-derive is a better name
39 lines
916 B
Rust
39 lines
916 B
Rust
#![feature(const_fn)]
|
|
|
|
|
|
pub extern crate plugin_api_derive;
|
|
pub extern crate common_api;
|
|
|
|
pub use common_api::*;
|
|
|
|
pub use plugin_api_derive::*;
|
|
|
|
use serde::de::DeserializeOwned;
|
|
use serde::Serialize;
|
|
|
|
extern "C" {
|
|
fn send_action(ptr: *const u8, len: usize);
|
|
}
|
|
|
|
pub fn send_actions(action: Vec<Action>) {
|
|
let ret = bincode::serialize(&action).unwrap();
|
|
unsafe {
|
|
send_action(ret.as_ptr(), ret.len());
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
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 _
|
|
} |