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

In situ parsing #318

Open
dtolnay opened this issue May 10, 2017 · 5 comments
Open

In situ parsing #318

dtolnay opened this issue May 10, 2017 · 5 comments

Comments

@dtolnay
Copy link
Member

dtolnay commented May 10, 2017

pub fn from_mut_str<'a, T>(s: &'a mut str) -> Result<T>
where
    T: Deserialize<'a>;

Strings with escape sequences are decoded in place!

// before
"{\"msg\":\"abc\\nxyz\"}"

// after
"{\"msg\":\"abc\nxyzz\"}"
            ^^^^^^^^ output &str
@lambda-fairy
Copy link

lambda-fairy commented May 12, 2017

&[u8], &[Option<u8>], and &[u16] can be decoded in place as well, since every element of a JSON array takes up at least two bytes. With some effort this can apply to tuples and structs too.

I can imagine doing something clever to represent maps, but that might be going a bit too far.

@clarfonthey
Copy link

What would happen to the end of the string here? Because the result is shorter than the actual buffer.

Would the input itself have any guarantees?

@dtolnay
Copy link
Member Author

dtolnay commented May 15, 2017

There are no guarantees around what modification is made to the input string. In the example above, b'z' happens to be duplicated to account for the difference in length.

@clarfonthey
Copy link

Oh, I see. I missed the single b'z' so it looked like the string was shortened, which didn't seem right.

@saethlin
Copy link

saethlin commented Nov 22, 2018

This would be pretty cool. Does anyone have an implementation of this in-place (and no_std) unescaping? I can't find one, but maybe my google-fu just isn't good enough.

EDIT: Got a prototype working. This was much easier than I thought, just needed some time to think on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants