Skip to content

Commit f013f87

Browse files
CfirTsabariNemo157
authored andcommitted
Add Examples.
Example of zlip tokio write ecnoder and decoder. Example reading zstd from stdin and write gzip to stdout, using tokio Related to Nullus157#101
1 parent 796abb2 commit f013f87

File tree

4 files changed

+91
-1
lines changed

4 files changed

+91
-1
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ futures-test = "0.3.5"
6161
ntest = "0.3.3"
6262
timebomb = "0.1.2"
6363
bytes = "0.5.0"
64-
tokio-02 = { package = "tokio", version = "0.2.21", default-features = false, features = ["io-util", "stream"] }
64+
tokio-02 = { package = "tokio", version = "0.2.21", default-features = false, features = ["io-util", "stream", "macros", "io-std"] }
6565
tokio-03 = { package = "tokio", version = "0.3.0", default-features = false, features = ["io-util", "stream"] }
6666
tokio-util = { version = "0.4.0", default-features = false, features = ["io"] }
6767

@@ -96,3 +96,11 @@ required-features = ["zlib"]
9696
[[test]]
9797
name = "zstd"
9898
required-features = ["zstd"]
99+
100+
[[example]]
101+
name = "zlib_tokio_02_write"
102+
required-features = ["zlib", "tokio-02"]
103+
104+
[[example]]
105+
name = "zstd_gzip"
106+
required-features = ["zstd", "gzip", "tokio-02"]

examples/zlib_tokio_02_write.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
use async_compression::tokio_02::write::ZlibDecoder;
2+
use async_compression::tokio_02::write::ZlibEncoder;
3+
4+
use std::io::Result;
5+
use tokio_02::io::AsyncWriteExt as _; // for `write_all` and `shutdown`
6+
7+
use tokio_02 as tokio; // this enable the tokio main macro
8+
#[tokio_02::main]
9+
async fn main() -> Result<()> {
10+
let data = b"example";
11+
let compressed_data = compress(data).await?;
12+
let de_compressed_data = decompress(&compressed_data).await?;
13+
assert_eq!(de_compressed_data, data);
14+
println!("{:?}", String::from_utf8(de_compressed_data).unwrap());
15+
Ok(())
16+
}
17+
18+
async fn compress(in_data: &[u8]) -> Result<Vec<u8>> {
19+
let mut encoder = ZlibEncoder::new(Vec::new());
20+
encoder.write_all(in_data).await?;
21+
encoder.shutdown().await?;
22+
Ok(encoder.into_inner())
23+
}
24+
25+
async fn decompress(in_data: &[u8]) -> Result<Vec<u8>> {
26+
let mut decoder = ZlibDecoder::new(Vec::new());
27+
decoder.write_all(in_data).await?;
28+
decoder.shutdown().await?;
29+
Ok(decoder.into_inner())
30+
}

examples/zstd_gzip.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use async_compression::tokio_02::bufread::ZstdDecoder;
2+
use async_compression::tokio_02::write::GzipEncoder;
3+
4+
use std::io::Result;
5+
use tokio_02::io::stderr;
6+
use tokio_02::io::stdin;
7+
use tokio_02::io::stdout;
8+
use tokio_02::io::AsyncReadExt as _; // for `read_to_end`
9+
use tokio_02::io::AsyncWriteExt as _; // for `write_all` and `shutdown`
10+
use tokio_02::io::BufReader;
11+
12+
// Run this example by running the following in the terminal:
13+
// ```
14+
// echo 'example' | zstd | cargo run --example zstd_gzip --features="all" | gunzip -c ─╯
15+
// ```
16+
17+
use tokio_02 as tokio; // this enable the tokio main macro
18+
#[tokio_02::main]
19+
async fn main() -> Result<()> {
20+
// Read zstd encoded data from stdin and decode
21+
let mut reader = ZstdDecoder::new(BufReader::new(stdin()));
22+
let mut x: Vec<u8> = vec![];
23+
reader.read_to_end(&mut x).await?;
24+
25+
// print to stderr the length of the decoded data
26+
let mut error = stderr();
27+
error.write_all(x.len().to_string().as_bytes()).await?;
28+
error.shutdown().await?;
29+
30+
// print to stdin encoded gzip data
31+
let mut writer = GzipEncoder::new(stdout());
32+
writer.write_all(&x).await?;
33+
writer.shutdown().await?;
34+
35+
// flush stdout
36+
let mut res = writer.into_inner();
37+
res.flush().await?;
38+
39+
Ok(())
40+
}

0 commit comments

Comments
 (0)