Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unsafe attribute for Rust 2024 edition #102

Open
mezum opened this issue Mar 8, 2025 · 1 comment
Open

Support unsafe attribute for Rust 2024 edition #102

mezum opened this issue Mar 8, 2025 · 1 comment

Comments

@mezum
Copy link

mezum commented Mar 8, 2025

Summary

csbindgen connot generate C# Native method declarations from Rust 2024 edition sources
because some attributes (such as no_mangle and export_name) must now be marked as unsafe
using new introduced attribute unsafe but csbindgen cannot recognize it.

Workaround

Use 2021 edition and no_mangle and export_name without unsafe attribute.

Details

From Rust 1.82, some attributes has ability in all editions to mark as unsafe like this:

#[unsafe(no_mangle)]
pub unsafe extern "C" fn hoge_init() {}

#[allow(non_snake_case)]
#[unsafe(export_name = "hoge_deinit")]
pub unsafe extern "C" fn Destroy() {}

Last February 21, Rust 1.85 stable has been released with 2024 edition!
But in 2024 edition, these attributes must be marked as unsafe.
csbindgen cannot recognize unsafe attribute, so generated codes contains no DllImported native methods.

Reproduce

  1. Extract the attached tarball.
  2. Run cargo build.
  3. Open ./target/csbindgen/NativeMethods.g.cs but It contains no DllImported methods.

csbindgen-with-rust2024.tar.gz

@zhangr4
Copy link

zhangr4 commented Mar 8, 2025

+1

I also encountered this issue and was going to raise the same issue. I suggest change to if name == "no_mangle || name == "unsafe(no_mange)" in

if name == "no_mangle" {

I am noob in Rust, just correct me if if I make any mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants