4 releases (1 stable)

1.0.0 Aug 21, 2023
0.3.0 Jul 21, 2023
0.2.0 Jun 20, 2023
0.1.0 May 3, 2023

#418 in Procedural macros

45 downloads per month

MIT license

16KB
223 lines

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,
    ]
);

Dependencies

~1–7MB
~44K SLoC