Skip to content

Commit

Permalink
Ignoring pointer of empty slices (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertbastian authored Nov 27, 2023
1 parent 62cb67c commit 04efb3c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
20 changes: 17 additions & 3 deletions macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,28 @@ fn gen_params_invocation(param: &ast::Param, expanded_params: &mut Vec<Expr>) {
let tokens = if let ast::TypeName::PrimitiveSlice(_, mutability, _) = &param.ty {
match mutability {
ast::Mutability::Mutable => quote! {
unsafe { core::slice::from_raw_parts_mut(#data_ident, #len_ident) }
if #len_ident == 0 {
&mut []
} else {
unsafe { core::slice::from_raw_parts_mut(#data_ident, #len_ident) }
}
},
ast::Mutability::Immutable => quote! {
unsafe { core::slice::from_raw_parts(#data_ident, #len_ident) }
if #len_ident == 0 {
&[]
} else {
unsafe { core::slice::from_raw_parts(#data_ident, #len_ident) }
}
},
}
} else {
quote! {unsafe { core::slice::from_raw_parts(#data_ident, #len_ident) } }
quote! {
if #len_ident == 0 {
&[]
} else {
unsafe { core::slice::from_raw_parts(#data_ident, #len_ident) }
}
}
};
expanded_params.push(parse2(tokens).unwrap());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ mod ffi {
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_fill_slice(s_diplomat_data: *mut f64, s_diplomat_len: usize) {
Foo::fill_slice(unsafe { core::slice::from_raw_parts_mut(s_diplomat_data, s_diplomat_len) })
Foo::fill_slice(if s_diplomat_len == 0 {
&mut []
} else {
unsafe { core::slice::from_raw_parts_mut(s_diplomat_data, s_diplomat_len) }
})
}
#[no_mangle]
extern "C" fn Foo_destroy(this: Box<Foo>) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ mod ffi {
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_from_slice(s_diplomat_data: *const f64, s_diplomat_len: usize) {
Foo::from_slice(unsafe { core::slice::from_raw_parts(s_diplomat_data, s_diplomat_len) })
Foo::from_slice(if s_diplomat_len == 0 {
&[]
} else {
unsafe { core::slice::from_raw_parts(s_diplomat_data, s_diplomat_len) }
})
}
#[no_mangle]
extern "C" fn Foo_destroy(this: Box<Foo>) {}
Expand Down
8 changes: 6 additions & 2 deletions macro/src/snapshots/diplomat__tests__method_taking_str.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: macro/src/lib.rs
expression: "rustfmt_code(&gen_bridge(parse_quote! {\n mod ffi\n {\n struct Foo {} impl Foo\n {\n pub fn from_str(s : & DiplomatStr) { unimplemented! () }\n }\n }\n }).to_token_stream().to_string())"
expression: "rustfmt_code(&gen_bridge(parse_quote! {\n mod ffi\n {\n struct Foo {} impl Foo\n { pub fn from_str(s : & DiplomatStr) { unimplemented! () } }\n }\n }).to_token_stream().to_string())"
---
mod ffi {
#[repr(C)]
Expand All @@ -13,7 +13,11 @@ mod ffi {
use diplomat_runtime::*;
#[no_mangle]
extern "C" fn Foo_from_str(s_diplomat_data: *const u8, s_diplomat_len: usize) {
Foo::from_str(unsafe { core::slice::from_raw_parts(s_diplomat_data, s_diplomat_len) })
Foo::from_str(if s_diplomat_len == 0 {
&[]
} else {
unsafe { core::slice::from_raw_parts(s_diplomat_data, s_diplomat_len) }
})
}
#[no_mangle]
extern "C" fn Foo_destroy(this: Box<Foo>) {}
Expand Down

0 comments on commit 04efb3c

Please sign in to comment.