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 parsing of borrowed strings #228

Merged
merged 1 commit into from
May 8, 2020
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
2 changes: 1 addition & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
match self.bytes.string()? {
ParsedStr::Allocated(s) => visitor.visit_string(s),
ParsedStr::Slice(s) => visitor.visit_str(s),
ParsedStr::Slice(s) => visitor.visit_borrowed_str(s),
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ impl<'a> Bytes<'a> {
}
}

pub fn string(&mut self) -> Result<ParsedStr<'_>> {
pub fn string(&mut self) -> Result<ParsedStr<'a>> {
if self.consume("\"") {
self.escaped_string()
} else if self.consume("r") {
Expand All @@ -566,7 +566,7 @@ impl<'a> Bytes<'a> {
}
}

fn escaped_string(&mut self) -> Result<ParsedStr<'_>> {
fn escaped_string(&mut self) -> Result<ParsedStr<'a>> {
use std::iter::repeat;

let (i, end_or_escape) = self
Expand Down Expand Up @@ -621,7 +621,7 @@ impl<'a> Bytes<'a> {
}
}

fn raw_string(&mut self) -> Result<ParsedStr<'_>> {
fn raw_string(&mut self) -> Result<ParsedStr<'a>> {
let num_hashes = self.bytes.iter().take_while(|&&b| b == b'#').count();
let hashes = &self.bytes[..num_hashes];
let _ = self.advance(num_hashes);
Expand Down
16 changes: 16 additions & 0 deletions tests/borrowed_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
struct Borrowed<'a> {
value: &'a str,
}

const BORROWED: &str = "Borrowed(value: \"test\")";

#[test]
fn borrowed_str() {
assert_eq!(
ron::de::from_str(BORROWED).ok(),
Some(Borrowed { value: "test" })
);
}