Skip to content

Commit

Permalink
Fix ron-rs#423 deserialising an identifier into a borrowed str
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Oct 18, 2022
1 parent 562963f commit 7885461
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix issue [#410](https://github.com/ron-rs/ron/issues/410) trailing comma parsing in tuples and `Some` ([#412](https://github.com/ron-rs/ron/pull/412))
- Error instead of panic when deserializing non-identifiers as field names ([#415](https://github.com/ron-rs/ron/pull/415))
- Breaking: Fix issue [#307](https://github.com/ron-rs/ron/issues/307) stack overflow with explicit recursion limits in serialising and deserialising ([#420](https://github.com/ron-rs/ron/pull/420))
- Fix issue [#423](https://github.com/ron-rs/ron/issues/423) deserialising an identifier into a borrowed str ([#424](https://github.com/ron-rs/ron/pull/424))

## [0.8.0] - 2022-08-17

Expand Down
2 changes: 1 addition & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {

self.last_identifier = Some(identifier);

visitor.visit_str(identifier)
visitor.visit_borrowed_str(identifier)
}

fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value>
Expand Down
43 changes: 43 additions & 0 deletions tests/423_de_borrowed_identifier.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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>;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a serialize dyn struct")
}

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))))
}
}

0 comments on commit 7885461

Please sign in to comment.