Skip to content

Commit 492a942

Browse files
WardBrianpvdrz
authored andcommitted
Wrap __library.get calls for variables if wrap_unsafe_ops
1 parent cd5b843 commit 492a942

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bindgen/codegen/dyngen.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ impl DynamicItems {
208208
ident: Ident,
209209
ty: TokenStream,
210210
is_required: bool,
211+
wrap_unsafe_ops: bool,
211212
) {
212213
let member = if is_required {
213214
quote! { *mut #ty }
@@ -231,15 +232,20 @@ impl DynamicItems {
231232
});
232233

233234
let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string());
234-
self.constructor_inits.push(if is_required {
235-
quote! {
236-
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym)?;
237-
}
235+
236+
let library_get = if wrap_unsafe_ops {
237+
quote!(unsafe { __library.get::<*mut #ty>(#ident_str) })
238238
} else {
239-
quote! {
240-
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym);
241-
}
242-
});
239+
quote!(__library.get::<*mut #ty>(#ident_str))
240+
};
241+
242+
let qmark = if is_required { quote!(?) } else { quote!() };
243+
244+
let var_get = quote! {
245+
let #ident = #library_get.map(|sym| *sym)#qmark;
246+
};
247+
248+
self.constructor_inits.push(var_get);
243249

244250
self.init_fields.push(quote! {
245251
#ident

bindgen/codegen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,7 @@ impl CodeGenerator for Var {
808808
.to_rust_ty_or_opaque(ctx, &())
809809
.into_token_stream(),
810810
ctx.options().dynamic_link_require_all,
811+
ctx.options().wrap_unsafe_ops,
811812
);
812813
} else {
813814
result.push(tokens);

0 commit comments

Comments
 (0)