mirror of
https://github.com/zed-industries/zed.git
synced 2025-01-23 10:28:21 +00:00
Update plugin dependencies
This commit is contained in:
parent
0cf64d6fba
commit
35b2eff29c
4 changed files with 30 additions and 33 deletions
24
Cargo.lock
generated
24
Cargo.lock
generated
|
@ -3614,10 +3614,21 @@ name = "plugin"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"rust_plugin_macros",
|
||||
"plugin_macros",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "plugin_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.16.8"
|
||||
|
@ -4320,17 +4331,6 @@ dependencies = [
|
|||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_plugin_macros"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"serde",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.21"
|
||||
|
|
|
@ -6,4 +6,4 @@ edition = "2021"
|
|||
[dependencies]
|
||||
serde = "1.0"
|
||||
bincode = "1.3"
|
||||
rust_plugin_macros = { path = "../plugin_macros" }
|
||||
plugin_macros = { path = "../plugin_macros" }
|
|
@ -1,7 +1,7 @@
|
|||
#[repr(C)]
|
||||
pub struct Buffer {
|
||||
ptr: *const u8,
|
||||
len: usize,
|
||||
pub struct __Buffer {
|
||||
pub ptr: *const u8,
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
/// Allocates a buffer with an exact size.
|
||||
|
@ -9,7 +9,7 @@ pub struct Buffer {
|
|||
#[no_mangle]
|
||||
pub extern "C" fn __alloc_buffer(len: usize) -> *const u8 {
|
||||
let vec = vec![0; len];
|
||||
let buffer = unsafe { Buffer::from_vec(vec) };
|
||||
let buffer = unsafe { __Buffer::from_vec(vec) };
|
||||
return buffer.ptr;
|
||||
}
|
||||
|
||||
|
@ -21,31 +21,30 @@ pub extern "C" fn __alloc_buffer(len: usize) -> *const u8 {
|
|||
// std::mem::drop(vec);
|
||||
// }
|
||||
|
||||
impl Buffer {
|
||||
impl __Buffer {
|
||||
#[inline(always)]
|
||||
pub unsafe fn to_vec(&self) -> Vec<u8> {
|
||||
slice::from_raw_parts(self.ptr, self.len).to_vec()
|
||||
core::slice::from_raw_parts(self.ptr, self.len).to_vec()
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub unsafe fn from_vec(mut vec: Vec<u8>) -> Buffer {
|
||||
pub unsafe fn from_vec(mut vec: Vec<u8>) -> __Buffer {
|
||||
vec.shrink_to(0);
|
||||
let ptr = vec.as_ptr();
|
||||
let len = vec.len();
|
||||
std::mem::forget(vec);
|
||||
Buffer { ptr, len }
|
||||
__Buffer { ptr, len }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn leak_to_heap(self) -> *const Buffer {
|
||||
pub fn leak_to_heap(self) -> *const __Buffer {
|
||||
let boxed = Box::new(self);
|
||||
let ptr = Box::<Buffer>::into_raw(boxed) as *const Buffer;
|
||||
let ptr = Box::<__Buffer>::into_raw(boxed) as *const __Buffer;
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::{Buffer, __alloc_buffer};
|
||||
#[macro_use]
|
||||
pub use super::{__Buffer, __alloc_buffer};
|
||||
pub use plugin_macros::bind;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ use core::panic;
|
|||
|
||||
use proc_macro::TokenStream;
|
||||
use quote::{format_ident, quote};
|
||||
use syn::{parse_macro_input, ItemFn, VisPublic, Visibility};
|
||||
use syn::{parse_macro_input, ItemFn, Visibility};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
||||
|
@ -20,25 +20,23 @@ pub fn bind(args: TokenStream, function: TokenStream) -> TokenStream {
|
|||
let outer_fn_name = format_ident!("__{}", inner_fn_name);
|
||||
|
||||
TokenStream::from(quote! {
|
||||
use serde;
|
||||
|
||||
#[no_mangle]
|
||||
#inner_fn
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const Buffer {
|
||||
pub extern "C" fn #outer_fn_name(ptr: *const u8, len: usize) -> *const ::plugin::__Buffer {
|
||||
// setup
|
||||
let buffer = Buffer { ptr, len };
|
||||
let buffer = ::plugin::__Buffer { ptr, len };
|
||||
let data = unsafe { buffer.to_vec() };
|
||||
|
||||
// operation
|
||||
let argument = bincode::deserialize(&data).unwrap();
|
||||
let argument = ::bincode::deserialize(&data).unwrap();
|
||||
let result = #inner_fn_name(argument);
|
||||
let new_data: Result<Vec<u8>, _> = bincode::serialize(&result);
|
||||
let new_data: Result<Vec<u8>, _> = ::bincode::serialize(&result);
|
||||
let new_data = new_data.unwrap();
|
||||
|
||||
// teardown
|
||||
let new_buffer = unsafe { Buffer::from_vec(new_data) };
|
||||
let new_buffer = unsafe { ::plugin::__Buffer::from_vec(new_data) };
|
||||
return new_buffer.leak_to_heap();
|
||||
}
|
||||
})
|
||||
|
|
Loading…
Reference in a new issue