1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
pub use calcite_proc_macros::*;
pub use futures;
pub use deno_core;
pub trait FromZeroCopyBuf<'a> {
fn from_zero_copy_buf(buff: &'a deno_core::plugin_api::ZeroCopyBuf) -> Self;
}
pub struct ArrayBuffer<'a, T>(&'a mut [T]);
impl<'a, T> ArrayBuffer<'a, T> {
pub fn as_mut_slice(&mut self) -> &mut [T] {
self.0
}
pub fn as_slice(&self) -> &[T] {
self.0
}
}
impl<'a, T> FromZeroCopyBuf<'a> for ArrayBuffer<'a, T> {
fn from_zero_copy_buf(buff: &deno_core::plugin_api::ZeroCopyBuf) -> Self {
unsafe {
Self(std::slice::from_raw_parts_mut(
buff[..].as_ptr() as *mut T,
buff.len() / std::mem::size_of::<T>(),
))
}
}
}
impl<'a, T: serde::Deserialize<'a>> FromZeroCopyBuf<'a> for T {
fn from_zero_copy_buf(buff: &'a deno_core::plugin_api::ZeroCopyBuf) -> Self {
serde_json::from_slice(buff).expect("Wrong argument type")
}
}
pub fn to_argument_type<'a, T: FromZeroCopyBuf<'a>>(buff: &'a deno_core::plugin_api::ZeroCopyBuf) -> T {
T::from_zero_copy_buf(buff)
}
#[macro_use]
extern crate serde_derive;
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AsyncResult<T, E> {
pub command_id: usize,
pub result: Result<T, E>,
}
pub struct ReturnBuffer(Box<[u8]>);
impl<T: serde::Serialize> From<T> for ReturnBuffer {
fn from(t: T) -> Self {
Self(serde_json::to_vec(&t).unwrap().into_boxed_slice())
}
}
impl ReturnBuffer {
pub fn inner(self) -> Box<[u8]> {
self.0
}
pub fn from_bytes(bytes: Vec<u8>) -> Self {
Self(bytes.into_boxed_slice())
}
}