如何以惯用/高效的方式将数据从Read + Seek转换为Write?

时间:2015-11-01 11:43:09

标签: io pipe rust

我想从输入文件中的随机位置获取数据,然后将它们顺序输出到输出文件。优选地,没有不必要的分配。

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延迟。

问题:

  1. 稳定的Rust是否有一些帮助从一个流中获取x个字节并将它们推送到另一个流?或者我必须自己动手?
  2. 如果我必须自己动手,也许有更好的方法?

1 个答案:

答案 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,就可以看到它与您的代码相似。但是,它需要处理更多错误情况:

  1. write 总是写下您要求的所有内容!
  2. &#34;中断&#34;写不通常是致命的。
  3. 它还使用更大的缓冲区大小但仍然堆栈分配它。

相关问题