←back to thread

Eurydice: a Rust to C compiler

(jonathan.protzenko.fr)
185 points todsacerdoti | 5 comments | | HN request time: 1.58s | source
Show context
BobbyTables2 ◴[] No.46179544[source]
Not saying it isn’t neat, but WHY?

Seems like the kind of thing that happens before a language is natively supported by a compiler.

replies(4): >>46179591 #>>46179642 #>>46180031 #>>46180139 #
1. procaryote ◴[] No.46180031[source]
They want to use rust but can't, because they need C compatibility, e.g. because they're providing a library or wants to support platforms that rust don't.

It's at least a less bad idea than I expected originally – I've mainly seen people try to use transpilers to sneakily start using the language they prefer as part of a larger code base, with all the obvious problems.

It's still not great as you now have an additional translation step to deal with. You will at some point need to look for bugs in the generated C layer, which will really suck. Users of the C library can't read your source to understand what it does internally without an extra step to figure out what rust code the thing they use corresponds to, etc.

If you want to provide a C library, write C. If rust isn't an option because it doesn't work for your customers who use C, don't use rust.

replies(1): >>46180592 #
2. zozbot234 ◴[] No.46180592[source]
You can write a C-compatible binary library in Rust (see the cdylib target) and cbindgen can then generate proper C header files for any C-ABI interfaces exposed in Rust code. A full Rust-to-C compiler should only be needed for targets that have some C compiler available but are just not supported by the current Rust toolchain.
replies(1): >>46180855 #
3. flohofwoe ◴[] No.46180855[source]
If you have an existing build system for your C project, you sure as hell don't want to bring another compiler toolchain (like Rust) into the mix.
replies(1): >>46181085 #
4. zozbot234 ◴[] No.46181085{3}[source]
You'd need the Rust compiler toolchain anyway, just to do the Rust-to-C conversion step instead of compiling to a binary library? What would be the point? The C ABI is quite compatible across toolchains.
replies(1): >>46181390 #
5. flohofwoe ◴[] No.46181390{4}[source]
> You'd need the Rust compiler toolchain anyway, just to do the Rust-to-C conversion step instead of compiling to a binary library?

The Rust-to-C conversion would be done by the Rust library author (or more likely, some automated CI process) to create a C source code distribution of the Rust library.