Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/bitset/serialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ where
let mut block_indices: BlockIndices = unsafe{MaybeUninit::zeroed().assume_init()};
mask.for_each_bit(|i|{
block_indices.as_mut()[i] = *index_offset;
*index_offset += Primitive::ONE;
// Allowed to overflow here, on the very last round with
// BlockIndices::Item=u8 and 256bit config.
// (root level with 256 items)
*index_offset = index_offset.wrapping_add(Primitive::ONE);
});
block_indices
};
Expand Down
6 changes: 6 additions & 0 deletions src/primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ pub trait Primitive:

fn trailing_zeros(self) -> u32;
fn wrapping_neg(self) -> Self;
fn wrapping_add(self, rhs: Self) -> Self;

fn is_zero(self) -> bool;
}
Expand Down Expand Up @@ -69,6 +70,11 @@ macro_rules! impl_primitive {
self.wrapping_neg()
}

#[inline]
fn wrapping_add(self, rhs: Self) -> Self {
self.wrapping_add(rhs)
}

#[inline]
fn is_zero(self) -> bool {
self == 0
Expand Down
8 changes: 5 additions & 3 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use rand::{thread_rng, Rng};

type BitSet = hi_sparse_bitset::BitSet<hi_sparse_bitset::config::_256bit>;

/// For issue https://github.com/tower120/hi_sparse_bitset/pull/47
#[test]
fn serde() {
#[cfg_attr(miri, ignore)]
fn regression_deserialization_256bit_arithmetic_overflow() {
type BitSet = hi_sparse_bitset::BitSet<hi_sparse_bitset::config::_256bit>;

let mut rng = thread_rng();
let bitset: BitSet = (0..BitSet::max_capacity())
.filter(|_| rng.gen())
Expand Down