Skip to content

Instantly share code, notes, and snippets.


mikowitz/ Secret

Last active November 8, 2021 00:49
What would you like to do?
First compiling version of Xairo Rust code
use cairo::{ImageSurface, Context, Format};
use rustler::{Env, ResourceArc, Term};
use std::fs::File;
pub struct CairoWrapper {
pub context: Context,
pub surface: ImageSurface
type WrapperRA = ResourceArc<CairoWrapper>;
fn new(width: i32, height: i32) -> WrapperRA {
let surface = ImageSurface::create(Format::ARgb32, width, height).expect("Nope");
let context = Context::new(&surface).expect("Nope");
CairoWrapper { context, surface }
fn paint(context: WrapperRA, red: f64, green: f64, blue: f64) -> WrapperRA {
context.context.set_source_rgb(red, green, blue);
fn save(context: WrapperRA, filename: String) -> WrapperRA {
let mut file = File::create(filename).expect("Nope");
context.surface.write_to_png(&mut file).unwrap();
rustler::init!("Elixir.Xairo.Native", [new, paint, save], load=on_load);
fn on_load(env: Env, _info: Term) -> bool {
rustler::resource!(CairoWrapper, env);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment