I want to take data from random locations in input file, and output them sequentially to output file. Preferably, with no unnecessary allocations.
This is one kind of solution I have figured out:
use std::io::{ self, SeekFrom, Cursor, Read, Write, Seek };
#[test]
fn read_write() {
// let's say this is input file
let mut input_file = Cursor::new(b"worldhello");
// and this is output file
let mut output_file = Vec::<u8>::new();
assemble(&mut input_file, &mut output_file).unwrap();
assert_eq!(b"helloworld", &output_file[..]);
}
// I want to take data from random locations in input file
// and output them sequentially to output file
pub fn assemble<I, O>(input: &mut I, output: &mut O) -> Result<(), io::Error>
where I: Read + Seek, O: Write
{
// first seek and output "hello"
try!(input.seek(SeekFrom::Start(5)));
let mut hello_buf = [0u8; 5];
try!(input.take(5).read(&mut hello_buf));
try!(output.write(&hello_buf));
// then output "world"
try!(input.seek(SeekFrom::Start(0)));
let mut world_buf = [0u8; 5];
try!(input.take(5).read(&mut world_buf));
try!(output.write(&world_buf));
Ok(())
}
Let's not worry about I/O latency here.
Questions:
- Does the stable Rust have some helper to take x bytes from one stream and push them to another stream? Or do I have to roll my own?
- If I have to roll my own, maybe there is a better way?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…