Skip to content

Commit

Permalink
hide serde behind a feature flag + add support for the treemap
Browse files Browse the repository at this point in the history
  • Loading branch information
irevoire committed Aug 31, 2022
1 parent b3c4b3f commit 7adf7e2
Showing 5 changed files with 122 additions and 55 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -19,9 +19,11 @@ license = "MIT OR Apache-2.0"
bytemuck = "1.7.3"
byteorder = "1.4.3"
retain_mut = "=0.1.7"
serde = { version = "1.0.139", optional = true }

[features]
simd = []
serde = ["dep:serde"]

[dev-dependencies]
proptest = { version = "1.0.0" }
57 changes: 2 additions & 55 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
@@ -12,14 +12,10 @@ mod cmp;
mod inherent;
mod iter;
mod ops;
#[cfg(feature = "serde")]
mod serde;
mod serialization;

use serde::de::SeqAccess;
use serde::de::Visitor;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;

use self::cmp::Pairs;
pub use self::iter::IntoIter;
pub use self::iter::Iter;
@@ -44,52 +40,3 @@ pub use self::iter::Iter;
pub struct RoaringBitmap {
containers: Vec<container::Container>,
}

impl<'de> Deserialize<'de> for RoaringBitmap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct BitmapVisitor;

impl<'de> Visitor<'de> for BitmapVisitor {
type Value = RoaringBitmap;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("roaring bitmap")
}

fn visit_bytes<E>(self, bytes: &[u8]) -> Result<RoaringBitmap, E>
where
E: serde::de::Error,
{
RoaringBitmap::deserialize_from(bytes).map_err(serde::de::Error::custom)
}

fn visit_seq<A>(self, mut seq: A) -> Result<RoaringBitmap, A::Error>
where
A: SeqAccess<'de>,
{
let mut bytes: Vec<u8> = Vec::new();
while let Some(el) = seq.next_element()? {
bytes.push(el);
}
RoaringBitmap::deserialize_from(&*bytes).map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_bytes(BitmapVisitor)
}
}

impl Serialize for RoaringBitmap {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut buf = Vec::new();
self.serialize_into(&mut buf).map_err(serde::ser::Error::custom)?;

serializer.serialize_bytes(&buf)
}
}
58 changes: 58 additions & 0 deletions src/bitmap/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::de::SeqAccess;
use serde::de::Visitor;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;

use crate::RoaringBitmap;

impl<'de> Deserialize<'de> for RoaringBitmap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct BitmapVisitor;

impl<'de> Visitor<'de> for BitmapVisitor {
type Value = RoaringBitmap;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("roaring bitmap")
}

fn visit_bytes<E>(self, bytes: &[u8]) -> Result<RoaringBitmap, E>
where
E: serde::de::Error,
{
RoaringBitmap::deserialize_from(bytes).map_err(serde::de::Error::custom)
}

// in some case bytes will be serialized as a sequence thus we need to accept both
// even if it means non optimal performance
fn visit_seq<A>(self, mut seq: A) -> Result<RoaringBitmap, A::Error>
where
A: SeqAccess<'de>,
{
let mut bytes: Vec<u8> = Vec::new();
while let Some(el) = seq.next_element()? {
bytes.push(el);
}
RoaringBitmap::deserialize_from(&*bytes).map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_bytes(BitmapVisitor)
}
}

impl Serialize for RoaringBitmap {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut buf = Vec::new();
self.serialize_into(&mut buf).map_err(serde::ser::Error::custom)?;

serializer.serialize_bytes(&buf)
}
}
2 changes: 2 additions & 0 deletions src/treemap/mod.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,8 @@ mod cmp;
mod inherent;
mod iter;
mod ops;
#[cfg(feature = "serde")]
mod serde;
mod serialization;

pub use self::iter::{IntoIter, Iter};
58 changes: 58 additions & 0 deletions src/treemap/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use serde::de::SeqAccess;
use serde::de::Visitor;
use serde::Deserialize;
use serde::Deserializer;
use serde::Serialize;

use crate::RoaringTreemap;

impl<'de> Deserialize<'de> for RoaringTreemap {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct TreemapVisitor;

impl<'de> Visitor<'de> for TreemapVisitor {
type Value = RoaringTreemap;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("roaring bitmap")
}

fn visit_bytes<E>(self, bytes: &[u8]) -> Result<RoaringTreemap, E>
where
E: serde::de::Error,
{
RoaringTreemap::deserialize_from(bytes).map_err(serde::de::Error::custom)
}

// in some case bytes will be serialized as a sequence thus we need to accept both
// even if it means non optimal performance
fn visit_seq<A>(self, mut seq: A) -> Result<RoaringTreemap, A::Error>
where
A: SeqAccess<'de>,
{
let mut bytes: Vec<u8> = Vec::new();
while let Some(el) = seq.next_element()? {
bytes.push(el);
}
RoaringTreemap::deserialize_from(&*bytes).map_err(serde::de::Error::custom)
}
}

deserializer.deserialize_bytes(TreemapVisitor)
}
}

impl Serialize for RoaringTreemap {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut buf = Vec::new();
self.serialize_into(&mut buf).map_err(serde::ser::Error::custom)?;

serializer.serialize_bytes(&buf)
}
}

0 comments on commit 7adf7e2

Please sign in to comment.