-
-
Save mcapell/0a9465cf84f5566eb0b56cb183c8f045 to your computer and use it in GitHub Desktop.
Revisions
-
seanjensengrey revised this gist
Feb 21, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ This is a small demo of how to create a library in [Rust](http://www.rust-lang.org/) and call it from Python (both CPython and PyPy) using the [CFFI](https://cffi.readthedocs.org/en/latest/) instead of `ctypes`. Based on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used `ctypes` CFFI is nice because: -
seanjensengrey revised this gist
Jul 20, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ This is a small demo of how to create a library in [Rust](http://www.rust-lang.org/) and call it from Python (both CPython and PyPy) using the [CFFI](https://cffi.readthedocs.org/en/latest/) Based on http://harkablog.com/calling-rust-from-c-and-python.html which used `ctypes` -
seanjensengrey revised this gist
Jul 20, 2015 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ This is a small demo of how to create a library in [Rust](http://www.rust-lang.org/) and call it from Python using the [CFFI](https://cffi.readthedocs.org/en/latest/) Based on http://harkablog.com/calling-rust-from-c-and-python.html which used `ctypes` @@ -17,7 +17,7 @@ from either of: * `brew install rust` * [Multirust](https://gist.github.com/seanjensengrey/07bb8ae5397a81d39321) 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. -
seanjensengrey renamed this gist
May 27, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
seanjensengrey revised this gist
May 27, 2015 . 2 changed files with 41 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -6,4 +6,42 @@ 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: * `brew install rust` * [Multirust](https://gist.github.com/seanjensengrey/07bb8ae5397a81d39321) 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 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 ``` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,5 +11,5 @@ ffi.cdef('int treble(int);') print "math from rust!", lib.treble(10) -
seanjensengrey created this gist
May 27, 2015 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ This is a small demo of how to create a library in [Rust](http://www.rust-lang.org/) and calling it from Python via the [CFFI](https://cffi.readthedocs.org/en/latest/) Based on http://harkablog.com/calling-rust-from-c-and-python.html 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` This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ 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 lib.treble(10) # 30 This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,6 @@ #![crate_type = "dylib"] #[no_mangle] pub extern fn treble(value: i32) -> i32 { value * 3 }