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

Why does length of encoded data is at the first of bytes? #214

Open
farbodpm opened this issue Apr 2, 2022 · 3 comments
Open

Why does length of encoded data is at the first of bytes? #214

farbodpm opened this issue Apr 2, 2022 · 3 comments

Comments

@farbodpm
Copy link

farbodpm commented Apr 2, 2022

Hi,
I was validating quick prot with prost protocol. Encoded data was different from the prost output. Then I realized that the length of the encoded data is added at first.
WHY?

@nerdrew
Copy link
Collaborator

nerdrew commented Apr 9, 2022

You can encode it with or without a length prefix.

Here's how to encode / decode without a length prefix:

let mut buf = Vec::new();
let mut writer = Writer::new(&mut buf);
MyMessage::default().write_message(&mut writer)?;

let mut reader = BytesReader::from_bytes(&buf);
let proto = MyMessage::from_reader(&mut reader, &buf)?;

FWIW this also tripped me up when I first started with this library. Suggestions on how to make the docs clearer?

This is a similar issue to #202

@TeemuKoivisto
Copy link

TeemuKoivisto commented Jan 10, 2023

Thanks @nerdrew for the tip! Incase anyone else is doing the same, I'm using u8 as the header byte so for me it looks like:

    let mut out = Vec::new();
    let mut writer = Writer::new(&mut out);
    writer.write_u8(header.try_into().unwrap()).unwrap();
    payload.write_message(&mut writer).expect("Unable to serialize message!");
    Message::Binary(out)

For the suggestions, I think adding examples would probably be the most helpful in the docs and maybe comments. In the first place, it of course would be nice to not to need such things. But there's probably a good reason why having to encode length is necessary.

@makapuf
Copy link

makapuf commented Nov 22, 2023

Agreeing with the fact that this is not clear in the documentation (also would be nice to have the reason for it)

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

4 participants