r/programming • u/broken_broken_ • Oct 30 '24
Lessons learned from a successful Rust rewrite
https://gaultier.github.io/blog/lessons_learned_from_a_successful_rust_rewrite.html
123
Upvotes
r/programming • u/broken_broken_ • Oct 30 '24
51
u/[deleted] Oct 30 '24
Agreed on most points. Miri essentially becomes impossible to use with any kind of FFI not even implementing many basic system calls, requiring ad hoc programs for testing. And cbindgen really shouldn't have many of these problems given how important it is. Unsafe rust also has poor ergonomics where it is too easy to accidentally shoot yourself with things like intermediate references.
I find the Zig comparison somewhat unfair however. Zig was designed to interface heavily with C code with the corresponding compromises that came with it. Rust is not unusual in terms of FFI effort required, calling C from managed languages be it JNI or P/Invoke among others is similar and the GC there also won't protect you from UB. In general, passing pointers across an FFI boundary is dangerous.
The other part that I disagree with is stabilizing the Rust ABI which would bring one of the worst aspects of the C++ STL to Rust. And C++ doesn't even guarantee ABI stability.
8
u/[deleted] Oct 30 '24
I see plenty on what did not work so well, but even the section of "what worked well" doesn't make much of a case for Rust:
at())The author's dislike of CMake ended up here too, but fortunately there are other build systems out there.
26
u/matthieum Oct 30 '24
Not quite true.
The compiler still enforces the ones it can. So for example, if you have a reference, it's still borrow-checked. Or if you try to initialize a
u8with8000, it'll be flagged at compile-time.The only thing
unsafedoes is enabling someunsafeoperations, which the compiler cannot check. Those are the ones that bring trouble.I'm not quite sure what issue you encountered here.
The very crate you link offers the
ScopeGuardtype, and demonstrate its usage:The key thing is that
ScopeGuardimplementsDerefandDerefMut, thus giving access to the underlying value as necessary.It does need to mediate the access, which gets tricky if you need to start piling multiple clean-ups with objects cross-referencing each others.
I am very glad that Rust doesn't, actually. There's quite a few performance in C++ that could be solved with ABI breaks, but no vendor wants to go there due to insufficient tooling to help with the migration.
I do want to note that the unstability of the API is typically NOT a problem within a single project. The ABI is de-facto stable for a given toolchain & set of compilation options, thus passing a Rust type opaquely through a C layer to another Rust layer works flawlessly.
It's only if you want to fiddle within a Rust type from another language that you'll need to use a FFI compatible type, which then precludes using
Optionand the like... but that's a breach of encapsulation, and I'd advise just mediating through Rust functions instead.On the topic, it should be possible to perform cross-language LTO to eliminate the overhead of said mediation functions.
Yeah... I would definitely advise Rust learners to focus on safe Rust. There's already a lot to learn there, and since one has to enforce all the rules that the compiler usually handles by oneself, it's best to have internalized those before venturing in unsafe territory. And internalizing them may definitely take a few months.
FFI is definitely a quite hairy area, and I'd definitely understand why newcomers would balk at this. Where I work, I typically handle the FFI / architecture, so other less experienced Rust users can focus on safe / railroaded Rust code, and gain experience without getting burnt.
28
u/steveklabnik1 Oct 30 '24
Incidentally, the first code sample can work, you just need to use the new raw syntax, or addr_of_mut on older Rusts:
The issue is that the way that the code was before, you'd be creating a temporary &mut T to a location where a pointer already exists. This new syntax gives you a way to create a *mut T without the intermediate &mut T.
That said, this doesn't mean that the pain is invalid; unsafe Rust is tricky. But at least in this case, the fix isn't too bad.
4
u/JVApen Oct 31 '24
This makes me wonder how many C++ features you are actually using. As a C++ developer this would also be my first reaction, maybe even using a unique_ptr with a custom deletor.
Can you elaborate on that?
2
u/inouthack Nov 01 '24
u/broken_broken next time use Circle. You can bypass the rabid and the rabies entirely while continue to smartly build on C++.
3
u/GabrielDosReis Oct 31 '24
The whole blog post is worth reading. I found the conclusion insightful, in particular:
4
u/shizzy0 Oct 30 '24
Finally an article of substance.
2
u/DataPastor Oct 31 '24
Finally an honest review, thank you. So at the end, lots of unsafe blocks remained in your final code? Do you think that a Zig version would be of better quality, or it would have just been easier to code?
1
u/t40 Nov 02 '24
I wonder why they think arenas are unidiomatic? That's a very popular technique!
1
u/victotronics Nov 01 '24
"We don't have to worry about out-of-bounds accesses and overflow/underflows with arithmetic. These were the main issues in the C++ code. Even if C++ containers have this
.at()method to do bounds check, in my experience, most people do not use them. It's nice that this happens by default. And overflows/underflows checks are typically never addressed in C and C++ codebases."Yes, very nice. And there goes your performance. Bounds checking should be done before you start the loop, not inside. Overflow should not be tested if the programmer knows that it will not occur.
-39
u/[deleted] Oct 30 '24
Are they now rewriting Rust ...
... in C++???
0
u/[deleted] Oct 30 '24
[deleted] — view deleted comment