【发布时间】:2015-11-01 11:43:09
【问题描述】:
我想从输入文件中的随机位置获取数据,并将它们按顺序输出到输出文件。最好不要进行不必要的分配。
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 延迟。
问题:
- 稳定的 Rust 是否有一些助手可以从一个流中获取 x 字节并将它们推送到另一个流?还是我必须自己动手?
- 如果我必须自己动手,也许有更好的方法?
【问题讨论】:
-
不相关:将 assemble 更改为使用
<I: ?Sized, O: ?Sized>,它更加通用(允许 trait 对象)。