-
Notifications
You must be signed in to change notification settings - Fork 50
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
Nathan Ringo
committed
Jul 30, 2021
1 parent
9e93c18
commit a6d7970
Showing
5 changed files
with
49 additions
and
0 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
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,20 @@ | ||
use crate::{Gc, Trace}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
impl<'de, T: Deserialize<'de> + Trace> Deserialize<'de> for Gc<T> { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
T::deserialize(deserializer).map(Gc::new) | ||
} | ||
} | ||
|
||
impl<T: Serialize + Trace> Serialize for Gc<T> { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
T::serialize(&self, serializer) | ||
} | ||
} |
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,23 @@ | ||
#![cfg(feature = "serde")] | ||
|
||
use gc::Gc; | ||
use serde_json::json; | ||
use std::collections::HashMap; | ||
|
||
type Example = Gc<HashMap<String, Gc<Vec<i32>>>>; | ||
|
||
#[test] | ||
fn serde_tests() { | ||
let value = json!({ | ||
"hello": [104, 101, 108, 108, 111], | ||
"world": [119, 111, 114, 108, 100], | ||
}); | ||
|
||
let mut expected = HashMap::new(); | ||
expected.insert("hello".to_string(), Gc::new(vec![104, 101, 108, 108, 111])); | ||
expected.insert("world".to_string(), Gc::new(vec![119, 111, 114, 108, 100])); | ||
let expected = Gc::new(expected); | ||
|
||
assert_eq!(serde_json::to_value(&expected).unwrap(), value); | ||
assert_eq!(serde_json::from_value::<Example>(value).unwrap(), expected); | ||
} |