Dev Diary 09/11/2018
Had my first fights with the borrow checker for this project. State machines that produce some sort of result are annoying in rust: The functions to advance them work by taking mutable references. But when they are done, you want to return the result you have been accumulating by value. The workaround involves putting the accumulating result into an Option
, so you can take
it at the end. This introduces a bit of runtime overhead and makes the code noisier, but it satisfies the borrow checker.
Technically, the borrow checker is correct, because the type system does not express that the state machine is never used again once the result has been produced. But still, in C you could just write the straightforward code and everything still works as long as the state machine is used correctly. And it is not like the Option
-workaround would prevent incorrect usage, it just panics at runtime.
The whole bpmux/rel implementation will consist of composing state machines, so I'll just have to accept this.
- resumable encoding/decoding for length-delimited bytes: https://github.com/AljoschaMeyer/varu64-rs/blob/master/src/nb.rs#L222