Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Error when trying to create a vector to store message bytes

I am trying to stream a file through libflate gzip decoder. I get an error when trying to create a vector to store message bytes.

use std::io::Read;
use std::io::Cursor;
use libflate::gzip::Decoder;

fn main() {
    let mut f = std::fs::File::open("./20181002.bytes").unwrap();
    let mut decoder = Decoder::new(&f).unwrap();

    let mut dst = [0u8; 4];
    let msglen_bytes = decoder.read_exact(&mut dst).unwrap();
    println!("msglen_bytes: {}", msglen_bytes);

    let mut bytes = vec!(0u8, msglen_bytes, msglen_bytes);
    let msg_bytes = decoder.read_exact(&mut bytes).unwrap();

    let msg = root_as_flatbuffers_msg(&msg_bytes).expect("Bad Msg");

    println!("Msg: {:?}", msg);
}
error[E0308]: mismatched types
  --> src/main.rs:45:31
   |
45 |     let mut bytes = vec!(0u8, msglen_bytes, msglen_bytes);
   |                               ^^^^^^^^^^^^ expected `u8`, found `()`

error[E0308]: mismatched types
  --> src/main.rs:48:42
   |
48 |     let msg = root_as_flatbuffers_msg(&msg_bytes).expect("Bad Msg");
   |                                          ^^^^^^^^^^ expected slice `[u8]`, found `()`
   |
   = note: expected reference `&[u8]`
              found reference `&()`

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

read_exact() operates by side effect – it reads data into the provided slice, and its return value only exists to signal an error, if any. This is why you got () in msg_bytes (which turned into &() because you borrowed it).

To fix the issue, call read_exact() without capturing the return value and pass &bytes to root_as_flatbuffers_msg():

decoder.read_exact(&mut bytes).unwrap();
let msg = root_as_flatbuffers_msg(&bytes).expect("Bad Msg");
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading