Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #423 deserialising an identifier into a borrowed str #424

Merged
merged 1 commit into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))))
}
}