"A much better alternative is to reuse the original library and just publish a safe interface to it." I think I must reject the premise a bit, unless I misunderstand how Rust works. If you write a safe wrapper around unsafe code, is it not still the case, that if the unsafe code bombs out, it will take the safe Rust down with it, engulfed in shared flames? (Unless you spawn unsafe code in its separate process or some…
How to not rewrite it in Rust
11–20 of 231 posts
Re: How to not rewrite it in Rust
#12What are examples of the unheard of things?
Re: How to not rewrite it in Rust
#13The title should be "how to not" rather than "how not to".
It really depends on your opinion on split infinitives. Both are correct.
It also isn't the wording used in the original article title.
Re: How to not rewrite it in Rust
#14> However at best, the temptation to RiiR is unproductive
> A much better alternative is to reuse the original library and just publish a safe interface to it.
Just as models are a lower dimensional representation of a more complex problem, [1] I have to re-iterate that there are no truth(ism)s in software and as in all engineering, there are trade offs. RiiR can and often is a valid choice. The author talks about "introducing bugs", under an engineering approach to a rewrite, more like a language-port, rewrites can _find_ a lot of bugs. One such way is as follows.
1. keep the same interface in the new system, client code should work against either system.
2. have a body of integration tests, capture these from the field OR write a small collection of orthogonal tests, somewhere between unit and integration.
3. use the tooling, bindgen, etc and generate as much of the interface programmatically as possible.
4. iterate on the port, doing differential testing against both systems.
Doing a language port is comparable in work to long term maintenance and refactoring of an existing codebase. As the tooling gets better, RiiR will be more of a smooth oxidization.
If you own the C/C++ that could possibly get rewritten, I'd think one needs to rationalize NOT RiiR. Better tooling (IDE, build), perf tooling is improving, low bug count, increased team velocity, build is improved, etc.
But the biggest reason to RiiR is safety. Integrating a body of C/C++ code into your rust codebase introduces a huge amount of unsafe code, much worse than surrounding your entire rust program with unsafe { }.
At least do what is outlined in the article but ALSO compile the native code into Wasm and run it from within a sandbox.
edit, something like a library for reading (parsing) a file format, should absolutely be RiiR or run from a sandbox. Data parsing and memory corruption vulnerabilities are excellent dance partners.
[1] see PCA https://en.wikipedia.org/wiki/Principal_component_analysis
Re: How to not rewrite it in Rust
#15Earlier quoted context omitted.
It really depends on your opinion on split infinitives. Both are correct.
Surely "How Not To..." is typically used when you're talking of a bad way to do something, or a way to fail - so this sounds like it's describing a bad way to rewrite something in Rust, which is not the case. It also isn't the wording used in the original article title.
I think this kind of title is trying to do exactly that, or at least, that's how I took it.
> It also isn't the wording used in the original article title.
This is a good reason to switch it, for sure. I didn't realize this, if you had led with that, I wouldn't have said anything :)
Re: How to not rewrite it in Rust
#16> This may be problematic because it’ll require all users of our chmlib crate (and their users) to have Autotools installed. Autoconf creates a portable shell script, right? No need to have it installed just to build the project.
The tree only contains configure.in, not the generated configure output, and Makefile.am, not the generated Makefile.in. You do need the entire autotools suite installed for it to build. Why they published it like this is beyond me, however. The whole point of autotools is not making your users install them (unlike CMake and virtually every other build system for C there is).
Re: How to not rewrite it in Rust
#17> This may be problematic because it’ll require all users of our chmlib crate (and their users) to have Autotools installed. Autoconf creates a portable shell script, right? No need to have it installed just to build the project.
"portable" to different Unices, right? I'm on Windows, for example.
I'm just being pedantic. It's a problem...
Re: How to not rewrite it in Rust
#18> Rust gives you the power to do things which would be unheard of (or just plain dangerous) in other languages What are examples of the unheard of things?
Re: How to not rewrite it in Rust
#19> Rust gives you the power to do things which would be unheard of (or just plain dangerous) in other languages What are examples of the unheard of things?
In another language you'd risk having a pointer to invalid memory when the function returns if the pointer escapes the function, and so it is advised to use pointers to objects on the stack sparingly if at all.
In rust, the compiler will statically determine if a pointer to an object on the stack could escape the function, and if so will fail to compile.
Re: How to not rewrite it in Rust
#20I still have issues with Rust string handling. Half of the time I have no idea how to get strings out of libraries. I don't understand the intentions of the language creators at all. Does anybody have a great intro to Rust for software engineers who come from Python or something similar where high level types are the norm?
All of Rust's complexity for strings comes out of complexity due to UTF-8. Well, a tiny bit comes from the fact that Rust has pointers too, but most of it is UTF-8. Did you happen to read the book? We cover strings early because this is a common pain point. Also, if you have something more detailed than "get strings out of libraries", I can give better advice. It's tough to tell what the actual issue is.
use sha2::{Sha256, Digest};
fn main() {
let mut hasher = Sha256::new();
hasher.input(b"hello world");
let result = hasher.result();
??
}
How do I get a string with the hash value? Which chapter of which book should I read?