RainwayApp / node-clangffi

Generate Typescript FFI bindings for C/C++ libraries using libclang and ffi-napi.
MIT License
7 stars 1 forks source link

Support enums of different sizes #47

Open mattiekat opened 2 years ago

mattiekat commented 2 years ago
#[repr(u8)]
#[derive(Debug, Display, Copy, Clone, Eq, PartialEq)]
pub enum RainwayChannelMode {
    Unreliable = 0,
    Reliable = 1,
}

Take this perfectly innocuous looking enum in Rust. It generates this wonderfully correct C/C++ header definition via cbindgen:

enum RainwayChannelMode
#ifdef __cplusplus
  : uint8_t
#endif // __cplusplus
 {
  Unreliable = 0,
  Reliable = 1,
};
#ifndef __cplusplus
typedef uint8_t RainwayChannelMode;
#endif // __cplusplus

But then node-clangffi seems to think it is a bit confusing because it ends up generating

export enum RainwayChannelMode {
  Unreliable = 0,
  Reliable = 1,
}
export type RainwayChannelMode = number;
// ...
export const RainwayChannelModeDef = ref.types.int;
export const RainwayChannelModeDef = ref.types.uint8;

Basically we need the uint8_t typedef to override the size, but to only keep the enum definition.

andrewmd5 commented 2 years ago

Now that Bebop supports varying enum sizes this might be a bigger priority