Bit twiddling pre-RFC

I do like these ideas, but things like simplifying masks certainly feel like they don’t need to be in std. (And could go well with a variety of other bit-focused things like bit vectors or whatever.)

That said, I would like to see a trait that’s sortof a mix of this Truncate and FromBits from pre-RFC FromBits/IntoBits, since one of my longstanding wishes is to obviate as, and a key part of that is getting the [iu]N <-> [iu]M conversions into something that makes the truncation/reinterpretation/signchange/etc explicit.

Some random API thoughts:

  • The existing “what bit” APIs take u32, so this probably should as well (not usize)
  • When I see things like “(inclusive)” in a doc comment I think “why is this not a range?”
    • In fact, RangeArgument<u32> seems perfect, so you can have u32::mask(10..), u8::mask(3..=5), etc
  • I first thought set would set it to 1, but it turned out to be more what I think of as assign
  • That set_bits example call makes my eyes glaze over a bit.
    • It feels like there might be a nice fluent-style API that produces proxies of some sort, like a.bits(16..24).copy_from(b);
1 Like