News for hackers. Stuff that matters.
Re:The Git source code audit, viewed as a Rust programmer by saghm on Tuesday January 24, 2023 @12:21PM (#34506730)
> So you often need to index something based on an u32 (or something) and then there's a cast. How do avoid it?

I imagine the nuance here is in the "or something", since that could imply something signed or larger than 32 bits, but for that specific case, I feel like a large amount of code that people write nowadays can be comfortably assumed not to need to run on 16-bit processors. Although `usize `only implements `TryFrom<u32>` and not `From<u32>`, even the standard library documentation for Rust simplifies things by saying that `usize` will be either 4 or 8 bytes (https://doc.rust-lang.org/std/primitive.usize.html), although I understand this isn't a hard contract and could change in the future. I guess it depends a bit on the use case, but I think that most of the time just using `try_from` and panicking with an appropriate error message on error would be fine, and if you want to be extra sure, you can always use something like `const_assert!` (https://docs.rs/static_assertions/1.1.0/static_assertions/ma...) with the `BITS` constant (https://doc.rust-lang.org/std/primitive.usize.html#associate...) to catch it at compile time to be extra certain.

Re:The Git source code audit, viewed as a Rust programmer by nextaccountic on Tuesday January 24, 2023 @12:48PM (#34507175)
Oh, u32 is generally fine exactly because 16 bits is so niche. But you still have a cast between u32 and usize (and .into() isn't that much better). Casts or conversions on each collection access makes code hard to read

So if your application can work with u32 everywhere (on either 32 bits or 64 bits platforms), you should use u32 and impl Index<u32> on your collection, to avoid the cast.

It's only if you need the less common use case of u32 on 32 bits platforms, u64 in 64 bits platforms that you should bother with usize

Re:The Git source code audit, viewed as a Rust programmer by phkahler on Tuesday January 24, 2023 @01:39PM (#34507973)
>> So if your application can work with u32 everywhere (on either 32 bits or 64 bits platforms), you should use u32 and impl Index<u32> on your collection, to avoid the cast.

This seems strange. Why does Rust not automatically use whatever size index you hand it?

Re:The Git source code audit, viewed as a Rust programmer by db48x on Tuesday January 24, 2023 @02:12PM (#34508546)
Ultimately it is because the Rust developers decided that there would be no implicit conversions. In a lot of cases implicit conversions are convenient, but many people feel that the complexity just isn’t worth it.
Re:The Git source code audit, viewed as a Rust programmer by Sharlin on Tuesday January 24, 2023 @03:30PM (#34509836)
It’s not out of the question that additional `Index` impls for other integer types could be added to the standard library at some point; the issue is discussed often enough. But it’s not trivial to do because some currently valid code could become ambiguous if impls are added, so some sort of a transition would be required.