Hi, I'm not very fluent in Rust.
I'm also told that "new users can only put 2 links in a post", so I'll write links like [L0] which means to look up "L0" in Go Playground - The Go Programming Language which I'm using as a pastebin. Sorry.
I recently released Puffs [L0], which is not about Rust per se, but the /r/rust sub-reddit discussed Puffs at [L1] and Rust also came up often on Hacker News' Puffs discussion at [L2]
One discussion point was about implicit bounds checking and the efficiency of an array indexing expression like "a[i]". Y'all don't need telling that, in Rust, it's more idiomatic to work with iterators instead of indexing expressions. Combined with a for loop, array bounds checks can be eliminated entirely, within the loop body.
Still, it's not obvious to me how the Rust equivalent of my Puffs code at [L3] would be able to eliminate all of the array or slice bounds checks. I'm using "bounds checks" here to also include an iterator hitting the end of the slice.
Here's a brief aside about Puffs syntax. It is a curly-brace, C like language, with a few idiosyncracies. These aren't really relevant to my questions (below), but some of you might be curious.
There are "assert etc" statements in that file. Puffs assertions are entirely compile time concepts, they do not have a runtime representation. There is also no notion in Puffs of debug and release modes.
Some method calls have a question mark: "foo?(arguments)". Roughly speaking, the question mark indicates a call (usually involving I/O) that can result in a co-routine suspension (e.g. "my write buffer is full" or "my read buffer is empty"). The arguments also have to be explicitly named: "foo?(name0:val0, name1:val1)" and not "foo?(val0, val1)".
Integer types can be refined: "var x u32[..511]" means that x is a 32-bit integer that is no larger than 511.
Back to the main topic... I am iterating, but I'm not consuming exactly one byte per iteration. Each iteration (of the "while:loop" outer loop) consumes a variable number of bits (because that's how Huffman coding works), and if there aren't enough bits held in local variables ("bits" and "n_bits"), then more are pulled from the underlying source of byte data (in my code, called "in.src"). To repeat, each iteration consumes a variable number of bits, and depending on "n_bits"'s ebb and flow, therefore consumes a variable number of bytes.
I'm happy to be proven wrong, but I believe that that means that I couldn't use the "for val in bytes_iter { etc }" mechanism if I tried to write this in Rust.
Instead, IIUC, every time I (conditionally) read the next byte (in Puffs, a "read_u8?" call, which happens at up to 12 places during that outer loop), the equivalent Rust code would be calling an Iterator<u8>'s next method. Sorry if I'm mangling Rust syntax or terminology. As I said, I'm not fluent in Rust.
The thing is that the callee would have to make a bounds check (comparing to the end of the slice), and the caller would have to handle both the None and Some cases of the Option returned, right? The compiler can optimize the two if-checks (one callee, one caller) into one, but the compiler can't optimize the one to zero, right?
In Puffs, runtime bounds checks can be entirely eliminated because the compile time assertion mechanism propagates concepts like "starting with at least 12 bytes of data available in my read buffer, and conditionally reading 2 of those bytes, there is guaranteed to be at least 10 bytes of data available, regardless of which if-branch was taken". Is there a similar compile time concept in Rust that could reduce that one conditional to zero (within the loop body)?
There was also some talk of dependent types in those Hacker News / Reddit discussions. IIUC there are proposals for dependent types in Rust, but they're still proposals and not yet confirmed to be in Rust? I am not an expert on dependent types, but if there is an expert here that can show how those language proposals would also solve my problem (getting to zero runtime bounds checks), I'm happy to listen.