-
Notifications
You must be signed in to change notification settings - Fork 34
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
Issues with repr(C) trait #21
Comments
The issue is that a The most sensible thing to do then is to give it instead a fixed size layout, such as typedef int32_t MyEnum; enum {
MY_ENUM_FOO,
// etc.
}; (Or use what |
Ah, I guess using enums for return values across a C API is somewhat problematic if the compiler is free to choose the datatype? In my particular case, the enum is being used to communicate the status between an application, A that dynamically loads the library B (the part I am writing). So i guess that both A and B should use the same size for the enum? |
Yes.
B#[derive(...)]
#[repr(i32)]
pub
enum Status {
Ok = 0,
OhNo,
}
#[no_mangle] pub extern "C"
fn status () -> Status
{
// ...
}
AIf C binary#include <stdint.h>
typedef int32_t Status; enum {
STATUS_OK = 0,
STATUS_OH_NO,
};
// If at link / load time:
Status get_status (void);
// If manually loaded at runtime
// (_e.g._, hot reloading)
#include <dl.h>
void some_func (...)
{
...
Status (*get_status)(void) = dlsym(... "get_status" ...);
...
} If Rust binary#[derive(...)]
#[repr(i32)]
pub
enum Status {
Ok = 0,
OhNo,
}
fn some_func (...)
{
...
let get_status: extern "C" fn() -> Status = unsafe { ... };
...
} |
Thank you for taking the time to create an example, it really helped with the understanding. Fortunately, a new standard is being drafted defining the communication interface between A and B, so your feedback may prove useful in creating a more robust/portable interface. |
I am writing some code that needs to be callable from C. To my understanding enums exposed to the c code should have the trait #[repr(C)], but this seems to cause an issue with the module:
Produces the error:
Is there a workaround for this? I tried defining multiple types: #[repr(C,i32)] which appears to have been possible previously: rust-lang/rust#68585
Any help is much appreciated :)
The text was updated successfully, but these errors were encountered: