{"id":16403487,"url":"https://github.com/patrickelectric/cpy-rs","last_synced_at":"2025-06-18T17:36:20.605Z","repository":{"id":161237711,"uuid":"635965335","full_name":"patrickelectric/cpy-rs","owner":"patrickelectric","description":"A Rust crate to help creating C++ and Python bindings","archived":false,"fork":false,"pushed_at":"2023-08-21T16:08:38.000Z","size":6424,"stargazers_count":10,"open_issues_count":5,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-18T18:06:37.023Z","etag":null,"topics":["bindings","cpp","python","rust"],"latest_commit_sha":null,"homepage":"https://patrickelectric.work/cpy-rs/cpy_binder/","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/patrickelectric.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-05-03T20:54:31.000Z","updated_at":"2025-01-20T02:35:34.000Z","dependencies_parsed_at":null,"dependency_job_id":"56d185fb-a65c-4b4c-aba6-8bac0f7fb46e","html_url":"https://github.com/patrickelectric/cpy-rs","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickelectric%2Fcpy-rs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickelectric%2Fcpy-rs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickelectric%2Fcpy-rs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/patrickelectric%2Fcpy-rs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/patrickelectric","download_url":"https://codeload.github.com/patrickelectric/cpy-rs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245061382,"owners_count":20554563,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bindings","cpp","python","rust"],"created_at":"2024-10-11T05:49:29.740Z","updated_at":"2025-03-23T05:31:14.358Z","avatar_url":"https://github.com/patrickelectric.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cpy-rs 🦀🛠️🐍\n[![Test all targets](https://github.com/patrickelectric/cpy-rs/actions/workflows/action.yml/badge.svg)](https://github.com/patrickelectric/cpy-rs/actions/workflows/action.yml)\n[![crates.io](https://img.shields.io/crates/v/cpy-binder.svg)](https://crates.io/crates/cpy-binder)\n[![docs.rs](https://img.shields.io/docsrs/cpy-binder?label=docs.rs)](https://docs.rs/cpy-binder)\n\n\n\n`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.\n\n## Features\n\n- **cpy_enum**: To export enums.\n- **cpy_struct**: To export structures.\n- **cpy_fn**: To export functions for both C++ and Python.\n- **cpy_fn_c**: To export exclusive C++ functions.\n- **cpy_fn_py**: To export exclusive python functions.\n- **cpy_module**: To export the python module.\n\n\u003e 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.\n\n## Example Usage\n\nThe repository contains [an example project](https://github.com/patrickelectric/cpy-rs/tree/master/example) that demonstrates how to use `cpy-rs` to create bindings.\n\n* [Check here](https://github.com/patrickelectric/cpy-rs/tree/master/example/py_project) to see an example of a python based code.\n* [Check here](https://github.com/patrickelectric/cpy-rs/tree/master/example/cpp_project) to see an CMake / C++ based example.\n\n### Code usage\n\n```rust\nuse cpy_binder::{cpy_enum, cpy_fn, cpy_fn_c, cpy_fn_py, cpy_module, cpy_struct};\n\n#[cpy_enum]\n#[comment = \"Material types\"]\nenum Material {\n    Plastic,\n    Rubber,\n}\n\n#[cpy_struct]\n#[comment = \"2D Size\"]\nstruct Size2D {\n    width: f64,\n    height: f64,\n}\n\n#[cpy_struct]\n#[comment = \"Tire structure\"]\nstruct Tire {\n    material: Material,\n    pressure: f64,\n    size: Size2D,\n}\n\n\n#[cpy_fn]\n#[comment_c = \"@brief Creates and returns a random tire.\\n\n    @return Tire A randomly generated tire.\\n\"]\n#[comment_py = \"Creates and returns a random tire.\\n\n    Returns:\\n\n        Tire: A randomly generated tire.\\n\"]\nfn create_random_tire() -\u003e Tire {\n    use rand::Rng;\n    let mut rng = rand::thread_rng();\n\n    let random_material = if rng.gen_bool(0.5) {\n        Material::Plastic\n    } else {\n        Material::Rubber\n    };\n\n    Tire {\n        material: random_material,\n        pressure: rng.gen_range(30.0..60.0),\n        size: Size2D {\n            width: rng.gen_range(5.0..10.0),\n            height: rng.gen_range(10.0..20.0),\n        },\n    }\n}\n\n#[cpy_fn_c]\n#[comment = \"Function for C ABI\"]\nfn format_wheel_identifier_c(dimensions: \u0026[u8; 3]) {\n    println!(\"Wheel identifier: {:?}\", dimensions);\n}\n\n#[cpy_fn_py]\n#[comment = \"Format wheel identifier for Python\"]\nfn format_wheel_identifier_py(dimensions: [u8; 3]) {\n    println!(\"Wheel identifier: {:?}\", dimensions);\n}\n\n\n// Used to export Python module\ncpy_module!(\n    name = example,\n    types = [Material, Size2D, Tire],\n    functions = [\n        create_random_tire,\n        format_wheel_identifier,\n    ]\n);\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickelectric%2Fcpy-rs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpatrickelectric%2Fcpy-rs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpatrickelectric%2Fcpy-rs/lists"}