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

fixed32 not working as expected => const. 4 bytes #1229

Open
mp1609 opened this issue Jan 15, 2025 · 0 comments
Open

fixed32 not working as expected => const. 4 bytes #1229

mp1609 opened this issue Jan 15, 2025 · 0 comments

Comments

@mp1609
Copy link

mp1609 commented Jan 15, 2025

Hi there,

I am using a header, to identify what communication is done. So I thought, when I use "fixed32" for all of my header data, my header should have the same size (3x4byte = 12bytes) all the time, independet of the values.

Cargo.toml:

[...]

[dependencies]
prost = "0.13.4"

[build-dependencies]
prost-build = "0.13.4"

So my idea is, that every message has the "MsgHeader" so, I know on every message which type it is.

So here is my definition of the header:

backend.proto

syntax = "proto3";

package backend;

message MsgHeader {
    fixed32  pkgType = 1;   // package type
    fixed32  pkgNr   = 2;   // package number
    fixed32  pkgSize = 3;   // package size
}

message PkgHeader {    
    MsgHeader msgHeader             = 1;
}

So my generated rust file looks like:

// This file is @generated by prost-build.
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
pub struct MsgHeader {
    /// package type
    #[prost(fixed32, tag = "1")]
    pub pkg_type: u32,
    /// package number
    #[prost(fixed32, tag = "2")]
    pub pkg_nr: u32,
    /// package size
    #[prost(fixed32, tag = "3")]
    pub pkg_size: u32,
}
#[derive(Clone, Copy, PartialEq, ::prost::Message)]
pub struct PkgHeader {
    #[prost(message, optional, tag = "1")]
    pub msg_header: ::core::option::Option<MsgHeader>,
}

In my understanding, the struct PkgHeader must have a minimum of 3x4bytes but also a constant size.

So when I use the code in my main:

use prost::Message;

mod backend {    
    include!("protobuf/backend.rs");
}

fn main() { 
    let mut pkg_header = backend::PkgHeader {
        msg_header: Some(backend::MsgHeader {
            pkg_type : 0,
            pkg_nr   : 0,
            pkg_size : 0,
        }),
    };

    let minPkgSize = pkg_header.encode_to_vec().len();
    eprintln!("minPkgSize {}", minPkgSize); // <<<<<<<<< the size is here 2


    let mut pkg_header2 = backend::PkgHeader {
        msg_header: Some(backend::MsgHeader {
            pkg_type : 123456789,
            pkg_nr   : 123456789,
            pkg_size : 123456789,
        }),
    };

    let minPkgSize2 = pkg_header2.encode_to_vec().len();

    eprintln!("minPkgSize2 {}", minPkgSize2); //  <<<<<<<<<  the size is here 17
}

Perhaps a noob question, sorry for that, but I have no idea whats wrong.

Also the definitions says alway 4bytes:
https://stackoverflow.com/questions/59195191/when-to-use-fixed-value-protobuf-type-or-under-what-scenarios
grafik

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