How to idiomatically / efficiently pipe data from Read+Seek to Write?

后端 未结 1 776
故里飘歌
故里飘歌 2020-11-30 15:01

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 o

相关标签:
1条回答
  • 2020-11-30 15:48

    You are looking for 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(())
    }
    

    If you look at the implementation of io::copy, you can see that it's similar to your code. However, it takes care to handle more error cases:

    1. write does not always write everything you ask it to!
    2. An "interrupted" write isn't usually fatal.

    It also uses a larger buffer size but still stack-allocates it.

    0 讨论(0)
提交回复
热议问题