Skip to main content

Automatic FFI generation for Python <-> Rust interfacing.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

rustypy-0.1.17.tar.gz (39.8 kB view details)

Uploaded Source

File details

Details for the file rustypy-0.1.17.tar.gz.

File metadata

  • Download URL: rustypy-0.1.17.tar.gz
  • Upload date:
  • Size: 39.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for rustypy-0.1.17.tar.gz
Algorithm Hash digest
SHA256 1f9a367ee6f08e554545493cf0ba0fde96e1a4e927702c92aeddb1841ccc3a72
MD5 f2d1bfb45d4034c5d435da29dfdb8f33
BLAKE2b-256 30b7a541634d70596d9934e5a5ad5e29446007ee4bcfbb373537fe4995c07dc8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page