Skip to content

Instantly share code, notes, and snippets.

@mikowitz

mikowitz/lib.rs Secret

Last active November 8, 2021 00:49
Show Gist options
  • Save mikowitz/8ca55d78ff2929ef4a7bb0cb233edd71 to your computer and use it in GitHub Desktop.
Save mikowitz/8ca55d78ff2929ef4a7bb0cb233edd71 to your computer and use it in GitHub Desktop.
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>;
#[rustler::nif]
fn new(width: i32, height: i32) -> WrapperRA {
let surface = ImageSurface::create(Format::ARgb32, width, height).expect("Nope");
let context = Context::new(&surface).expect("Nope");
ResourceArc::new(
CairoWrapper { context, surface }
)
}
#[rustler::nif]
fn paint(context: WrapperRA, red: f64, green: f64, blue: f64) -> WrapperRA {
context.context.set_source_rgb(red, green, blue);
context.context.paint().unwrap();
context
}
#[rustler::nif]
fn save(context: WrapperRA, filename: String) -> WrapperRA {
let mut file = File::create(filename).expect("Nope");
context.surface.write_to_png(&mut file).unwrap();
context
}
rustler::init!("Elixir.Xairo.Native", [new, paint, save], load=on_load);
fn on_load(env: Env, _info: Term) -> bool {
rustler::resource!(CairoWrapper, env);
true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment