Giter Site home page Giter Site logo

pombredanne / rustypy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from iduartgomez/rustypy

0.0 1.0 0.0 1014 KB

Automatic FFI and binding generation for Python <-> Rust interfacing

License: BSD 3-Clause "New" or "Revised" License

Python 51.00% Rust 49.00%

rustypy's Introduction

RustyPy Build Status

RustyPy is a code generator for generating binding functions between Rust and Python files. In addition it provides a series of types for interfacing from Python with Rust, with automatic type conversion handling for primitives and other basic types (like vectors, hash maps and tuples) and an API for working with those (both from Rust and Python).

Features

  • Generate bindings in Rust targetting Python functions.
  • Generate bindings in Python targetting Rust functions.
  • Provides types for interfacing between Rust and Python.

Installation

To install RustyPy just use pip:

pip install rustypy

RustyPy requires Python 3.5 or more and works with Rust stable.

To target Python from Rust the package cpython is required to initialize the package.

Documentation

Usage

RustyPy includes a command line interface to generate the code, which you can embeed in your build chain if it's necessary.

Generate Python bindings in Rust

You can execute the script writing:

$rustypy -h

or

$python -m rustypy -h

make sure that rustypy is in your current Python path. The help command has all the information to generate bindings succesfully.

It also includes functions to generate bindings dynamically. In Python use:

from rustypy.pywrapper import bind_py_pckg_funcs
# default prefix is "rust_bind_"
optional = ["my_bind_prefix_", "other_prefix_"]
bind_py_pckg_funcs(prefixes=optional)

This function will generate the bindings for the package from which is called from (so the package must be initiated placing an _init_.py file in one of the parents folders).

More info: Python in Rust

Generate Rust bindings in Python

Due to the nature of Python this is done dynamically, so no files are generated and the bindings are wrapped appropriately with their own callables from Python.

from rustypy.rswrapper import bind_rs_crate_funcs

source_path = "/home/user/workspace/rs_test_lib"
compiled_lib = "/home/user/workspace/rs_test_lib/target/debug/libtest_lib.so"

# default prefix is "python_bind_"
optional = ["my_bind_prefix_", "other_prefix_"]
lib_binds = bind_rs_crate_funcs(source_path, compiled_lib, prefixes=optional)

lib_binds.my_bind_prefix__ffi_function("Hello from Python!")

There is no concept of 'module' in C (which is the language used for interfacing) so the functions cannot be namedspaced as you would in pure Rust. Read about Rust FFI in the book.

More info: Rust in Python.

Rust crate documentation.

rustypy's People

Contributors

iduartgomez avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.