tafia / quick-protobuf

A rust implementation of protobuf parser
MIT License
452 stars 87 forks source link

bug: Compile fails, inccorect code gen for 'bytes' with flag --dont_use_cow. #173

Closed jozn closed 4 years ago

jozn commented 4 years ago

I illustrate with a sample:

for this message

message Invoke {
  uint32 method = 1;
  uint64 action_id = 2;
  bool is_response = 3;
  bytes rpc_data = 4;
}

the output is:

#[derive(Debug, Default, PartialEq, Clone)]
pub struct Invoke {
    ...
    pub rpc_data: Vec<u8>,
}

impl MessageWrite for Invoke {
    fn get_size(&self) -> usize {
        ...
        + if self.rpc_data == vec![] { 0 } else { 1 + sizeof_len((&self.rpc_data).len()) }
    }

    fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
       ...
        if self.rpc_data != vec![] { w.write_with_tag(34, |w| w.write_bytes(&**&self.rpc_data))?; }
        Ok(())
    }
}

compiler fails to infer type:

error[E0282]: type annotations needed
   --> src/pb/pb/sys.rs:197:31
    |
197 |         + if self.rpc_data == vec![] { 0 } else { 1 + sizeof_len((&self.rpc_data).len()) }
    |                               ^^^^^^ cannot infer type for type parameter `T`
    |
    = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

output should be something like:

+ if self.rpc_data.is_empty() { 0 } else { 1 + sizeof_len((&self.rpc_data).len()) }