Writing a JPEG Decoder in Rust – Part 2: Implementation I
1–10 of 86 posts
Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#2 while i Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#3 let codes: Vec = data_table.iter()
.zip(code_lengths.iter())
.zip(code_table.iter())
.map(|((&value, &length), &code)| {
HuffmanCode {
length: length,
code: code,
value: value,
}
})
.collect();
Nice!Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#4I don't know rust, but what happens if you get a 0xff as the last byte here, wouldn't i+1 go out of array bounds? while i
Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#5I don't know rust, but what happens if you get a 0xff as the last byte here, wouldn't i+1 go out of array bounds? while i
The syntax "vec[i+1]" means "I expect this index to be in bounds, and if it's not, my program is so hopelessly broken it should be shut down immediately."
If you're really confident in your code and you need to wring out the last bit of performance, you could also use:
unsafe { vec.get_unchecked(i+1) }
...but in this case, your code would produce undefined behavior. If you want to detect this case and recover from it, you could also write: if let Some(value) = vec.get(i+1) {
// Use value.
} else {
// Fail with an error.
}
...or you could immediately exit the current routine with an error if no value is available: let value = try!(vec.get(i+1).ok_or(make_an_error()));
If you're writing a lot of parser code, your best bet might be to define a custom macro named that looks something like "try_get!(vec, i+1)" wrapping the final example above.Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#6Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#7Rust code looks very similar to ES6 or Typescript. The great CS language convergence has begun!
Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#8Rust code looks very similar to ES6 or Typescript. The great CS language convergence has begun!
Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#9this is a really elegant way to take parallel arrays and put them into a table: let codes: Vec = data_table.iter() .zip(code_lengths.iter()) .zip(code_table.iter()) .map(|((&value, &length), &code)| { HuffmanCode { length: length, code: code, value: value, } }) .collect(); Nice!
(mapv HuffmanCode. code_lengths code_table data_table)
I don't know if even dependent types would allow for that, given the (variable number of) arguments are all different types.Re: Writing a JPEG Decoder in Rust – Part 2: Implementation I
#10Rust code looks very similar to ES6 or Typescript. The great CS language convergence has begun!
Yes: now we all forget how to correctly manage memory. Rust: where resources are unlimited and aborting when you run out of them is okay
I'd change "Rust" to "safe programming" here, but OK.
There have been lots of fun security vulnerabilities coming from programs trying to handle OOM gracefully. e.g. SpiderMonkey: https://bugzilla.mozilla.org/show_bug.cgi?id=982957, https://bugzilla.mozilla.org/show_bug.cgi?id=730415