csbindgen icon indicating copy to clipboard operation
csbindgen copied to clipboard

Support unsafe attribute for Rust 2024 edition

Open mezum opened this issue 9 months ago • 1 comments

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

mezum avatar Mar 08 '25 11:03 mezum

+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 https://github.com/Cysharp/csbindgen/blob/768761c834941ffd26d8c5c44f5d8403d485dad1/csbindgen/src/parser.rs#L126

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

zhangr4 avatar Mar 08 '25 11:03 zhangr4

Happened to have the same issue when migrated to Rust edition 2024.

Odonno avatar Jul 05 '25 11:07 Odonno

#103 has been released in v1.9.4.

neuecc avatar Aug 18 '25 08:08 neuecc