使用 rusty-cheddar 在 C 中生成不透明指针



我正在尝试使用 rusty-cheddar 板条箱为用 Rust 编写的库生成 C 头文件。

以下是 Rust 中结构体的定义和实现:

pub struct AccountDatabase {
    money: HashMap<String, u32>,
}
impl AccountDatabase {
    fn new() -> AccountDatabase {
        AccountDatabase {
            money: HashMap::new(),
        }
    }
}

如果我把#[repr(C)]放在结构体之前,生锈的切达干酪会在 C 中生成以下结构体声明

typedef struct AccountDatabase {
    HashMap money;
} AccountDatabase;

C 不知道HashMap,因此我希望将结构声明为不透明指针。

解决方案直接在自述文件上指定:

要定义不透明结构,您必须定义一个标记为 #[repr(C)] 的公共 newtype。

因此:

struct AccountDatabase {
    money: HashMap<String, u32>,
}
impl AccountDatabase {
    fn new() -> AccountDatabase {
        AccountDatabase {
            money: HashMap::new()
        }
    }
}
#[repr(C)]
pub struct Crate_AccountDatabase(AccountDatabase);

(或您选择的其他结构命名(

最新更新