Skip to content

Commit a61c994

Browse files
committed
Merge branch 'release/v0.1.0'
2 parents 40e17d4 + ba10875 commit a61c994

File tree

13 files changed

+404
-12
lines changed

13 files changed

+404
-12
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,8 @@ Cargo.lock
4141
# These are backup files generated by rustfmt
4242
**/*.rs.bk
4343

44+
# Others
45+
async-pipe-rs.iws
46+
47+
4448
# End of https://www.gitignore.io/api/rust,macos

.idea/.gitignore

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

.idea/misc.xml

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

.idea/modules.xml

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

.idea/vcs.xml

Lines changed: 6 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: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
[package]
2-
name = "async-pipe-rs"
2+
name = "async-pipe"
33
version = "0.1.0"
4+
description = "Creates an asynchronous piped reader and writer pair using tokio.rs"
5+
homepage = "https://github.com/rousan/async-pipe-rs"
6+
repository = "https://github.com/rousan/async-pipe-rs"
7+
keywords = ["pipe", "future", "async", "reader", "writer"]
8+
categories = ["asynchronous"]
49
authors = ["Rousan Ali <[email protected]>"]
10+
readme = "README.md"
11+
license = "MIT"
512
edition = "2018"
613

7-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8-
914
[dependencies]
15+
tokio = { version = "0.2", features= [] }
16+
log = "0.4"
17+
18+
[dev-dependencies]
19+
tokio = { version = "0.2", features = ["full"] }

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1-
# pipe-rs
1+
# async-pipe-rs
22

3-
```Rust
4-
pipe<W: AsyncWrite, R: AsyncRead>() -> (W, R)
3+
[![crates.io](https://img.shields.io/crates/v/async-rs.svg)](https://crates.io/crates/async-rs)
4+
[![Documentation](https://docs.rs/async-rs/badge.svg)](https://docs.rs/async-rs)
5+
[![MIT](https://img.shields.io/crates/l/async-rs.svg)](./LICENSE)
6+
7+
Creates an asynchronous piped reader and writer pair using `tokio.rs`.
8+
9+
[Docs](https://docs.rs/async-rs)
10+
11+
## Example
12+
13+
```rust
14+
use async_pipe;
15+
use tokio::prelude::*;
16+
17+
#[tokio::main]
18+
async fn main() {
19+
let (mut w, mut r) = async_pipe::pipe();
20+
21+
tokio::spawn(async move {
22+
w.write_all(b"hello world").await.unwrap();
23+
});
24+
25+
let mut v = Vec::new();
26+
r.read_to_end(&mut v).await.unwrap();
27+
println!("Received: {:?}", String::from_utf8(v));
28+
}
529
```
30+
31+
## Contributing
32+
33+
Your PRs and stars are always welcome.

async-pipe-rs.iml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="RUST_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
9+
<sourceFolder url="file://$MODULE_DIR$/benches" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="inheritedJdk" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
</component>
15+
</module>

examples/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use async_pipe;
2+
use tokio::prelude::*;
3+
4+
#[tokio::main]
5+
async fn main() {
6+
let (mut w, mut r) = async_pipe::pipe();
7+
8+
tokio::spawn(async move {
9+
w.write_all(b"hello world").await.unwrap();
10+
});
11+
12+
let mut v = Vec::new();
13+
r.read_to_end(&mut v).await.unwrap();
14+
println!("Received: {:?}", String::from_utf8(v));
15+
}

src/lib.rs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
1+
//! Creates an asynchronous piped reader and writer pair using `tokio.rs`.
2+
//!
3+
//! # Examples
4+
//!
5+
//! ```
6+
//! # async fn run() {
7+
//! use async_pipe;
8+
//! use tokio::prelude::*;
9+
//!
10+
//! let (mut w, mut r) = async_pipe::pipe();
11+
//!
12+
//! tokio::spawn(async move {
13+
//! w.write_all(b"hello world").await.unwrap();
14+
//! });
15+
//!
16+
//! let mut v = Vec::new();
17+
//! r.read_to_end(&mut v).await.unwrap();
18+
//!
19+
//! println!("Received: {:?}", String::from_utf8(v));
20+
//! # }
21+
//!
22+
//! tokio::runtime::Runtime::new().unwrap().block_on(run());
23+
//! ```
24+
25+
use state::State;
26+
use std::sync::{Arc, Mutex};
27+
28+
pub use self::reader::PipeReader;
29+
pub use self::writer::PipeWriter;
30+
31+
mod reader;
32+
mod state;
33+
mod writer;
34+
35+
/// Creates a piped pair of an [`AsyncWrite`](https://docs.rs/tokio/0.2.16/tokio/io/trait.AsyncWrite.html) and an [`AsyncRead`](https://docs.rs/tokio/0.2.15/tokio/io/trait.AsyncRead.html).
36+
pub fn pipe() -> (PipeWriter, PipeReader) {
37+
let shared_state = Arc::new(Mutex::new(State {
38+
reader_waker: None,
39+
writer_waker: None,
40+
data: None,
41+
done_reading: false,
42+
read: 0,
43+
done_cycle: true,
44+
closed: false,
45+
}));
46+
47+
let w = PipeWriter {
48+
state: Arc::clone(&shared_state),
49+
};
50+
51+
let r = PipeReader {
52+
state: Arc::clone(&shared_state),
53+
};
54+
55+
(w, r)
756
}

0 commit comments

Comments
 (0)