Live data from Hacker News

How to rewrite it in Rust

adventures.michaelfbryan.com

11–20 of 43 posts

Re: How to rewrite it in Rust

#11
post #2

This ability to incrementally add Rust to a C codebase is very useful for adopting Rust in established projects. You don't actually have to rewrite everything on day one. You can stop writing new C code right now, and then gradually replace old code only when you need to fix it or refactor it.

Is there any way to do this the other way around and integrate new rust code into non-trivial C project with non-trivial build system?

Re: How to rewrite it in Rust

#12
post #11
post #2

This ability to incrementally add Rust to a C codebase is very useful for adopting Rust in established projects. You don't actually have to rewrite everything on day one. You can stop writing new C code right now, and then gradually replace old code only when you need to fix it or refactor it.

Is there any way to do this the other way around and integrate new rust code into non-trivial C project with non-trivial build system?

Yep, it sort of looks the same. This is how Rust entered Firefox, for example. The most straightforward way is to call `cargo build` from within whatever build system you're using for C.

Re: How to rewrite it in Rust

#13
post #9

We did a similar thing with a Scala -> Rust rewrite for the http://prisma.io query engine. By rewriting small components and integrating them into the existing project using Javas native interface, our small team of 5 developers were able to pull off this massive rewrite in just under a year. The resulting code base is rearchitected in a few very important ways, but mostly follows the same structure. And because we k…

I promise that this is asked genuinely and isn't some sort of veiled "gotcha!" (it's tough to tell on the internet sometimes); what was the reason for a change from Scala to Rust?

I ask because Scala already has a good type system and the JVM typically has good performance nowadays, particularly with something like GraalVM, so I am actually really curious to why you felt a Rust rewrite was a good idea.

Re: How to rewrite it in Rust

#15
post #6
post #2

This ability to incrementally add Rust to a C codebase is very useful for adopting Rust in established projects. You don't actually have to rewrite everything on day one. You can stop writing new C code right now, and then gradually replace old code only when you need to fix it or refactor it.

Twice I've been part of a move from Javascript to Typescript that worked much the same. Both projects were large applications with several developers working on them. Both had been ongoing for a few years before the port started. In both projects we decided to write all new code in Typescript and convert JS to TS when we made any largish change to an existing JS file. In both cases it took around a year for us to hit…

I did something similar for a large-ish C# project and F#. The company I worked for was mostly an F# place, but we had a fairly large legacy codebase written in C#; typically we had the pattern of "if you had downtime or need to fix a bug in the C#, just rewrite the C# code into F#".

Annoyingly, at least with the typical msbuild pattern, you have to be using the same language at the project level, but you can have as many projects as you want per solution (it's weird). So it's not as seamless as the JS/TS system, but overall it's not too bad, since you still can mix and match somewhat.

Re: How to rewrite it in Rust

#16
post #9

We did a similar thing with a Scala -> Rust rewrite for the http://prisma.io query engine. By rewriting small components and integrating them into the existing project using Javas native interface, our small team of 5 developers were able to pull off this massive rewrite in just under a year. The resulting code base is rearchitected in a few very important ways, but mostly follows the same structure. And because we k…

Do you have any write-ups on this? I might be looking to do this for a large java codebase soon.

Re: How to rewrite it in Rust

#18
> This is where build scripts come in. Our strategy will be for the Rust crate to use a build.rs build script and the cc crate to invoke the equivalent commands to our make invocation

Yikes - port the entire build system to cargo before you write a line of Rust. Now draw the reset of the owl!

Surely's there's an incremental path for the build as well? Perhaps if you're using CMake?

Re: How to rewrite it in Rust

#19
post #6

Earlier quoted context omitted.

Twice I've been part of a move from Javascript to Typescript that worked much the same. Both projects were large applications with several developers working on them. Both had been ongoing for a few years before the port started. In both projects we decided to write all new code in Typescript and convert JS to TS when we made any largish change to an existing JS file. In both cases it took around a year for us to hit…

> When converting C to Rust you have to do things on a module by module or compile artifact by compile artifact basis, which makes it much more challenging. OP is essentially about proving the opposite. It does take a bit of setup to get there, but you can ultimately translate C to Rust on a function-by-function basis, and Rustify interfaces, data structures, etc. only gradually after nothing on the C side is relying…

> you need to forgo quite a few C++-exclusive features to end up with interfaces that Rust can work with

Luckily, many C++ projects do this already so they can be called from C.

Re: How to rewrite it in Rust

#20
> However, Rust has a killer feature when it comes to this sort of thing. It can call into C code with no overhead (i.e. the runtime doesn’t need to inject automatic marshalling like C#’s P/Invoke) and it can expose functions which can be consumed by C just like any other C function.

As we see below, you may still need to write some code to convert from C types to something that is more ergonomic to use in Rust. But the marshaling ABI-wise is minimal.

> Turns out the original tinyvm will crash for some reason when you have multiple layers of includes

It's actually crashing because there are no lines of code in the file, so certain data structures never get initialized.

Post reply on HN