Reading from a ProtoBuffer file

I am trying to read a ProtoBuffer file. I am using Prost and trying to use the prost::Message::decode_length_delimited function .

If I pass the Vector I get an error saying the Buf trait is not implemented. Passing the Cursor also results in the same error ( cannot infer type )

I understand that it does not make sense to read the entire file at once and this will be a kind of server . But I am trying to initially play around before moving to a different approach
Any help appreciated.
But I always get the following error

cargo run
   Compiling dnstap v0.1.0 (/home/hvram/projects/dnstap)
warning: unused import: `prost::Message`
 --> src/main.rs:6:5
  |
6 | use prost::Message;
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0283]: type annotations needed
  --> src/main.rs:30:5
   |
30 |     prost::Message::decode_length_delimited(&mut buffer);
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type
   |
   = note: cannot satisfy `_: prost::Message`
   = note: required by `decode_length_delimited`

error: aborting due to previous error; 1 warning emitted

For more information about this error, try `rustc --explain E0283`.
error: could not compile `dnstap`

To learn more, run the command again with --verbose.

fn main() -> Result<(), Box> {
println!("Hello, world!");
let mut f = File::open("dnstap.log")?;
let mut reader = BufReader::new(f);
let capacity = reader.capacity();
let mut buffer = reader.fill_buf()?;
//let mut buffer = Vec::new();
// read the whole file
//let msg = f.read_to_end(&mut buffer)?;
//let mut cursor: Cursor<Vec<u8>> = Cursor::new(buffer);
//println!("Finished Reading! {}",buffer.len());
prost::Message::decode_length_delimited(&mut buffer);
//println!("Finished Reading! {}",m1);
Ok(())

}

I believe you need something like

let message: SomeType = prost::Message::decode_length_delimited(&mut buffer)?;

Where SomeType implements the prost::Message trait. (There are many such types, and Rust has no way to infer which one you meant.)

Thanks @quinedot .It works I tried

// Include the dnstap module, which is generated from dnstap.proto.
pub mod dnstap {
include!(concat!(env!("OUT_DIR"), "/dnstap.rs"));
}
....
....
let dnstap_message:dnstap::Message = prost::Message::decode_length_delimited(&mut cursor)?;

where the dnstap.rs is from dnstap.proto Github using the protobuild

and that works.
Thanks for the hep

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.