11 ms·
A Guide to Porting C/C++ to Rust
- geezerjay 10y agoThe book appears to be promising, but it is still far from being complete and doesn't touch the main points of interest, such as inheritance and problems arising from any form of template specialization. Perhaps the author could get the book up to a presentable point before advertising it.
- jhoechtl 10y agoThere was a rant earlier this week in the same vain: Instead of presenting something pre-mature check back later once its done. I think it is still wothwhile to present something you have instead of waiting once its done. Also the understanding "once its done" varies from the use case. This in your opinion unfinished book might be of use to others. It is also good to have something written and share it with others to get feedback (as you did) at an early stage.
- e12e 10y agoIs there any indication that the hn submitter "ingve" and the github owner "locka99" is the same person? For that matter, the author might want to put his/her name on the book - I couldn't find anything in either the foreword or credits section.
- bluejekyll 10y agoOr maybe they're trying to get more community support for it?
- tom_mellior 10y ago> doesn't touch the main points of interest Including how to port code from C or C++ to Rust, i.e., what it claims to be. So far this is an OK overview of Rust's advantages over C and C++, but very very little of it is an actual "guide" to porting stuff.
- ericpts 10y agoI find that most of the arguments given against C++ (such as memory leaks or dangling pointers) can be avoided by using Modern C++ features, like never allocating memory with new but using std::make_unique or avoiding null pointers with not_null from GSL or std::optional.
- burntsushi 10y agoThis seems to be about inline with some criticism this guide got on /r/rust: https://www.reddit.com/r/rust/comments/5qq7ty/a_guide_to_porting_c_c_to_rust/dd1wrh1/ https://www.reddit.com/r/rust/comments/5qq7ty/a_guide_to_por... Specifically, I think this work should be regarded as a "work in progress." :-)
- Tesl 10y agoDon't think there is a std::optional yet, have to go with boost::optional. I think you're point is completely correct though. You can write very safe code today using modern C++ features.
- vvanders 10y agoThose things help but it's still possible to return a pointer to an inner part of an allocation(say an element in a vector) and then have the vector go out of scope. Rust is much more robust about catching those cases and verifying that you don't have objects outliving their references.
- geezerjay 10y ago> Those things help but it's still possible to return a pointer to an inner part of an allocation(say an element in a vector) and then have the vector go out of scope. That's something that the programmer needs to specifically ask for, and I'm not aware of any C++ compiler which doesn't throw a bunch of warnings regarding an object's scope and lifetime. That means your point boils down to arguing that if a programmer really wants to, he can still shoot himself in the proverbial foot by writing C++ code, but in order for that to happen he needs to have a death wish and intentionally avoid all warnings and safe practices to begin with.
- ar15saveslives 10y agoThe main problem is what to do with tons of opensource/thirdparty C++ libraries.
- bluejekyll 10y agoIsn't that what this guide is for?
- quickben 10y agoNothing. Unless you think rust will become the next c++, and the world will move to it en masse, in which case people still won't rewrite most of the old libraries to it. At best people can hope that somebody comes up with a neat import system of c++ namespaces, a rock solid debugger and a fluid ide. Rust will be like Go. Both pushed by corporations, and probably follow the fate of cobol, if history is to be asked.
- moomin 10y agoSince COBOL is the longest actively used language I wouldn't say that was so bad.
- quickben 10y agoIf anything it was good for the overall good of our ecosystem. People coded it, used it, built with it. We all are in a better place knowledge and experience wise. New languages generally are good.
- bluejekyll 10y ago> Rust will be like Go. Perhaps in adoption, but Rust has a huge feature over Go, which is that it can be integrated into existing codebases through FFI conventions, such that you can slowly incorporate Rust in safety critical areas (as an initial starting point).
- bluejekyll 10y agoI just started porting an application using corrode on a C library at work. This was more of a POC, to show the team. Based on that, I think this book is missing a section on incremental conversion. You don't want to convert entire applications at once, you want to do it a source file or module at a time. This means incorporating the Rust toolchain into the build of the C/C++ project Makefile, or etc., system. It is not difficult, and will increase confidence in the final product. This can even be used to test incremental conversions in production, rather than having any doubt about the entire conversion. This is an awesome start for a great manual on the area. I'm very inspired by the experience I had in doing this, and hope that we do more.
- tom_mellior 10y ago> I just started porting an application using corrode on a C library at work. Can you tell us a bit about your experience with Corrode? The Readme itself states that it's not expected to work on "real" code, and there are open issues like this: https://github.com/jameysharp/corrode/issues/52 https://github.com/jameysharp/corrode/issues/52 titled "Arrays are broken!" in which Corrode's developer wrote half a year ago: "The current hack gets arrays translated correctly in function arguments, but gets them wrong everywhere else." In your experience, does it work better than this makes it seem? (To be somewhat fair, the issue does have detailed discussion, and partial fixes seem to have been merged, but it's still open.)
- bluejekyll 10y agoI didn't run into the Arrays issue. This issue I filed, has a list of all the things I hit: https://github.com/jameysharp/corrode/issues/109 https://github.com/jameysharp/corrode/issues/109 Most of the issues I ran into were not exactly with the corrode tool, but with compiler options, i.e. passing the DARWIN_C_LEVEL flag for macOS (my main dev machine). Actually, the library I hacked on was only built for Linux, I spent a lot of my time converting the C to multi-platform, not because it was necessary, but because I didn't feel like running a VM. What I found is that corrode produces mostly compilable code. I had played around a bit with FFI from C to Rust before, so the task of integrating a Rust library into a C codebase was already familiar to me. I would love to see a guide for doing that in corrode though, like an example project, for others. If I have time I might do that. One really cool thing, is that the Rust compiler detected some undefined behavior in our C code, which I filed a bug for to our team (it was an uninitialized stack variable, passed as a pointer to a function; in practice it would never actually rely on the undefined behavior since all the fields of the struct were set in that function, but it's still poor form). There are some things which you will need to massage, but I found in general that it wasn't much. I had a problem with null function pointer creation that I need to go back and look at. I think my biggest issue, is that every conversion produces unsafe code in Rust. It's left up to the developer to figure out how to convert it to safe code. I think this is ok, but unsafe is still dark arts for me in Rust (meaning that I don't completely grok the best practices right now). I need to go back and reread the Rustonomicon: https://doc.rust-lang.org/nightly/nomicon/ https://doc.rust-lang.org/nightly/nomicon/ , as there may be more details there than the last time I checked it out. I would love it if corrode could try and produce more safe blocks of code, but I also understand why it's this way. Anyway, I think it's an awesome tool, works 95% of the time, and you can probably easily come up with some standard techniques for going back to fix up the commonly incorrect cases. I'll reiterate this: integrate the Rust code into your C build right at the beginning (I used Cargo + staticlib), I even made this optional in my setup, so that you can flip between the two implementations easily for testing. I set up a make recipe which converts the C src to Rust with corrode (to capture all the CFLAGS needed), and that will only run as long as the .rs file doesn't exist. I found this to be very powerful for verification. I'd highly recommend corrode at this point, but don't be misled to believe that you won't need to make changes to the produced code. Edit: I should mention that to get all this setup, with all the C library fixes in macOS included and the Makefile integration, it took me 6 hours to get setup, with about 10-15 minutes spent actually making the produced .rs file compile.
- catnaroek 10y ago> A language like Java would have the reliability angle covered but is hard to make performant. ConcurrentModificationException.
- the8472 10y agoThat is a runtime check similar to RefCell::borrow() panic if you try to read from it while something else is mutating it.
- catnaroek 10y agoWith two important differences: (0) RefCell can't be used in a multithreaded context. You have to use an actual Mutex. (1) ConcurrentModificationException isn't reliable. It's thrown on a best-effort basis. In any case, in a high-level language, just memory-safety is a very cheap guarantee. You also want to protect the internal invariants of your data structures - especially the concurrent ones!
- shmerl 10y agoThis looks interesting, though I can't find much info about inheritance and code reuse there, which should be a major topic I suppose? Who is the author of this book?
- Manishearth 10y agoThe book is incomplete. Porting an inheritance model to Rust is ... interesting. You can emulate it with some macros, but usually you should redesign the idioms used based on the exact use.
- shmerl 10y agoYes, I'm not talking about porting things 1:1. I'm talking about explaining OOP approach that Rust suggests, and may be examples of how this methodology can be applied to solve issues like code reuse, that C++ at times solves with inheritance and etc.
- static_noise 10y agoTODO: write the rest of the comment.
- Kenji 10y agoI bet most people just read the first page of this guide and thought "yeah, I should upvote that, I'm gonna read it later" and don't realize that the article is full of gaping holes and TODOs which really devalues the entire work. I started reading it seriously and as the TODOs came in masses, I was disappointed and put it away.
- jeffdavis 10y agoI'd really like to see some guides for using rust incrementally in a large C or C++ codebase. * What are some good ways to deal with a lot of C macros and C/C++ header files in general. * What is a good build strategy for integrating with a big, say, cmake build system? * How to choose APIs, other than always using the lowest common denominator (C) for everything? Put another way, how do you choose which parts to write in rust first so that you don't end up with rust code that looks like it's translated from C? * How to work with other memory allocation strategies, like a region allocator (like postgres memory contexts)?
- Animats 10y agoThe guide really isn't written yet. After the introductory material, it's mostly a list of 'todo' items. [1] The "corrode" translator apparently just compiles C to unmaintainable, unsafe Rust. Raw pointers in C become unsafe raw pointers in Rust. That's probably not too useful. See [2]. Now you have heavy technical debt. Also, losing all the comments during translation is cruel to later maintenance programmers. Good inter-language translation for programming languages is very hard. Usually, you lose the idioms of both the source language and the target language. There are few, if any, good systems for this. To do this well, you need verification-like analysis of the source program. You need to know if a pointer can ever be null. You need to know how big a passed array is. You need to know if something has single ownership. If an analyzer could look at a C program and answer "yes", "no", or "don't know" to those questions, with a reasonably low percentage of "don't knows", then you could do a translation to proper Rust. You could brute-force some of this by running the Rust compiler from the translator. Start out with everything being a single-owner reference, and compile. Anything that generates a borrow checker error gets turned into a reference counted item on the next try. [1] https://locka99.gitbooks.io/a-guide-to-porting-c-to-rust/content/porting-code.html https://locka99.gitbooks.io/a-guide-to-porting-c-to-rust/con... [2] https://news.ycombinator.com/item?id=12056230 https://news.ycombinator.com/item?id=12056230
- Manishearth 10y ago> The "corrode" translator apparently just compiles C to unmaintainable, unsafe Rust. The point is for it to be a first step in conversion, with large swathes of code being easy to make safe. The issue with idioms is a real one. But still, it gives you a good path for incremental conversion which IMO is pretty valuable.
- Animats 10y agoIt's too bad there aren't more Corrode examples on line. But from what's available, it just passes C raw pointers through function calls as unsafe Rust raw pointers. The interfaces generated are no better than they were in C. You can't rewrite one side or the other and get safety, because the interface itself is unsafe. That's no good. This isn't a syntax problem. It's a semantic problem.