Skip to content

Instantly share code, notes, and snippets.

@genesem
Forked from seanjensengrey/rust-python-cffi.md
Created February 23, 2016 14:38
Show Gist options
  • Save genesem/ec5382e4958dd4130f2b to your computer and use it in GitHub Desktop.
Save genesem/ec5382e4958dd4130f2b to your computer and use it in GitHub Desktop.
Calling Rust from Python/PyPy using CFFI (C Foreign Function Interface)

This is a small demo of how to create a library in Rust and call it from Python (both CPython and PyPy) using the CFFI instead of ctypes.

Based on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used ctypes

CFFI is nice because:

  • Reads C declarations (parses headers)
  • Works in both CPython and PyPy (included with PyPy)
  • Lower call overhead than ctypes

Install Rust

from either of:

I recommend installing Rust via multirust so that you can compile projects that use unstable features (core) as well as testing for Rust 1.0,1.1, etc backwards compatibility.

The per-directory override mechanism is especially nice.

Build library

rustc treble.rs

This will create a libtreble.dylib

Run the Python client

python test.py 
<cffi.api.FFILibrary_./libtreble.dylib object at 0x1089d5490>
math from rust! 30
try:
from cffi import FFI
except ImportError:
print "pip install cffi, included with PyPy"
ffi = FFI()
lib = ffi.dlopen("./libtreble.dylib")
print lib
# <cffi.api.FFILibrary_./libtreble.dylib object at 0x107f440d0>
ffi.cdef('int treble(int);')
print "math from rust!", lib.treble(10)
#![crate_type = "dylib"]
#[no_mangle]
pub extern fn treble(value: i32) -> i32 {
value * 3
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment