Skip to content

Instantly share code, notes, and snippets.

@mcapell
Forked from seanjensengrey/rust-python-cffi.md
Created December 9, 2016 14:40
Show Gist options
  • Save mcapell/0a9465cf84f5566eb0b56cb183c8f045 to your computer and use it in GitHub Desktop.
Save mcapell/0a9465cf84f5566eb0b56cb183c8f045 to your computer and use it in GitHub Desktop.

Revisions

  1. @seanjensengrey seanjensengrey revised this gist Feb 21, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rust-python-cffi.md
    Original 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/)
    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 which used `ctypes`
    Based on http://harkablog.com/calling-rust-from-c-and-python.html (dead) which used `ctypes`

    CFFI is nice because:

  2. @seanjensengrey seanjensengrey revised this gist Jul 20, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion rust-python-cffi.md
    Original 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/)
    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`

  3. @seanjensengrey seanjensengrey revised this gist Jul 20, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions rust-python-cffi.md
    Original 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 calling it from Python via the [CFFI](https://cffi.readthedocs.org/en/latest/)
    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 backwards compatibility.
    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.

  4. @seanjensengrey seanjensengrey renamed this gist May 27, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @seanjensengrey seanjensengrey revised this gist May 27, 2015. 2 changed files with 41 additions and 3 deletions.
    40 changes: 39 additions & 1 deletion README.md
    Original 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`
    * 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
    ```






    4 changes: 2 additions & 2 deletions test.py
    Original file line number Diff line number Diff line change
    @@ -11,5 +11,5 @@

    ffi.cdef('int treble(int);')

    print lib.treble(10)
    # 30
    print "math from rust!", lib.treble(10)

  6. @seanjensengrey seanjensengrey created this gist May 27, 2015.
    9 changes: 9 additions & 0 deletions README.md
    Original 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`
    15 changes: 15 additions & 0 deletions test.py
    Original 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
    6 changes: 6 additions & 0 deletions treble.rs
    Original 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
    }