Skip to content

Commit

Permalink
Wrap __library.get calls for variables if wrap_unsafe_ops
Browse files Browse the repository at this point in the history
  • Loading branch information
WardBrian committed Oct 21, 2024
1 parent 51ee9c3 commit 2fa7b49
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions bindgen/codegen/dyngen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ impl DynamicItems {
ident: Ident,
ty: TokenStream,
is_required: bool,
wrap_unsafe_ops: bool,
) {
let member = if is_required {
quote! { *mut #ty }
Expand All @@ -231,15 +232,20 @@ impl DynamicItems {
});

let ident_str = codegen::helpers::ast_ty::cstr_expr(ident.to_string());
self.constructor_inits.push(if is_required {
quote! {
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym)?;
}

let library_get = if wrap_unsafe_ops {
quote!(unsafe { __library.get::<*mut #ty>(#ident_str) })
} else {
quote! {
let #ident = __library.get::<*mut #ty>(#ident_str).map(|sym| *sym);
}
});
quote!(__library.get::<*mut #ty>(#ident_str))
};

let qmark = if is_required { quote!(?) } else { quote!() };

let var_get = quote! {
let #ident = #library_get.map(|sym| *sym)#qmark;
};

self.constructor_inits.push(var_get);

self.init_fields.push(quote! {
#ident
Expand Down
1 change: 1 addition & 0 deletions bindgen/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,7 @@ impl CodeGenerator for Var {
.to_rust_ty_or_opaque(ctx, &())
.into_token_stream(),
ctx.options().dynamic_link_require_all,
ctx.options().wrap_unsafe_ops,
);
} else {
result.push(tokens);
Expand Down

0 comments on commit 2fa7b49

Please sign in to comment.