Skip to content

Commit

Permalink
feat(bitfield): add Pair::pack_from_{src, dst} (#294)
Browse files Browse the repository at this point in the history
This commit adds `pack_from_src` and `pack_from_dst` const fn methods to
the bitpacking `Pair{N}` types in `mycelium_bitfield`. It also changes
the `dst` type for `Pack{N}::pair_with` from `&Self` to `Self`, since
packing specs are `Copy`able.
  • Loading branch information
hawkw committed Aug 9, 2022
1 parent 781ed69 commit d1d90ff
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions bitfield/src/pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ macro_rules! make_packers {
const fn mk_mask(n: u32) -> $Bits {
if n == 0 {
return 0
};
}
let one: $Bits = 1; // lolmacros
let shift = one.wrapping_shl(n - 1);
shift | (shift.saturating_sub(1))
Expand Down Expand Up @@ -591,7 +591,7 @@ macro_rules! make_packers {
/// into another location, and vice versa.
pub const fn pair_at(&self, at: u32) -> $Pair {
let dst = $Pack::starting_at(at, self.bits());
self.pair_with(&dst.typed())
self.pair_with(dst.typed())
}

/// Returns a pair type for packing bits from the range
Expand All @@ -606,7 +606,7 @@ macro_rules! make_packers {
/// # Note
/// The two ranges must be the same size. This can be asserted
/// by the `assert_valid` method on the returned pair type.
pub const fn pair_with(&self, dst: &Self) -> $Pair {
pub const fn pair_with(&self, dst: Self) -> $Pair {
// TODO(eliza): validate that `dst.shift + self.bits() < N_BITS` in
// const fn somehow lol
let (dst_shl, dst_shr) = if dst.shift > self.shift {
Expand Down Expand Up @@ -803,6 +803,25 @@ macro_rules! make_packers {
Self(packer.pack_truncating(value, self.0))
}

/// Pack bits from `src` into `self`, using the packing pair
/// specified by `pair`, with `self` serving as the "destination" member
/// of the pair, and `src` serving as the "source" member of the
/// pair.
#[inline]
pub const fn pack_from_src(self, value: $Bits, pair: &$Pair) -> Self {
Self(pair.pack_from_src(self.0, value))
}

/// Pack bits from `dst` into `self`, using the packing pair
/// specified by `pair`, with `self` serving as the "siyrce" member
/// of the pair, and `dst` serving as the "destination" member of the
/// pair.
#[inline]
pub const fn pack_from_dst(self, value: $Bits, pair: &$Pair) -> Self {
Self(pair.pack_from_dst(value, self.0))
}


/// Pack bits from `value` into `self`, using the range
/// specified by `packer`.
///
Expand Down

0 comments on commit d1d90ff

Please sign in to comment.