Skip to content

Commit 8e19102

Browse files
committed
Implement the logic
1 parent e13ea8a commit 8e19102

File tree

13 files changed

+352
-89
lines changed

13 files changed

+352
-89
lines changed

.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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
[package]
22
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

714
[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: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
# async-pipe-rs
22

3-
```Rust
4-
use async_pipe::pipe;
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)
56

6-
fn main() {
7-
let (w, r) = pipe();
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));
828
}
929
```
30+
31+
## Contributing
32+
33+
Your PRs and stars are always welcome.

async-pipe-rs.ipr

Lines changed: 0 additions & 20 deletions
This file was deleted.

async-pipe-rs.iws

Lines changed: 0 additions & 59 deletions
This file was deleted.

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: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,31 @@
1-
#[cfg(test)]
2-
mod tests {
3-
#[test]
4-
fn it_works() {
5-
assert_eq!(2 + 2, 4);
6-
}
1+
use state::State;
2+
use std::sync::{Arc, Mutex};
3+
4+
pub use self::reader::PipeReader;
5+
pub use self::writer::PipeWriter;
6+
7+
mod reader;
8+
mod state;
9+
mod writer;
10+
11+
pub fn pipe() -> (PipeWriter, PipeReader) {
12+
let shared_state = Arc::new(Mutex::new(State {
13+
reader_waker: None,
14+
writer_waker: None,
15+
data: None,
16+
done_reading: false,
17+
read: 0,
18+
done_cycle: true,
19+
closed: false,
20+
}));
21+
22+
let w = PipeWriter {
23+
state: Arc::clone(&shared_state),
24+
};
25+
26+
let r = PipeReader {
27+
state: Arc::clone(&shared_state),
28+
};
29+
30+
(w, r)
731
}

0 commit comments

Comments
 (0)