Skip to content

Instantly share code, notes, and snippets.

@jackdempsey
Last active August 29, 2015 14:12
Show Gist options
  • Save jackdempsey/ab7871eecb295a342883 to your computer and use it in GitHub Desktop.
Save jackdempsey/ab7871eecb295a342883 to your computer and use it in GitHub Desktop.
points
require "fiddle"
require "fiddle/import"
module RustPoint
extend Fiddle::Importer
dlload "./libpoints.dylib"
extern "Point* make_point(int, int)"
extern "double get_distance(Point*, Point*)"
end
use std::num::Int;
use std::num::Float;
pub struct Point { x: int, y: int }
struct Line { p1: Point, p2: Point }
impl Line {
pub fn length(&self) -> f64 {
let xdiff = self.p1.x - self.p2.x;
let ydiff = self.p1.y - self.p2.y;
((xdiff.pow(2) + ydiff.pow(2)) as f64).sqrt()
}
}
#[no_mangle]
pub extern "C" fn make_point(x: int, y: int) -> Box<Point> {
box Point { x: x, y: y }
}
#[no_mangle]
pub extern "C" fn get_distance(p1: &Point, p2: &Point) -> f64 {
Line { p1: *p1, p2: *p2 }.length()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment