I’m very new to rust, just working my way through the Book and trying out various small’ish examples trying to get impression of the language.
But I couldn’t help to notice some of the surprisingly inefficient or even strange implementations in std from the point of view of C++ person. Makes me wonder if there is still library modernisation talks happening and whether I should try to contribute
Few of such the examples I noticed today are composing errors types and std::time::Duration/Instant.
Current Duration/Instant implementation seems plain absurd. Split second/nanos storage makes it heavier than it needs to be, plus introduces nontrivial overhead on every operation for normalisation. Duration also seems to be defined as positive only, which sounds like WTF? And then std::sys::unix::time::Instant has completely different representation, which makes interop even more expensive.
Compared to C++ chrono implementation, where duration is just int64_t type-tagged with precision. And time_point is yet again duration with type-tagged epoch. This makes all (most) operations extremely cheap and efficient. And on top of it all durations easily and efficiently generalise to calendar manipulation, as can be seen in https://github.com/HowardHinnant/date.
Another example is propagating errors. From what I seen so far in the book recommended way is to re-wrap multiple errors on library boundary into composite enum, and add impl Error for it. But this also looks quite a bit more labour-intensive and less generic than C++ std::system_error (Guess I should try implementing it in rust some day, it might work here even better than in c++).
Some of the benefits of C++ system_error:
- type is fixed size & lightweight: 2 words
- It is polymorphic in underlying error type, we store actual error code + pointer to static object corresponding to category.
- It’s super-cheap to test for concrete errors – just compare actual code value & category ptr directly.
- allows to check for more generic “error conditions” by dynamically mapping codes. But this is more expensive check.