-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
1 deletion.
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
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" | ||
|
@@ -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"] } |
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,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() | ||
} | ||
} |