On 05/26/2012 04:38 PM, Benjamin Kircher wrote:
Hi.

This is my first mail to this list, so hello all. I am doing C++
during daytime for a small company in Germany since nearly 4 years now
but I wrestle with that language a bit longer (since the Visual Studio
6.0 days, uh), well, and never really did a lot of C. There are a few
things about Rust I must say I really like. I am poking around with
rustc since a few weeks, having some fun in the late hours after work.
So much for the introduction.

Welcome!

While declaring a record in a Rust program is very straightforward it
is cumbersome to retype all the information already present in C
header files, if not dangerous when the structures are not as expected
at runtime. Is there any way to actually get the declaration from a C
header in Rust, i.e. by using a  #include statement or to get any
compile time error when the types are not as expected? Am I missing
something?

There is a project called bindgen[1] that is going to become the official way to do C bindings. You run it against some C header files and it emits Rust source code with the proper declarations. Eventually it will be integrated into the main rust distribution as a tool and/or compiler pass.

Right now it's not quite user-friendly and it sometimes generates things that need manual tweaking, but I do use it for all my native bindings now.

Here are some projects that are using bindgen (see the gen.py files for examples of running the tool):

* https://github.com/brson/rust-mozjs
* https://github.com/brson/rust-azure
* https://github.com/brson/rust-harfbuzz

-Brian

[1] http://github.com/crabtw/rust-bindgen
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to