cbindgen icon indicating copy to clipboard operation
cbindgen copied to clipboard

Constant structure with array not generated

Open jxy-s opened this issue 2 years ago • 0 comments

A public constant structure with an array is not generated. Apologies if this has previously been reported or is documented as not in scope. I searched for a while without an answer.

As an example:

#[repr(C)]
pub struct Uuid {
    pub data1: u32,
    pub data2: u16,
    pub data3: u16,
    pub data4: [u8; 8],
}

pub const UUID_ZERO: Uuid = Uuid {
    data1: 0,
    data2: 0,
    data3: 0,
    data4: [0, 0, 0, 0, 0, 0, 0, 0],
};

#[no_mangle]
pub unsafe extern "C" fn check_uuid(uuid: *const Uuid) -> bool {
    false
}

Does not generate the public constant:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Uuid {
  uint32_t data1;
  uint16_t data2;
  uint16_t data3;
  uint8_t data4[8];
} Uuid;

bool check_uuid(const struct Uuid *uuid);
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Uuid {
  uint32_t data1;
  uint16_t data2;
  uint16_t data3;
  uint8_t data4[8];
};

extern "C" {

bool check_uuid(const Uuid *uuid);

} // extern "C"

However, if the array is commented out:

pub struct Uuid {
    pub data1: u32,
    pub data2: u16,
    pub data3: u16,
    //pub data4: [u8; 8],
}

pub const UUID_ZERO: Uuid = Uuid {
    data1: 0,
    data2: 0,
    data3: 0,
    //data4: [0, 0, 0, 0, 0, 0, 0, 0],
};

Then the constant is generated:

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

typedef struct Uuid {
  uint32_t data1;
  uint16_t data2;
  uint16_t data3;
} Uuid;

#define UUID_ZERO (Uuid){ .data1 = 0, .data2 = 0, .data3 = 0 }

bool check_uuid(const struct Uuid *uuid);
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

struct Uuid {
  uint32_t data1;
  uint16_t data2;
  uint16_t data3;
};

constexpr static const Uuid UUID_ZERO = Uuid{ /* .data1 = */ 0, /* .data2 = */ 0, /* .data3 = */ 0 };

extern "C" {

bool check_uuid(const Uuid *uuid);

} // extern "C"

I believe that, in this case, the array initialization should be generated by cbindgen:

#define UUID_ZERO (Uuid){ .data1 = 0, .data2 = 0, .data3 = 0, .data4 = { 0, 0, 0, 0, 0, 0, 0, 0 } }
constexpr static const Uuid UUID_ZERO = Uuid{ /* .data1 = */ 0, /* .data2 = */ 0, /* .data3 = */ 0,  /* .data4 = */ { 0, 0, 0, 0, 0, 0, 0, 0 } };

jxy-s avatar May 31 '23 23:05 jxy-s