Skip to content

Commit

Permalink
Added serde support and v3.
Browse files Browse the repository at this point in the history
  • Loading branch information
xacrimon committed Jan 14, 2020
1 parent da9b196 commit 37a71e9
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "dashmap"
version = "2.1.1"
version = "3.0.0"
authors = ["Acrimon <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -47,3 +47,4 @@ parking_lot = "0.10.0"
num_cpus = "1.11.1"
fxhash = "0.2.1"
crossbeam-utils = "0.7.0"
serde = { version = "~1.0.24", optional = true, features = ["derive"] }
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ If you have any suggestions or tips do not hesitate to open an issue or a PR.

[![downloads](https://img.shields.io/crates/d/dashmap)](https://crates.io/crates/dashmap)

## Cargo features

- `nightly` - Enables experimental nightly optimizations.

- `serde` - Enables serde support.

## Contributing

DashMap is gladly accepts contributions!
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub mod mapref;
mod t;
mod util;

#[cfg(feature = "serde")]
mod serde;

use crossbeam_utils::CachePadded;
use fxhash::FxBuildHasher;
use iter::{Iter, IterMut};
Expand Down
80 changes: 80 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::DashMap;
use serde::de::{Deserialize, MapAccess, Visitor};
use serde::export::PhantomData;
use serde::ser::{Serialize, SerializeMap, Serializer};
use serde::Deserializer;
use std::fmt;
use std::hash::Hash;

pub struct DashMapVisitor<K, V>
where
K: Eq + Hash,
{
marker: PhantomData<fn() -> DashMap<K, V>>,
}

impl<K, V> DashMapVisitor<K, V>
where
K: Eq + Hash,
{
fn new() -> Self {
DashMapVisitor {
marker: PhantomData,
}
}
}

impl<'de, K, V> Visitor<'de> for DashMapVisitor<K, V>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
{
type Value = DashMap<K, V>;

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

fn visit_map<M>(self, mut access: M) -> Result<Self::Value, M::Error>
where
M: MapAccess<'de>,
{
let map = DashMap::with_capacity(access.size_hint().unwrap_or(0));

while let Some((key, value)) = access.next_entry()? {
map.insert(key, value);
}

Ok(map)
}
}

impl<'de, K, V> Deserialize<'de> for DashMap<K, V>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserializer.deserialize_map(DashMapVisitor::<K, V>::new())
}
}

impl<K, V> Serialize for DashMap<K, V>
where
K: Serialize + Eq + Hash,
V: Serialize,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(Some(self.len()))?;
for ref_multi in self.iter() {
map.serialize_entry(ref_multi.key(), ref_multi.value())?;
}
map.end()
}
}

0 comments on commit 37a71e9

Please sign in to comment.