I understand that I can give a function a static linker symbol name like this:
// The linker symbol produced for this function will be named
// "foo_bar", not "asdf" or some mangled version of "asdf".
#[unsafe(export_name = "foo_bar")]
extern "C" fn asdf() {
// ...
}
Is there a way to provide a dynamically generated symbol name? For example:
trait MyTrait {
const NAME: &'static str;
// ...
}
struct MyStruct<T: MyTrait> {
data: T
}
impl<T: MyTrait> MyStruct<T> {
#[unsafe(export_name = "my_prefix_new_" + T::NAME)]
extern "C" c_new() -> *mut MyStruct<T> {
// ...
}
}
I want to do this to make it easier to write Rust plugins (cdylib) for an existing C-based plugin system.
Perhaps there's a way to collect the desired names when building and use them to construct a linker script that Cargo passes to the linker?
export_nameis aMetaNameValueStr. So there's no hope of doing something likeexport_name = "my_prefix_new_" + T::NAME, which makes it non-trivial (read: proc-macro fun).T::NAMEwhich is aconst &'static strand potentially lives in a completely different part of the code.T::NAMEI concur. But I assumed that he probably has some flexibility on how exactly to implement this. From the limited info I've got, I don't see why he can't just use the name of the struct (which IS available to the derive macro), potentially with an optional attribute argument to override the generated name. Although of course without knowing OP's exact objectives and constraints I can only give tentative suggestions.