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

take_from_bytes_cobs error should return the remainder of the buffer #203

Open
matoushybl opened this issue Jan 20, 2025 · 0 comments
Open

Comments

@matoushybl
Copy link

matoushybl commented Jan 20, 2025

When take_from_bytes_cobs fails to deserialize a value or is called on an incomplete COBS frame, it doesn't return the the remainder of the buffer.
In the first case, the buffer now contains a sequence of bytes already decoded with COBS, a zero and a sequence of bytes not yet decoded with COBS. It would be helpful if the method returned the unencoded portion of the buffer back, so that it could be easily handled further.

In the second case when the buffer doesn't contain a whole COBS frame, it'll leave the buffer further unprocessable.

As a workaround for my project, I implemented a thin wrapper around from_bytes_cobs that always returns the remainder of the buffer.
I can create a PR, if this is the right way to do this.

pub fn take_from_slice<'a, T>(buf: &'a mut [u8]) -> (&'a mut [u8], Result<T, Error>)
where
    T: Deserialize<'a>,
{
    let Some(zero_pos) = buf.iter().position(|&b| b == 0) else {
        return (buf, Err(Error::DeserializeUnexpectedEnd));
    };

    let (leading, trailing) = buf.split_at_mut(zero_pos + 1);

    (trailing, postcard::from_bytes_cobs(leading))
}

This is remotely related to: #71 .

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

No branches or pull requests

1 participant