4 ms·
Not sure why this post doesn't mention it, but for nontrivially sized C APIs, you don't have to convert the function signatures to Rust manually; instead you ca
by comex 12y ago
Not sure why this post doesn't mention it, but for nontrivially sized C APIs, you don't have to convert the function signatures to Rust manually; instead you can use rust-bindgen:
https://github.com/crabtw/rust-bindgen https://github.com/crabtw/rust-bindgen
(Caveat: I think it would need to be fixed up to avoid unstable APIs in order to run on stable releases of Rust, as opposed to nightlies.)
- kibwen 12y agorust-bindgen is an amazing project, dating all the way back to Rust 0.1 and dutifully updated by various community members throughout more than three years of catastrophically breaking changes. It's an invaluable boon to anyone binding a C API from Rust. > I think it would need to be fixed up to avoid unstable > APIs in order to run on stable releases of Rust, as > opposed to nightlies. The code generated by rust-bindgen won't be using any unstable features, so even if rust-bindgen itself happens to require a nightly build of Rust (which would be less than ideal), it can still be used to generate interfaces that can be used by stable Rust.
- minthd 12y agoInteresting. Can rust-bindgen support object oriented c?
- comex 12y agoNot sure what you mean by "object oriented C". It doesn't support C++ presently (something I'd love to see improved); there are some projects that do Objective-C interop; if you just mean C that uses structs in an OO-like way, it shouldn't be that different from any other C.
- minthd 12y agoYes, when using structs in an OO like way. Does it generate c-style rust wrapper ? or can it generate class-based rust wrapper ,perhaps with some guidance ?
- ossreality 12y agoHow would you generate a class based wrapper from C? Do you understand what youre even asking?
- comex 12y agoAFAIK, rust-bindgen doesn't currently have support for translating C functions and structs into anything other than the same things in Rust. It's somewhat idiomatic for manually-written bindings, layered on top of the generated ones, to take care of things like providing a memory-safe interface (which can't really be done automatically), converting between native Rust types and C representations if necessary (perhaps including things like Result vs. error codes or nulls), and adding object orientation. If you want to go full automatic, though, you can do a lot with Rust macros, so give them a try. Examples of manual wrappers on top of bindgen - there are many: http://erickt.github.io/blog/2014/12/13/rust-and-mdbm/ http://erickt.github.io/blog/2014/12/13/rust-and-mdbm/ https://github.com/kenz-gelsoft/wxRust/blob/rust-servo/src/base.rs https://github.com/kenz-gelsoft/wxRust/blob/rust-servo/src/b... (I don't like the design of this one, actually) https://github.com/rust-gnome/gtk/blob/master/src/widgets/action_bar.rs https://github.com/rust-gnome/gtk/blob/master/src/widgets/ac... (better)
- apaprocki 12y agoI assume parent meant something like GObject, where you get opaque reference counted structs. (Generally something like gobject-introspection is used to generically bind it to other languages, not auto-generated wrappers.)