我想从输入文件中的随机位置获取数据,然后将它们顺序输出到输出文件。优选地,没有不必要的分配。
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(())
}
让我们不要担心这里的I / O延迟。
问题:
答案 0 :(得分:7)
您正在寻找io::copy
:
use std::io::{self, prelude::*, SeekFrom};
pub fn assemble<I, O>(mut input: I, mut output: O) -> Result<(), io::Error>
where
I: Read + Seek,
O: Write,
{
// first seek and output "hello"
input.seek(SeekFrom::Start(5))?;
io::copy(&mut input.by_ref().take(5), &mut output)?;
// then output "world"
input.seek(SeekFrom::Start(0))?;
io::copy(&mut input.take(5), &mut output)?;
Ok(())
}
如果您查看the implementation of io::copy
,就可以看到它与您的代码相似。但是,它需要处理更多错误情况:
write
不总是写下您要求的所有内容!它还使用更大的缓冲区大小但仍然堆栈分配它。