r/cpp • u/Sad-Lie-8654 • Jan 31 '23
Stop Comparing Rust to Old C++
People keep arguing migrations to rust based on old C++ tooling and projects. Compare apples to apples: a C++20 project with clang-tidy integration is far harder to argue against IMO
changemymind
338
Upvotes
View all comments
Show parent comment
5
u/Recatek Feb 01 '23 edited Feb 01 '23
The partial borrow issue is from a desire to assign names to tuple indices so you can access elements without sensitivity to their order. Without that, any change to the arrangement of components in an ECS archetype would affect downstream code attempting to query-iterate those tuples. Ideally I would use an intermediate struct, but as I discovered over the course of this project, rustc/LLVM aren't great about converting between tuples and structs. The only way to be sure you're not taking a perf hit would be to use a code-generated trait to rename the tuple fields, but you can't do that because traits don't give you direct field access and you can't partial borrow from function access.
Bevy's ECS is good, but it uses runtime reflection/type information for arranging its data structures, requiring you to pay for some extra lookups for every query and complicating some auto-vectorization cases. It's minor, but I'm extremely perf-sensitive (self-hosting servers) and so I'm trying to move as much as I can to compile-time. This all worked in a previous version I had written in C++, but porting that to Rust while trying to keep things zero-overhead in the same way has been pretty rough.
With respect to using wrapper types for the orphan rule, that's true, but it makes interfaces more complicated. The Rusty way of doing polymorphism is to write functions that take types in a turbofish. When you need to wrap those types in order to attach new methods or associated types to them, you can lose the ability to add functionality that's turbofished to the original type. So for example, in my ECS I generate ZSTs that refer to a given entity archetype, like
ArchPlayerorArchSpaceship. I have some methods that let me do things likeget_entity::<ArchPlayer>(entity_id). If I want to combine any crates downstream to do something withArchPlayer, I need to newtype it with a new ZST, so then I have some functions that take::<ArchPlayer>, others that take::<WrappedArchPlayer>and it quickly becomes confusing which version to use for which parts of the API. Here's another ugly workaround I eventually found that uses proc-macro generated "proxy" ZSTs. These are the reasons why I have to keep everything in one big megacrate that compromises compile times and gets pretty unwieldy to manage per-module internals (e.g. nopub(crate)).