by proto_lambda · 4y ago · 0 comments
split_array will eventually allow something like the following, which avoids any explicit indexing and will fail to compile if the sizes don't match up:
struct IVec2(i32, i32);
impl IVec2 {
fn parse(b: [u8; 8]) -> Self {
let (first, second) = b.split_array();
IVec2(i32::from_le_bytes(first),
i32::from_le_bytes(second))
}
}Well, here's the thing. These small pieces of code DO show up in actual, real codebases! I've gotten feedback during code reviews along the lines of, "What if the surrounding context is refactored, this tiny, simple piece code could break" and to be perfectly honest, when I get it, I come down to your desk and we have a discussion about whether that kind of feedback is appropriate, and the purpose of code reviews.
(It also always seems like the issue I'm looking at is "fixed in nightly", but some of those features in nightly take a long time to get accepted, and the ergonomics of split_array() seem a bit dubious to me. Are you going to chain three split_array() for four fields? Obviously, for simple serialization and deserialization there are a ton of different options to automate this code away, but it's nice to be able to write simple code like this when appropriate, and the ergonomics of simple tasks matter.)
fn high_word(n: u32) -> u16 {
(n >> 16).try_into().unwrap()
}
Unless someone manages to solve the halting problem, we'll always need unwrap() or equivalent.See: https://play.rust-lang.org/?version=nightly&mode=release&edi...
Where if you look at the asm for playground::high_word then you'll see it's a bare shrl.
I'd quite like an analogue to unwrap() that's checked at compile time -- an out for the type checker, but an error if it's not dead code eliminated. This helps us to trust and rely on the compiler, and is the converse of `unsafe_unwrap`, which tells the compiler to trust us.
There is also this thing called "Infallible". Which will eventually be merged with "!". To express to the compiler and the programmer that something can never fail.
I’d be thrilled if C++ required wrapping declaration and use of global variables with ‘unsafe_and_evil { … }’
But it leads to just a TON of features and approaches to learn.