Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
228: Fix parsing of borrowed strings r=kvark a=caelunshun The implementation of `deserialize_str` would incorrectly call `Visitor::visit_str` when it should have called `Visitor::visit_borrowed_str`. As per https://serde.rs/lifetimes.html#transient-borrowed-and-owned-data, `visit_borrowed_str` should be called when the lifetime of the deserialized string is that of the input. This should not be a breaking change, because it only extends the lifetime of the deserialized string. The default implementation of `Visitor::visit_borrowed_str` forwards to `visit_str`, meaning that this commit only enables more flexibility. I added a test in `tests/borrowed_str.rs` which demonstrates this behavior. Prior to this change, the test would fail. Co-authored-by: caelunshun <caelunshun@gmail.com>
- Loading branch information
Showing
3 changed files
with
20 additions
and
4 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
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" }) | ||
); | ||
} |