It would be cool if there was an option to tell compiler how to name the object in the end. A strength of C mangling (or rather lack of it) is that you can take the object file and know beforehand the names. With mangling that will always depend on the version of the compiler. In practice, while mangled names are kind of stable, mangled C++ names are not really reliable in the real world
If we could tell the compiler “in the end name/alias this function as prefix_or_name_of_cargo_my_super_duper_function”, we could then easily call it in an assembler/pure C/etc
It could even be a separate wrapper. only for exposing stable function names in the object files
I would recommend reading up some materials on FFI in Rust if you’re interested. Calling functions in Rust from C and in C from Rust (or even languages other than C) is both extremely common and a primary usecase of the language.
No, the attribute is unsafe. An unsafe function is marked unsafefn.
Also, unsafe by itself just means there are invariants that must me manually upheld to avoid unsound behavior. If those invariants are upheld, then it doesn’t matter if it’s unsafe.
It would be cool if there was an option to tell compiler how to name the object in the end. A strength of C mangling (or rather lack of it) is that you can take the object file and know beforehand the names. With mangling that will always depend on the version of the compiler. In practice, while mangled names are kind of stable, mangled C++ names are not really reliable in the real world
If we could tell the compiler “in the end name/alias this function as
prefix_or_name_of_cargo_my_super_duper_function”, we could then easily call it in an assembler/pure C/etcIt could even be a separate wrapper. only for exposing stable function names in the object files
I would recommend reading up some materials on FFI in Rust if you’re interested. Calling functions in Rust from C and in C from Rust (or even languages other than C) is both extremely common and a primary usecase of the language.
Wouldn’t that make the body of the function unsafe too?
Yeah, I’m slowly getting to that
No, the attribute is unsafe. An unsafe function is marked
unsafe fn.Also, unsafe by itself just means there are invariants that must me manually upheld to avoid unsound behavior. If those invariants are upheld, then it doesn’t matter if it’s unsafe.
https://doc.rust-lang.org/reference/abi.html#the-no_mangle-attribute https://doc.rust-lang.org/nomicon/other-reprs.html#reprc
Does
no_mangleandrepr(c)do that?Those are good to know, thanks. But as far as I understand, attribute enforces unsafe, repr is more about the data layout. Right?
But in there, I’ve found https://github.com/mozilla/cbindgen. Wicked