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

Audit Rust enums used in FFI #322

Open
luqmana opened this issue Jan 24, 2023 · 1 comment
Open

Audit Rust enums used in FFI #322

luqmana opened this issue Jan 24, 2023 · 1 comment

Comments

@luqmana
Copy link
Contributor

luqmana commented Jan 24, 2023

We have a lot of enum types that are passed/returned via FFI (whether it be us calling foreign functions or exposing functions called from non-Rust code) but that comes with a subtle footgun. See rust-lang/rust#36927. We need to go over the code to make sure we're not tripping up over this.

@luqmana
Copy link
Contributor Author

luqmana commented Jan 24, 2023

I think the most ergonomic one for most simple cases would be replacing the enum with a newtype + consts:

#[repr(C)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub enum ddi_info_cmd_t {
    DDI_INFO_DEVT2DEVINFO = 0,
    DDI_INFO_DEVT2INSTANCE = 1,
}

becomes this instead:

#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct  ddi_info_cmd_t(pub c_int);
impl ddi_info_cmd_t {
    pub const DDI_INFO_DEVT2DEVINFO: Self = Self(0);
    pub const DDI_INFO_DEVT2INSTANCE: Self = Self(1);
}

We get the namespaced variantsm some exhaustiveness checks and but can still cope with unknown variants.

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

1 participant