Instantly share code, notes, and snippets.

@Gankro /_lib.rs
Last active Jan 30, 2018

Embed
What would you like to do?
cbindgen bridging Rust tagged unions into C++
// Rust Library
#[repr(u8)]
pub enum DisplayItem {
Fill(Rect, Color),
Image { id: u32, bounds: Rect },
ClearScreen,
}
#[repr(C)]
pub struct Rect { x: f32, y: f32, w: f32, h: f32 }
#[repr(C)]
pub struct Color { r: u8, g: u8, b: u8, a: u8 }
#[no_mangle]
pub extern "C" fn push_item(item: DisplayItem) -> bool {
::std::mem::drop(item);
true
}
// 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 {
Fill,
Image,
ClearScreen,
};
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