0

I'm trying to stream bytes to a tcp server by using io::copy(&mut reader, &mut writer), but it gives me this error: the trait "std::io::Read" is not implemented for "Vec<{integer}>". Here I have a vector of bytes which would be the same as me opening a file and converting it to bytes. I want to write the bytes to the BufWriter. What am I doing wrong?

use std::io;
use std::net::TcpStream;
use std::io::BufWriter;

pub fn connect() {
    if let Ok(stream) = TcpStream::connect("localhost:8080") {
        println!("Connection established!");
        let mut reader = vec![
            137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 70, 0, 0, 0, 70,
        ];
        let mut writer = BufWriter::new(&stream);
        io::copy(&mut reader, &mut writer).expect("Failed to write to stream");
    } else {
        println!("Couldn't connect to the server")
    }
}
error[E0277]: the trait bound `Vec<{integer}>: std::io::Read` is not satisfied
  --> src/lib.rs:12:31
   |
12 |         io::copy(&mut reader, &mut writer).expect("Failed to write to stream");
   |         --------              ^^^^^^^^^^^ the trait `std::io::Read` is not implemented for `Vec<{integer}>`
   |         |
   |         required by a bound introduced by this call
   |
note: required by a bound in `std::io::copy`
Stargateur
  • 24,473
  • 8
  • 65
  • 91
Nicke7117
  • 187
  • 1
  • 10

2 Answers2

2

the compiler have a little trouble here, Vec doesn't implement Read but &[u8] do, you just have a get a slice from the vec before create a mutable reference:

copy(&mut reader.as_slice(), &mut writer).expect("Failed to write to stream");

See also:

Stargateur
  • 24,473
  • 8
  • 65
  • 91
  • Ok, what's a slice? – Nicke7117 Dec 17 '21 at 21:57
  • Now the server panics: `thread "main" panicked at "called "Result::unwrap()" on an "Err" value: Error { kind: InvalidData, message: "stream did not contain valid UTF-8" }"`. Isn't it possible to stream bytes to a tcp server? – Nicke7117 Dec 17 '21 at 22:03
  • @Nicke7117 that another problem with what you send I can't really help you there – Stargateur Dec 17 '21 at 22:09
  • @Nicke7117 It would be best to ask about your server panicking as a separate question. – effect Dec 18 '21 at 00:35
  • 1
    @Nicke7117 _"What's a slice?"_ → https://doc.rust-lang.org/reference/types/slice.html and https://doc.rust-lang.org/std/primitive.slice.html – Jmb Dec 18 '21 at 08:49
1

Using .as_slice() like so works for me:

pub fn connect() {
    if let Ok(stream) = TcpStream::connect("localhost:8080") {
        println!("Connection established!");
        let reader = vec![
            137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 0, 70, 0, 0, 0, 70,
        ];
        let mut writer = BufWriter::new(&stream);
        io::copy(&mut reader.as_slice(), &mut writer).expect("Failed to write to stream");
    } else {
        println!("Couldn't connect to the server")
    }
}

That’s because std::io::Read supports slices.

Clément Joly
  • 858
  • 9
  • 27