News for hackers. Stuff that matters.
Re:A Guide to Porting C/C++ to Rust by Animats on Sunday January 29, 2017 @01:01PM (#13515556)
The 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/con...

[2] https://news.ycombinator.com/item?id=12056230

Re:A Guide to Porting C/C++ to Rust by Manishearth on Sunday January 29, 2017 @01:43PM (#13515805)
> 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.

Re:A Guide to Porting C/C++ to Rust by Animats on Sunday January 29, 2017 @02:22PM (#13516013)
It'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.

Re:A Guide to Porting C/C++ to Rust by bluejekyll on Sunday January 29, 2017 @03:04PM (#13516253)
The interface is FFI, it has to be unsafe, because you can't guarantee anything about the C code.

The rust code though, can be converted to pure Rust. A strategy I use and most other FFI code I've seen, is to have a shim layer, which is unsafe, that calls through to safe code. And vise versa for calling out to C. The function definitions don't need to be unsafe, just the direct calls to C and conversions to C types.

Re:A Guide to Porting C/C++ to Rust by Manishearth on Sunday January 29, 2017 @05:09PM (#13517046)
> This isn't a syntax problem. It's a semantic problem.

I never said it was.

You can still make trivial semantic transformations on code by hand or by using a tool (corrode could do it itself, too) and make it safe. As bluejekyll said it's a bit tricky since you can't guarantee anything about the calls until you port those too.