Skip to content

Cannot generate corresponding C/C++ structure with #![feature(generic_const_exprs)] #1157

@astrojhgu

Description

@astrojhgu

The minimum test sample of lib.rs:

#![allow(incomplete_features)]
#![feature(generic_const_exprs)]

pub const N_BYTE_PER_FRAME: usize = 8192;

#[repr(C)]
pub struct Payload<T>
where
    [(); N_BYTE_PER_FRAME / std::mem::size_of::<T>()]: Sized,
    T: Sized + Default + 'static,
{
    pub data: [T; N_BYTE_PER_FRAME / std::mem::size_of::<T>()],    //Code 1
    //pub data: T,   //Code 2
}


#[unsafe(no_mangle)]
pub extern "C" fn use_payload_ci16(_p: Payload<i16>){
}

Note the above two lines with comments Code 1 and Code 2

When uncomment Code 1 (pub data: [T; N_BYTE_PER_FRAME / std::mem::size_of::<T>()]), cbindgen only generate a blank wrapper of Payload:

template<typename T = void>
struct Payload;

extern "C" {

void use_payload_ci16(Payload<int16_t> _p);

}  // extern "C"

}  // namespace test_gen

When uncomment Code 2 and comment Code 1, cbindgen works normally and generate a corresponding C++ wrapper:

template<typename T>
struct Payload {
  T data;
};

extern "C" {

void use_payload_ci16(Payload<int16_t> _p);
...

My build.rs:

use std::{env::var, fs, path::PathBuf};

pub fn main() {
    println!("cargo:rerun-if-changed=src/");
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=Cargo.toml");
    println!("cargo:rerun-if-changed=cbindgen.toml");

    let crate_dir = var("CARGO_MANIFEST_DIR").unwrap();
    let include_dir = PathBuf::from(&crate_dir).join("include");
    if !include_dir.exists() {
        fs::create_dir_all(&include_dir).expect("Failed to create include directory");
    }

    let header_path = include_dir.join("syncdaq.h");

    cbindgen::Builder::new()
        .with_crate(crate_dir)
        .with_config(cbindgen::Config::from_file("cbindgen.toml").unwrap())
        .generate()
        .unwrap()
        .write_to_file(header_path);
}

and the cbindgen.toml:

language = "C++"
include_guard = "TEST_GEN"
namespace = "test_gen"

[export]
include=["Payload"]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions