Crates.io | cpy-binder |
lib.rs | cpy-binder |
version | 1.0.0 |
source | src |
created_at | 2023-05-03 21:19:22.052067 |
updated_at | 2023-08-21 16:08:44.096863 |
description | Helps when creating binds from Rust to C++ and Python |
homepage | |
repository | https://github.com/patrickelectric/cpy-rs |
max_upload_size | |
id | 855972 |
size | 18,046 |
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.
Note: It's recommended to end the function signature with
_c
and_py
when usingcpy_fn_c
andcpy_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 thecpy_module
. Check the following example as a guide.
The repository contains an example project that demonstrates how to use cpy-rs
to create bindings.
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,
]
);