Because the iter can produce hundreds of thousands or millions of items, I would like to replace write_all with a little bit of chunking so I don't do an I/O for each item from the iter.
I gave BufWriter a go but it requires mut, and I can't use that within the closure.
It would be nice to be able to use write_vectored or write_all_vectored.
For vectored write, there are 2 problems to solve:
1 - Convert String to IoSlice
2 - take like 10,000 Strings (or up to some mem limit I can set) and write them out.
Is there any existing pattern for doing this?
Or is something wild like shipping each item to a channel, then an rx channel fills up some buffer and writes it out... possible?
Can you share more of your code? The following works for me:
use std::io::Write;
use std::io::BufWriter;
fn main() {
let mut buf_writer = BufWriter::new(Vec::<u8>::new()); // the Vec could also be a file
vec![String::new()] // infileiter
.into_iter()
.map(|x| x) // do some mapping
.for_each(|x: String| buf_writer.write_all(x.as_bytes()).unwrap())
}
It would be nice to be able to use write_vectored or write_all_vectored.
For vectored write, there are 2 problems to solve:
1 - Convert String to IoSlice
2 - take like 10,000 Strings (or up to some mem limit I can set) and write them out.
Is there any existing pattern for doing this?
Or is something wild like shipping each item to a channel, then an rx channel fills up some buffer and writes it out... possible?