Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into alloy
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsproul committed Aug 15, 2024
2 parents d107b54 + 5ba47dc commit da7f76a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
- name: Check code coverage with cargo-tarpaulin
run: cargo-tarpaulin --workspace --all-features --out xml
- name: Upload to codecov.io
uses: codecov/codecov-action@v2
uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true
token: ${{ secrets.CODECOV_TOKEN }}
35 changes: 35 additions & 0 deletions src/bitfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,24 @@ impl<N: Unsigned + Clone> Bitfield<Variable<N>> {
pub fn is_subset(&self, other: &Self) -> bool {
self.difference(other).is_zero()
}

/// Returns a new BitList of length M, with the same bits set as `self`.
pub fn resize<M: Unsigned + Clone>(&self) -> Result<Bitfield<Variable<M>>, Error> {
if N::to_usize() > M::to_usize() {
return Err(Error::InvalidByteCount {
given: M::to_usize(),
expected: N::to_usize() + 1,
});
}

let mut resized = Bitfield::<Variable<M>>::with_capacity(M::to_usize())?;

for (i, bit) in self.iter().enumerate() {
resized.set(i, bit)?;
}

Ok(resized)
}
}

impl<N: Unsigned + Clone> Bitfield<Fixed<N>> {
Expand Down Expand Up @@ -1403,4 +1421,21 @@ mod bitlist {
fn size_of() {
assert_eq!(std::mem::size_of::<BitList1024>(), SMALLVEC_LEN + 24);
}

#[test]
fn resize() {
let mut bit_list = BitList1::with_capacity(1).unwrap();
bit_list.set(0, true).unwrap();
assert_eq!(bit_list.len(), 1);
assert_eq!(bit_list.num_set_bits(), 1);
assert_eq!(bit_list.highest_set_bit().unwrap(), 0);

let resized_bit_list = bit_list.resize::<typenum::U1024>().unwrap();
assert_eq!(resized_bit_list.len(), 1024);
assert_eq!(resized_bit_list.num_set_bits(), 1);
assert_eq!(resized_bit_list.highest_set_bit().unwrap(), 0);

// Can't extend a BitList to a smaller BitList
resized_bit_list.resize::<typenum::U16>().unwrap_err();
}
}
2 changes: 1 addition & 1 deletion src/fixed_vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub use typenum;
/// assert_eq!(&long[..], &[1, 2, 3, 4, 0]);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Derivative)]
#[derivative(PartialEq, Hash(bound = "T: std::hash::Hash"))]
#[derivative(PartialEq, Eq, Hash(bound = "T: std::hash::Hash"))]
#[serde(transparent)]
pub struct FixedVector<T, N> {
vec: Vec<T>,
Expand Down

0 comments on commit da7f76a

Please sign in to comment.