0

My Rust program should either read from stdin or from a file stored on S3. Currently I have something like this:

fn load_from_s3(url: &String) -> impl Read {
    // ...  
    BufReader::new(some_stream)
}

// ...

let reader = match cli.value_of("s3_url") {
      Some(url) => csv::Reader::from_reader(load_from_s3(&url.to_string())),
      None => {
          let lock = io::stdin().lock();
          csv::Reader::from_reader(lock)
      }
  };

The Some branch warns me about this is found to be of type csv::reader::Reader<impl std::io::Read>. The None branch gives me the error expected opaque type, found struct std::io::StdinLock. I'm rather new to Rust and cannot figure out, how to get the types right.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Achim
  • 15,415
  • 15
  • 80
  • 144
  • 1
    `csv::reader::Reader` has a type parameter specifying the underlying IO reader. Since `StdinLock` and `BufReader` are different types, so are `csv::reader::Reader` and `csv::reader::Reader`. Instead, you'll need to use trait objects to allow polymorphism, and most likely you'll also need to box them to allow ownership, so you have `csv::reader::Reader>`. – apetranzilla Aug 22 '19 at 14:29
  • 1
    On second thought, [this](https://stackoverflow.com/q/26378842/1233251) is a better candidate for duplicate target. – E_net4 Aug 22 '19 at 14:32

0 Answers0