1

Eventually I will need a ByteStream for rusoto. So I thought I can create it from a futures::Stream:

pub fn z_stream(r: impl Read) -> Result<ByteStream> {
  let outstream = stream::empty();

  // TODO, how do I wrap outstream
  process(r, &mut wrapped_outstream)?;

  Ok(ByteStream::new(outstream))
}

I have a method process that takes 2 parameters, one for impl Read, the other for impl Write, where Read and Write come from std::io.

How do I wrap the outstream above into something that allows me to pass it to process? There's probably something already out there and I don't have to write my own wrapper?

user2084865
  • 625
  • 1
  • 7
  • 18
  • Does the `process` function run synchronously or asynchronously? – pretzelhammer Dec 28 '20 at 12:46
  • You are aware of the `into_blocking_read()` method for `ByteStream`, right? Have you tried that? Why doesn't it work? – Coder-256 Dec 28 '20 at 21:00
  • @pretzelhammer, for now sync. @Coder-256, but the `process()` method needs to write into it, not read from the ByteStream and there isn't a `into_write` method. – user2084865 Dec 29 '20 at 07:02

1 Answers1

0

You can't write to a Stream in the same way you can't write to an Iterator, they are read-only interfaces. You have to write to something else and then have that something else implement the Stream trait so it can be read from. In your case, since process is sync, you can pass it a Vec<u8> buffer to write to and then convert that buffer into a ByteStream using the From<Vec<u8>> impl:

use std::io::Write;

pub fn z_stream(r: impl Read) -> Result<ByteStream> {
  let buffer: Vec<u8> = Vec::new();

  process(r, &mut buffer)?; // &mut Vec<u8> impls io::Write

  Ok(buffer.into()) // ByteStream has From<Vec<u8>> impl
}
pretzelhammer
  • 13,874
  • 15
  • 47
  • 98