Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serialization support #79

Merged
merged 19 commits into from
Apr 4, 2020
Merged
Changes from 1 commit
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
39d3761
Implemented `serde::Serialize` for `HashMapRef`
GarkGarcia Mar 27, 2020
5d0bb3c
Merge branch 'master' of https://github.com/jonhoo/flurry
GarkGarcia Mar 27, 2020
668f363
Implemented `serde::Serialize` for `HashSetRef`
GarkGarcia Mar 27, 2020
4d344a7
Implemented `serde::Serialize` for `HashMap` and `HashSet`
GarkGarcia Mar 27, 2020
70e5e73
Implemented `serde::Deserialize` for `HashMap`
GarkGarcia Mar 28, 2020
78d9e1d
Made serialization an optional feature
GarkGarcia Mar 28, 2020
fd193df
Update src/map.rs
GarkGarcia Mar 28, 2020
0863d9e
Update Cargo.toml
GarkGarcia Mar 28, 2020
907b6bb
Removed the `serialize` feature in favor of `serde`
GarkGarcia Mar 28, 2020
6942c79
Replaced `HashMap::with_capacity(0)` by `HashMap::new()` in the imple…
GarkGarcia Mar 28, 2020
6f1dec3
Made the implementation of `Deserialize` for `HashMap` generic over `…
GarkGarcia Mar 29, 2020
3f93169
Implemented `Deserialize` for `HashSet`
GarkGarcia Mar 29, 2020
0cea56c
Fixed compiletime errors
GarkGarcia Mar 29, 2020
9822de1
Optimized the implementation of `Deserialize` for `HashMap` by preall…
GarkGarcia Mar 29, 2020
99ca146
Removed the usage of the `todo!` macro
GarkGarcia Mar 31, 2020
b483438
Fixed compile time erros
GarkGarcia Mar 31, 2020
9d7aef9
Fixed the `Deserialize::expecting` implementations
GarkGarcia Mar 31, 2020
c85a482
Moved the `Serialize` and `Deserialize` implementations to a separate…
GarkGarcia Apr 2, 2020
6464ff9
Added rudimentary test of the implementations of `Serialize` and `Des…
GarkGarcia Apr 2, 2020
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
63 changes: 62 additions & 1 deletion src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ use crate::iter::*;
use crate::node::*;
use crate::raw::*;
use crossbeam_epoch::{self as epoch, Atomic, Guard, Owned, Shared};
use serde::{Serialize, Serializer};
use serde::{
de::{MapAccess, Visitor},
Deserialize, Deserializer, Serialize, Serializer,
};
use std::borrow::Borrow;
use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
use std::hash::{BuildHasher, Hash, Hasher};
use std::iter::FromIterator;
use std::marker::PhantomData;
use std::sync::{
atomic::{AtomicIsize, AtomicUsize, Ordering},
Once,
Expand Down Expand Up @@ -2367,6 +2371,63 @@ where
}
}

impl<'de, K, V> Deserialize<'de> for HashMap<K, V, crate::DefaultHashBuilder>
jonhoo marked this conversation as resolved.
Show resolved Hide resolved
where
K: 'static + Deserialize<'de> + Send + Sync + Hash + Clone + Eq,
V: 'static + Deserialize<'de> + Send + Sync + Eq,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(HashMapVisitor::new())
}
}

struct HashMapVisitor<K, V> {
key_marker: PhantomData<K>,
value_marker: PhantomData<V>,
}

impl<K, V> HashMapVisitor<K, V> {
pub(crate) fn new() -> Self {
Self {
key_marker: PhantomData,
value_marker: PhantomData,
}
}
}

impl<'de, K, V> Visitor<'de> for HashMapVisitor<K, V>
where
K: 'static + Deserialize<'de> + Send + Sync + Hash + Clone + Eq,
V: 'static + Deserialize<'de> + Send + Sync + Eq,
{
type Value = HashMap<K, V, crate::DefaultHashBuilder>;

fn expecting(&self, _f: &mut Formatter<'_>) -> fmt::Result {
todo!("Create an error message");
}

fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let map = HashMap::with_capacity(access.size_hint().unwrap_or(0));
GarkGarcia marked this conversation as resolved.
Show resolved Hide resolved
let guard = map.guard();

while let Some((key, value)) = access.next_entry()? {
if let Some(_old_value) = map.insert(key, value, &guard) {
unreachable!(
"`MapAccess::next_entry` should yield two diffirent value associated with the same key"
GarkGarcia marked this conversation as resolved.
Show resolved Hide resolved
);
}
}

Ok(map)
}
}

#[cfg(not(miri))]
#[inline]
/// Returns the number of physical CPUs in the machine (_O(1)_).
Expand Down