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 Jul 19, 2022
1 parent 2f5d62d commit 66fefda
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 56 deletions.
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ license = "MIT/Apache-2.0"
bytemuck = "1.5.1"
byteorder = "1.0"
retain_mut = "0.1.2"
serde = "1.0.139"
serde = { version = "1.0.139", optional = true }

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

[dev-dependencies]
quickcheck = "0.9"
Expand Down
57 changes: 2 additions & 55 deletions src/bitmap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,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;
Expand All @@ -41,52 +37,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
Expand Up @@ -10,6 +10,8 @@ mod cmp;
mod inherent;
mod iter;
mod ops;
#[cfg(feature = "serde")]
mod serde;
mod serialization;

pub use self::iter::{IntoIter, Iter};
Expand Down
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 66fefda

Please sign in to comment.