cpy-binder

Crates.iocpy-binder
lib.rscpy-binder
version1.0.0
sourcesrc
created_at2023-05-03 21:19:22.052067
updated_at2023-08-21 16:08:44.096863
descriptionHelps when creating binds from Rust to C++ and Python
homepage
repositoryhttps://github.com/patrickelectric/cpy-rs
max_upload_size
id855972
size18,046
Patrick José Pereira (patrickelectric)

documentation

README

cpy-rs 🦀🛠️🐍

Test all targets crates.io docs.rs

cpy-rs is a Rust library that aids in creating bindings from Rust to C++ and Python. It provides a set of macros to easily export Rust structures, enums, and functions to both C/C++ and Python.

Features

  • cpy_enum: To export enums.
  • cpy_struct: To export structures.
  • cpy_fn: To export functions for both C++ and Python.
  • cpy_fn_c: To export exclusive C++ functions.
  • cpy_fn_py: To export exclusive python functions.
  • cpy_module: To export the python module.

Note: It's recommended to end the function signature with _c and _py when using cpy_fn_c and cpy_fn_py. When using such functions in C++ and Python, the suffix will be removed by the macro. The suffix is also not necessary inside the cpy_module. Check the following example as a guide.

Example Usage

The repository contains an example project that demonstrates how to use cpy-rs to create bindings.

  • Check here to see an example of a python based code.
  • Check here to see an CMake / C++ based example.

Code usage

use cpy_binder::{cpy_enum, cpy_fn, cpy_fn_c, cpy_fn_py, cpy_module, cpy_struct};

#[cpy_enum]
#[comment = "Material types"]
enum Material {
    Plastic,
    Rubber,
}

#[cpy_struct]
#[comment = "2D Size"]
struct Size2D {
    width: f64,
    height: f64,
}

#[cpy_struct]
#[comment = "Tire structure"]
struct Tire {
    material: Material,
    pressure: f64,
    size: Size2D,
}


#[cpy_fn]
#[comment_c = "@brief Creates and returns a random tire.\n
    @return Tire A randomly generated tire.\n"]
#[comment_py = "Creates and returns a random tire.\n
    Returns:\n
        Tire: A randomly generated tire.\n"]
fn create_random_tire() -> Tire {
    use rand::Rng;
    let mut rng = rand::thread_rng();

    let random_material = if rng.gen_bool(0.5) {
        Material::Plastic
    } else {
        Material::Rubber
    };

    Tire {
        material: random_material,
        pressure: rng.gen_range(30.0..60.0),
        size: Size2D {
            width: rng.gen_range(5.0..10.0),
            height: rng.gen_range(10.0..20.0),
        },
    }
}

#[cpy_fn_c]
#[comment = "Function for C ABI"]
fn format_wheel_identifier_c(dimensions: &[u8; 3]) {
    println!("Wheel identifier: {:?}", dimensions);
}

#[cpy_fn_py]
#[comment = "Format wheel identifier for Python"]
fn format_wheel_identifier_py(dimensions: [u8; 3]) {
    println!("Wheel identifier: {:?}", dimensions);
}


// Used to export Python module
cpy_module!(
    name = example,
    types = [Material, Size2D, Tire],
    functions = [
        create_random_tire,
        format_wheel_identifier,
    ]
);
Commit count: 52

cargo fmt