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

Repeated primitive fields do not default to packed output in proto3. #704

Closed
rcorre opened this issue Jan 15, 2024 · 0 comments · Fixed by #707
Closed

Repeated primitive fields do not default to packed output in proto3. #704

rcorre opened this issue Jan 15, 2024 · 0 comments · Fixed by #707

Comments

@rcorre
Copy link
Contributor

rcorre commented Jan 15, 2024

Given the following proto file:

syntax = "proto3";

package api;

message Example {
    repeated uint32 i = 1; 
}

And the following rust code:

let mut example = api::Example::new();
example.i.extend_from_slice(&[0, 1, 2, 3, 4, 5]);
let bytes = example.write_to_bytes().unwrap();
std::fs::write("/tmp/out", bytes).unwrap();

Produces a 12-byte file:

$ ls -lh /tmp/out 
-rw------- 1 rcorre rcorre 12 Jan 15 16:46 /tmp/out
$ xxd /tmp/out 
00000000: 0800 0801 0802 0803 0804 0805            ............

If I change the field to repeated uint32 i = 1 [packed=false];, I get the same output.
If I change the field to repeated uint32 i = 1 [packed=true];, instead I get an 8-byte file:

$ ls -lh /tmp/out 
-rw------- 1 rcorre rcorre 8 Jan 15 16:50 /tmp/out
$ xxd /tmp/out 
00000000: 0a06 0001 0203 0405                      ........

For a proto3 file, packed should be the default:

In proto2 this is done using the field option [packed=true]. In proto3 it is the default.

rcorre added a commit to rcorre/rust-protobuf that referenced this issue Jan 16, 2024
rcorre added a commit to rcorre/rust-protobuf that referenced this issue Jan 17, 2024
stepancheg pushed a commit that referenced this issue Feb 25, 2024
vaikzs pushed a commit to vaikzs/rust-protobuf that referenced this issue Jun 1, 2024
stepancheg pushed a commit that referenced this issue Jun 16, 2024
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

Successfully merging a pull request may close this issue.

1 participant