use std::io::{Read, Write};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// It's your “read stream”. Could be any.
let reader = std::fs::OpenOptions::new().read(true).open("in_file.txt")?;
// It's your adaptor to perform “middle step”.
let mut trans_reader = TransReader {
orginal: reader,
transform: |bytes: &mut [u8]| {
// Could be any transformation.
bytes.make_ascii_uppercase();
},
};
// It's your “write stream”. Could be any.
let mut writer = std::fs::OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open("out_file.txt")?;
// You can read bytes in anyway you want and then write. Eg. read all then write all.
let mut buf = vec![];
trans_reader.read_to_end(&mut buf)?;
writer.write_all(&buf)?;
Ok(())
}
// It's my implementation of adaptor you need.
pub struct TransReader<R: Read, F: FnMut(&mut [u8])> {
pub orginal: R,
pub transform: F,
}
impl<Org: Read, F: FnMut(&mut [u8])> Read for TransReader<Org, F> {
// This is the only method that needs to be implemented.
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let res = self.orginal.read(buf);
if let Ok(written_count) = res {
(self.transform)(&mut buf[..written_count]);
}
res
}
// I'm not 100% if manual impl. of other methods would also make for performance.
}
1条答案
按热度按时间ubof19bj1#
我已经实现了
Read
适配器,它应该是零成本的抽象,以实现您引入“中间步骤”的目的。它类似于
Iterator::map
,但用于Read
示例。Read
示例就是所谓的流。你似乎在使用Java/C++的术语。如果你有其他的意思,请澄清。字符串