Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> How would one handle this in Rust, C++, or Java?

In Rust you'd prefer to write the parser to slice the original memory and not copy out until you're done parsing. You can see this in, for instance, the signature of methods in the httparse crate:

    pub fn parse_headers<'b: 'h, 'h>(
        src: &'b [u8],
        dst: &'h mut [Header<'b>]
    ) -> Result<(usize, &'h [Header<'b>])>
To translate, this means that you must provide a byte slice that lives at least as long as 'b, and a mutable array of Headers that lives at least as long as 'h, and those headers then may reference data that lives as long as 'b (that is, the original bytes).

This way we avoid creating the garbage in the first place by demanding that the original allocation live long enough.





Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: