6 releases
0.1.6 | May 12, 2024 |
---|---|
0.1.5 | May 3, 2024 |
0.1.4 | Apr 26, 2024 |
0.1.3 | Mar 31, 2024 |
0.1.0 |
|
#19 in #data-source
300 downloads per month
16KB
253 lines
Multiple Readers
multiple-readers
is a Rust library aimed at simplifying the process of combining multiple types that implement the std::io::Read trait into a unified reader.
Features
- Combines multiple types that implement the std::io::Read trait into a unified reader.
- Provides SliceReader and BytesReader types, which respectively wrap
&[u8]
andVec<u8>
, implementing the std::io::Read and tokio::io::AsyncRead trait. - Can read from data sources sequentially until all data sources are exhausted.
- Supports tokio (
Unstable
)
Example
use multi_readers::{BytesReader, SliceReader, join_readers};
use std::{fs::File, io::Read};
fn main() -> std::io::Result<()> {
let slice = SliceReader::new(b"First-");
let bytes = BytesReader::new(b"Second-".to_vec());
std::fs::write("test.txt", b"Third")?;
let f = File::open("test.txt")?;
let mut reader = join_readers!(slice, bytes, f);
let mut buf = String::new();
reader.read_to_string(&mut buf)?;
assert_eq!(buf.as_str(), "First-Second-Third");
Ok(())
}
Async Example
- dependencies
tokio = { version = "*", features = ["full"]}
multi-readers = {version = "*", features = ["async"]}
use multi_readers::*;
use tokio::io::AsyncReadExt;
#[tokio::main]
async fn main() {
let slice1 = SliceReader::new(b"12345");
let slice2 = SliceReader::new(b"2346");
let mut reader = join_async_readers!(slice1, slice2);
let mut buf = [0; 4];
let len = reader.read(&mut buf).await.unwrap();
assert_eq!(&buf[..len], b"1234");
let len = reader.read(&mut buf).await.unwrap();
assert_eq!(&buf[..len], b"5234");
let len = reader.read(&mut buf).await.unwrap();
assert_eq!(&buf[..len], b"6");
}
Dependencies
~0–1.1MB
~19K SLoC