Skip to content

Commit

Permalink
add support for Vec<u8> and Box<[u8]>
Browse files Browse the repository at this point in the history
  • Loading branch information
usrtax committed Sep 29, 2022
1 parent 533c549 commit 6cd7107
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 119 deletions.
10 changes: 9 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
edition = "2021"
max_width = 80
tab_spaces = 2
edition = "2018"
unstable_features = true
condense_wildcard_suffixes = true
newline_style = "Unix"
use_field_init_shorthand = true
use_try_shorthand = true
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
reorder_imports = true
2 changes: 1 addition & 1 deletion deno_bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["deno", "ffi", "bindgen", "bindings", "macro"]
categories = ["development-tools::ffi", "development-tools"]
readme = "../README.md"
license = "MIT"
edition = "2018"
edition = "2021"

[lib]
path = "./lib.rs"
Expand Down
2 changes: 1 addition & 1 deletion deno_bindgen_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ keywords = ["deno", "ffi", "bindgen", "bindings", "macro"]
categories = ["development-tools::ffi", "development-tools"]
readme = "../README.md"
license = "MIT"
edition = "2018"
edition = "2021"

[lib]
proc-macro = true
Expand Down
46 changes: 45 additions & 1 deletion deno_bindgen_macro/src/derive_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use syn::FnArg;
use syn::ItemFn;
use syn::Meta;
use syn::NestedMeta;
use syn::PathArguments;
use syn::ReturnType;

pub fn process_function(
Expand All @@ -26,7 +27,6 @@ pub fn process_function(
syn::Type::Path(ref ty) => {
let segment = ty.path.segments.first().unwrap();
let ident = segment.ident.to_string();

match ident.as_str() {
"i8" => Type::I8,
"u8" => Type::U8,
Expand Down Expand Up @@ -113,6 +113,50 @@ pub fn process_function(
// This isn't a &str but i really but
// don't want to add another type for just owned strings.
"String" => Type::Str,
"Box" => {
let mut buf = false;
let mut gn = String::default();
if let PathArguments::AngleBracketed(args) = &segment.arguments {
if let Some(syn::GenericArgument::Type(syn::Type::Slice(args))) =
&args.args.first()
{
if let syn::Type::Path(args) = &*args.elem {
if let Some(args) = args.path.segments.first() {
gn = args.ident.to_string();
if gn == "u8" {
buf = true;
}
}
}
}
}
if buf {
Type::Buffer
} else {
panic!("{}<{}> return type not supported by Deno FFI", ident, gn)
}
}
"Vec" => {
let mut buf = false;
let mut gn = String::default();
if let PathArguments::AngleBracketed(args) = &segment.arguments {
if let Some(syn::GenericArgument::Type(syn::Type::Path(args))) =
&args.args.first()
{
if let Some(args) = &args.path.segments.first() {
gn = args.ident.to_string();
if gn == "u8" {
buf = true;
}
}
}
}
if buf {
Type::Buffer
} else {
panic!("{}<{}> return type not supported by Deno FFI", ident, gn)
}
}
_ => match metadata.type_defs.get(&ident) {
Some(_) => Type::StructEnum { ident },
None => panic!("{} return type not supported by Deno FFI", ident),
Expand Down
2 changes: 1 addition & 1 deletion deno_bindgen_macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub fn deno_bindgen(attr: TokenStream, input: TokenStream) -> TokenStream {
let transformer = quote! {
let length = (result.len() as u32).to_be_bytes();
let mut v = length.to_vec();
v.extend_from_slice(#slice);
v.extend_from_slice(&#slice);

::std::mem::forget(result);
let result = v.as_ptr();
Expand Down
Loading

0 comments on commit 6cd7107

Please sign in to comment.