Skip to content
Closed
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.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
14 changes: 14 additions & 0 deletions tests/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use rand::{thread_rng, Rng};

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

#[test]
fn serde() {
let mut rng = thread_rng();
let bitset: BitSet = (0..BitSet::max_capacity())
.filter(|_| rng.gen())
.collect();
let mut buffer = Vec::new();
bitset.serialize(&mut buffer).unwrap();
assert_eq!(bitset, BitSet::deserialize(&mut buffer.as_slice()).unwrap());
}
Loading