diff --git a/macro/src/lib.rs b/macro/src/lib.rs index 7c42ad00a..d2ddb3d58 100644 --- a/macro/src/lib.rs +++ b/macro/src/lib.rs @@ -102,14 +102,28 @@ fn gen_params_invocation(param: &ast::Param, expanded_params: &mut Vec) { let tokens = if let ast::TypeName::PrimitiveSlice(_, mutability, _) = ¶m.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()); } diff --git a/macro/src/snapshots/diplomat__tests__method_taking_mutable_slice.snap b/macro/src/snapshots/diplomat__tests__method_taking_mutable_slice.snap index 341f4b9b9..839a44824 100644 --- a/macro/src/snapshots/diplomat__tests__method_taking_mutable_slice.snap +++ b/macro/src/snapshots/diplomat__tests__method_taking_mutable_slice.snap @@ -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) {} diff --git a/macro/src/snapshots/diplomat__tests__method_taking_slice.snap b/macro/src/snapshots/diplomat__tests__method_taking_slice.snap index f88b027e4..9b1e7cce4 100644 --- a/macro/src/snapshots/diplomat__tests__method_taking_slice.snap +++ b/macro/src/snapshots/diplomat__tests__method_taking_slice.snap @@ -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) {} diff --git a/macro/src/snapshots/diplomat__tests__method_taking_str.snap b/macro/src/snapshots/diplomat__tests__method_taking_str.snap index b3e4b3043..01666e41f 100644 --- a/macro/src/snapshots/diplomat__tests__method_taking_str.snap +++ b/macro/src/snapshots/diplomat__tests__method_taking_str.snap @@ -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)] @@ -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) {}