-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy path423_de_borrowed_identifier.rs
45 lines (35 loc) · 1.13 KB
/
423_de_borrowed_identifier.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use std::any::Any;
use serde::{
de::{MapAccess, Visitor},
Deserializer,
};
#[test]
fn manually_deserialize_dyn() {
let ron = r#"SerializeDyn(
type: "engine_utils::types::registry::tests::Player",
)"#;
let mut de = ron::Deserializer::from_bytes(ron.as_bytes()).unwrap();
let result = de
.deserialize_struct("SerializeDyn", &["type"], SerializeDynVisitor)
.unwrap();
assert_eq!(
*result.downcast::<Option<(String, String)>>().unwrap(),
Some((
String::from("type"),
String::from("engine_utils::types::registry::tests::Player")
))
);
}
struct SerializeDynVisitor;
impl<'de> Visitor<'de> for SerializeDynVisitor {
type Value = Box<dyn Any>;
// GRCOV_EXCL_START
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a serialize dyn struct")
}
// GRCOV_EXCL_STOP
fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Self::Value, A::Error> {
let entry = map.next_entry::<&str, String>()?;
Ok(Box::new(entry.map(|(k, v)| (String::from(k), v))))
}
}