I've been reviewing the code in portable.rs and I believe there's an opportunity for some refactoring to improve code readability and maintainability, specifically around the bitwise operations.
|
let size_mod4 = bytes.len() & 3; |
- Use Core Ops for Explicit Bitwise Operations: Leveraging core::ops::BitAnd can make the operations more explicit and easier to understand. For example:
use core::ops::BitAnd;
let size_mod4 = bytes.len().bitand(3);
let remainder_jump = bytes.len().bitand(!3);
- change the operands to better reflect their bitwise nature
const TWO_LSB_MASK: usize = 0x0000_0000_0000_0003;
let size_mod4 = bytes.len().bitand(TWO_LSB_MASK);
let remainder_jump = bytes.len().bitand(!TWO_LSB_MASK);
There is precedent for 2. within this code, for instance
|
let a3 = a3_unmasked & 0x3FFF_FFFF_FFFF_FFFF; |
(name could be LOW_TWO_BITS_MASK, BIT_MASK_3, !TWO_LSB_MASK could be TWO_LSB_MASK_NOT etc)
- Standardize caps for hex representation
line 136 is all caps, line 166 is no caps etc
If you agree atleast partially, let me know which of the above options seem most sensible to you.
If you leave it up to me, I would likely first introduce the core::ops
and introduce consts for only those bitmasks used more than once. I'll prepare a PR upon that, and you could provide further critique on what seems the best readable code
Of course, one could be chosen without the other, or maybe the current code is as readable, understandable and maintainable in your view. Or, this implementation might look the way it does because the original looks that way and you'd like to keep parity. I haven't personally looked into the C/C++ version to have any strong feelings about that.
In anycase, if you'd like the style to stay the way it does, I will look into other avenues of improvement.