Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 14, 2022 20:15
Show Gist options
  • Save rust-play/be22f6143481e12332f0c432f3fb4c01 to your computer and use it in GitHub Desktop.
Save rust-play/be22f6143481e12332f0c432f3fb4c01 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use async_compression::tokio::bufread::GzipDecoder;
use futures::join;
use tokio::fs::File;
use tokio::io::{AsyncBufRead, AsyncBufReadExt, BufReader};
async fn count_lines<R>(mut reader: R) -> usize
where
R: AsyncBufRead + Send + Unpin,
{
let mut line = String::new();
let mut num_lines = 0;
while reader.read_line(&mut line).await.unwrap() > 0 {
num_lines += 1;
line.clear();
}
return num_lines;
}
#[tokio::main]
async fn main() {
// Read file lines
let unzipped_handle = tokio::spawn(async move {
let file = File::open("unzipped_file").await;
let reader = BufReader::new(file.unwrap());
count_lines(reader).await
});
// Read gzipped lines
let gzipped_handle = tokio::spawn(async move {
let file = File::open("gzipped_file").await;
let reader = BufReader::new(GzipDecoder::new(BufReader::new(file.unwrap())));
count_lines(reader).await
});
// Compare lines
let (unzipped_result, gzipped_result) = join!(unzipped_handle, gzipped_handle);
assert_eq!(413484, unzipped_result.unwrap());
assert_eq!(65654, gzipped_result.unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment