-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
hide serde behind a feature flag + add support for the treemap
- Loading branch information
Showing
5 changed files
with
122 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |