Instantly share code, notes, and snippets.

@Gankro /
Last active Jan 30, 2018

What would you like to do?
cbindgen bridging Rust tagged unions into C++
// Rust Library
pub enum DisplayItem {
Fill(Rect, Color),
Image { id: u32, bounds: Rect },
pub struct Rect { x: f32, y: f32, w: f32, h: f32 }
pub struct Color { r: u8, g: u8, b: u8, a: u8 }
pub extern "C" fn push_item(item: DisplayItem) -> bool {
// output of running cbindgen on the above crate
#include <cstdint>
#include <cstdlib>
struct Rect {
float x;
float y;
float w;
float h;
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
uint8_t a;
union DisplayItem {
enum class Tag : uint8_t {
struct Fill_Body {
Tag tag;
Rect _0;
Color _1;
struct Image_Body {
Tag tag;
uint32_t id;
Rect bounds;
Tag tag;
Fill_Body fill;
Image_Body image;
extern "C" {
bool push_item(DisplayItem item);
} // extern "C"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment