Last active
August 29, 2015 14:12
-
-
Save jackdempsey/ab7871eecb295a342883 to your computer and use it in GitHub Desktop.
points
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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