Python && Rust
with Davis Silverman
(feel free to interrupt with questions!)
Who am I
- Student
- Hardcore coder
- Polyglot
Poll
- Python developers?
- C developers?
- Heard of Rust?
Rust
- Safe
- Concurrent
- Fast
Hello, World!
fn main() {
println!("Hello, World!");
}
More Complicated
#[derive(Debug)]
struct Point {
x: f64,
y: f64,
}
impl Point {
fn origin() -> Point { Point{x: 0.0, y: 0.0} }
fn new(x: f64, y: f64) -> Point { Point{x: x, y: y} }
fn distance(&self, other: &Point) -> f64 {
f64::sqrt(
f64::powi((self.x - other.x), 2) +
f64::powi((self.y - other.y), 2)
)
}
}
fn main() {
let a: Point = Point::origin();
let b = Point::new(1.0, 3.5);
let distance = b.distance(&a);
println!("The distance between {:?} and {:?} is {}", a, b, distance);
}
http://is.gd/EiIo3E
Python
- Amazing Language!
- Amazing Code!
- Not very fast! (and that is OK!)
C Extensions
- Speed of C
- Python as main driver
- Terribleness of C
Enter Rust
- No Header Files!
- Type safety!
- Clean modern language!
Rust-Extensions
/// A Library about points and stuff.
extern crate libc;
#[repr(C)]
pub struct Point {
x: libc::c_double,
y: libc::c_double,
}
#[no_mangle]
pub extern "C" fn point_origin() -> Box<Point> {
Box::new(Point{x: 0.0, y: 0.0})
}
#[no_mangle]
pub extern "C" fn point_new(x: libc::c_double, y: libc::c_double) -> Box<Point> {
Box::new(Point{x: x, y: y}
}
#[no_mangle]
pub extern "C" fn point_distance(this: Box<Point>, other: Box<Point>) -> libc::c_double {
f64::sqrt(f64::powi((this.x - other.x), 2) +
f64::powi((this.y - other.y), 2))
}
ctypes
- defining a class
- define argument and return types
- ???
- Free!
Defining a class
- Whatever name you want
- field names need to be the same
- Derive from
Structure
from ctypes import Structure, c_double
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
Types!
- using c_* for types
- define them all or might not work
from ctypes import Structure, CDLL, c_double, pointer, POINTER
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
rlib = CDLL("points.dll")
rlib.point_new.restype = POINTER(POINT)
rlib.point_new.argtypes = [c_double, c_double]
rlib.point_origin.restype = POINTER(POINT)
rlib.point_origin.argtypes = []
???
- use them like you would anything else!
- You might need to free things!
from ctypes import Structure, CDLL, c_double, pointer, POINTER
class POINT(Structure):
_fields_ = [("x", c_double), ("y", c_double)]
rlib = CDLL("points.dll")
rlib.point_new.restype = POINTER(POINT)
rlib.point_new.argtypes = [c_double, c_double]
rlib.point_origin.restype = POINTER(POINT)
rlib.point_origin.argtypes = []
a = rlib.point_new(c_double(1.0), c_double(3.5))
b = rlib.point_origin()
print("Point {{x: {}, y: {}}}".format(a.contents.x, a.contents.y))
Short Demo
lets hope it works!
THE END?
- rust-cpython
- HTML5EVER
- Short Term Employment!
Questions?
- sinistersnare@gmail.com
- @sinistersnare (Github / ~twitter)
Python && Rust
By Davis Silverman
Python && Rust
Using Rust instead of C for C-Extensions in the CPython interpreter.
- 1,643