top | item 41271273

Zngur: A C++/Rust interop tool

154 points| hkalbasi | 1 year ago |hkalbasi.github.io

32 comments

order

benreesman|1 year ago

Having only glanced at the code I can’t offer much insight beyond “looks plausible”.

A far more important point is to address is that any FFI story with C++ that’s well executed is going to be great for the Rust ecosystem, and historically the community’s commitment to C++ interop has seemed tepid at best.

Even if one admits that Rust is strictly better than C++ for problems in the relevant domains, which seems an extraordinary claim that exceeds extraordinary evidence, there is still an ocean of C++ that we just can’t rewrite in even a decade, maybe not in a century.

There are use cases where Rust is just strictly a better choice: anything with an attack surface like a shell or an SSH daemon, or probably even a browser is an obvious candidate for a language with better reasoning about common attack vectors. I trust my Rust user land a lot more than my C userland.

But the “rewrite everything in Rust and push that via bad interop” is limiting the adoption of a cool language.

I’m a big fan of efforts to be more incremental.

tialaramex|1 year ago

> we just can’t rewrite in even a decade, maybe not in a century.

The former claim seems unlikely as stated ("cannot") but is at least plausible in practice ("will not"). The latter makes no sense. C++ didn't exist fifty years ago, so you're asking that we believe somehow C++ was so well suited to some unspecified problems that rewriting software in a better language will take more than twice as long.

saghm|1 year ago

> But the “rewrite everything in Rust and push that via bad interop” is limiting the adoption of a cool language.

For what it's worth, I've rarely heard "rewrite it in Rust" pushed by the most influential and productive members of the Rust ecosystem. I think the disparity here is that the people pushing the more extreme agenda aren't the same ones making major projects that people use, so it turns out that most of the stuff being made is for writing new software since that's what the people making it are interested in.

uneekname|1 year ago

I've been learning Rust and really enjoying it, but the primary downside (compared to C++) is the lack of access to the large, well-tested libraries that exist in C++. For example, my work would benefit from using the CGAL library [0], something that be nontrivial to rewrite in Rust. As much as I like Rust, at some point it's a better idea to use C++ to tap into libraries like that.

Does anyone have thoughts about how mature Zngur/CXX/etc. are for a project like mine? Would it be reasonable to invest effort in creating those bindings? Thanks.

[0] https://www.cgal.org/

throwup238|1 year ago

I’m using cxx/cxx-qt in a Rust QT app with substantial interop with Qt/QML and lots of C++ code (because Gpt/Claude make implementing some stuff easier in C++). It’s definitely mature enough because Qt’s libs are no joke.

At this point I have a Claude project set up with Cxx documentation and it autogenerates most of my bindings.

npalli|1 year ago

   So, Zngur allows you to use arbitrary Rust types in C++, store them by value in the C++ stack, and call arbitrary Rust methods and functions on them. But it doesn't bridge any C++ type into Rust, since it is not possible with the same ergonomic
You want to use C++ libraries from Rust. This only does interop the other way.

bschwindHN|1 year ago

I've used cxx.rs to bind to the OpenCascade CAD kernel. It was a lot of work but the bindings seem to work well.

hkalbasi|1 year ago

Zngur allows you to write functions operating over Rust types, adding methods to Rust types and implementing Rust traits for C++ and Rust types inside C++. So you can write a rusty wrapper for the C++ library inside C++.

If you want to immediately use a C++ library in Rust with minimal manual effort, check out autocxx [0]. Its generated api isn't very idiomatic Rust, and it uses Pin, moveit, ... but it covers a good percent of C++ apis and you can also make an idiomatic wrapper for it in Rust.

[0] https://docs.rs/autocxx/latest/autocxx/

pjmlp|1 year ago

This kind of tools are great, as C++ isn't going away any time soon, and Rust is decades away to be relevant in some domains, specially those where C++ only recently took over C.

mastax|1 year ago

Wow! Amazing how reframing the problem to exclude C++ types in rust makes it all so much more elegant.

hi-v-rocknroll|1 year ago

Isn't Google offering funding for this exact topic?

jdright|1 year ago

This is insane.

Is this being used in some codebase? How stable is it? What are the plans for it?

I have so many questions, but will stick with these for now.