https://github.com/robertknight/rust-cmacros
Rust lib for parsing macros from C header files and assisting with translation to Rust code
https://github.com/robertknight/rust-cmacros
Last synced: 28 days ago
JSON representation
Rust lib for parsing macros from C header files and assisting with translation to Rust code
- Host: GitHub
- URL: https://github.com/robertknight/rust-cmacros
- Owner: robertknight
- Created: 2015-06-20T21:35:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2017-04-27T08:55:49.000Z (about 8 years ago)
- Last Synced: 2025-03-11T12:52:07.200Z (about 2 months ago)
- Language: Rust
- Size: 137 KB
- Stars: 1
- Watchers: 2
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rust-cmacros
[](https://travis-ci.org/robertknight/rust-cmacros)Rust library to assist with parsing and translating #define
macro definitions from C header files
to corresponding Rust code for use with bindings
to external libraries.**Update: Vojtech Kral suggested a much better idea - Generate C++ code which leverages the compiler's type inference to determine the types of macros: http://vojtech.kral.hk/en/rust-importing-c-constants-proof-of-concept/**
## Intro
To use libraries with a C interface from Rust,
[rust-bindgen](https://github.com/crabtw/rust-bindgen) can be used
to generate Rust bindings automatically. It does not translate
constants or functions defined as macros in C headers to Rust
code however.rust-cmacros is a simple and fairly dumb library which helps to
fill this gap.It has two basic functions:
* `extract_macros()` parses the source of a C header file and extracts C macro definitions
* `generate_rust_src()` takes as input a set of extracted macros and a translator function
and generates Rust code.## Examples
```rust
cargo run --example parse_all_headers /usr/include
```Parse all headers in `/usr/include` and print re-constructed #define statements
```rust
cargo run --example translate_macros /usr/include/sqlite3.h
```Parse `/usr/include/sqlite3.h` and output translations of the macros using the default translation function.
In real usage, you would supply a translation function to skip or modify the translations
for non-trivial or unnecessary macros.