Skip to content

Commit

Permalink
Merge pull request #15 from integer32llc/serde
Browse files Browse the repository at this point in the history
  • Loading branch information
domodwyer authored Jul 29, 2024
2 parents cd618da + 8d33d63 commit ed5603d
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/bloom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ where
H: BuildHasher,
B: Bitmap,
{
#[cfg_attr(feature = "serde", serde(skip))]
hasher: H,
bitmap: B,
key_size: FilterSize,
Expand Down Expand Up @@ -528,4 +529,29 @@ mod tests {
assert_eq!(input_maybe_contains, merged.contains(v));
}
}

#[cfg(feature = "serde")]
#[test]
fn serde() {
type MyBuildHasher = BuildHasherDefault<twox_hash::XxHash64>;

let mut bloom_filter: Bloom2<MyBuildHasher, CompressedBitmap, i32> =
BloomFilterBuilder::hasher(MyBuildHasher::default())
.size(FilterSize::KeyBytes4)
.build();

for i in 0..10 {
bloom_filter.insert(&i);
}

let encoded = serde_json::to_string(&bloom_filter).unwrap();
let decoded: Bloom2<MyBuildHasher, CompressedBitmap, i32> =
serde_json::from_str(&encoded).unwrap();

assert_eq!(bloom_filter.bitmap, decoded.bitmap);

for i in 0..10 {
assert!(decoded.contains(&i), "didn't contain {}", i);
}
}
}

0 comments on commit ed5603d

Please sign in to comment.