Skip to content

Commit feeb77e

Browse files
authored
Merge pull request #12 from routerify/optional_tokio
Make futures default and tokio optional
2 parents cd927c3 + 88da7b6 commit feeb77e

File tree

4 files changed

+73
-66
lines changed

4 files changed

+73
-66
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
run: cargo clippy --verbose
2121

2222
- name: Run tests
23-
run: cargo test --verbose
23+
run: cargo test --verbose --all-features

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
name = "async-pipe"
33
version = "0.1.3"
44
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"
5+
homepage = "https://github.com/routerify/async-pipe-rs"
6+
repository = "https://github.com/routerify/async-pipe-rs"
77
keywords = ["pipe", "future", "async", "reader", "writer"]
88
categories = ["asynchronous"]
99
authors = ["Rousan Ali <rousanali786@gmail.com>"]
@@ -12,7 +12,7 @@ license = "MIT"
1212
edition = "2018"
1313

1414
[features]
15-
default = ["tokio"]
15+
default = ["futures"]
1616

1717
[dependencies]
1818
tokio = { version = "1", features= [], optional = true }
@@ -24,3 +24,7 @@ tokio = { version = "1", features = ["full"] }
2424

2525
[package.metadata.docs.rs]
2626
features = ["futures", "tokio"]
27+
28+
[[example]]
29+
name = "tokio"
30+
required-features = ["tokio"]
File renamed without changes.

src/lib.rs

Lines changed: 65 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
//!
2525
//! # Featues
2626
//!
27-
//! * `tokio` (default) Implement `AsyncWrite` and `AsyncRead` from `tokio::io`.
28-
//! * `futures` Implement `AsyncWrite` and `AsyncRead` from `futures::io`
27+
//! * `futures` (default) Implement `AsyncWrite` and `AsyncRead` from `futures::io`
28+
//! * `tokio` Implement `AsyncWrite` and `AsyncRead` from `tokio::io`.
2929
3030
use state::State;
3131
use std::sync::{Arc, Mutex};
@@ -37,7 +37,7 @@ mod reader;
3737
mod state;
3838
mod writer;
3939

40-
/// 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).
40+
/// Creates a piped pair of an [`AsyncWrite`](https://docs.rs/tokio/1.9.0/tokio/io/trait.AsyncWrite.html) and an [`AsyncRead`](https://docs.rs/tokio/1.9.0/tokio/io/trait.AsyncRead.html).
4141
pub fn pipe() -> (PipeWriter, PipeReader) {
4242
let shared_state = Arc::new(Mutex::new(State {
4343
reader_waker: None,
@@ -59,64 +59,67 @@ pub fn pipe() -> (PipeWriter, PipeReader) {
5959

6060
#[cfg(test)]
6161
mod test {
62-
use super::*;
63-
use std::io;
64-
use tokio::io::{AsyncReadExt, AsyncWriteExt};
65-
66-
#[tokio::test]
67-
async fn read_write() {
68-
let (mut writer, mut reader) = pipe();
69-
let data = b"hello world";
70-
71-
let write_handle = tokio::spawn(async move {
72-
writer.write_all(data).await.unwrap();
73-
});
74-
75-
let mut read_buf = Vec::new();
76-
reader.read_to_end(&mut read_buf).await.unwrap();
77-
write_handle.await.unwrap();
78-
79-
assert_eq!(&read_buf, data);
80-
}
81-
82-
#[tokio::test]
83-
async fn eof_when_writer_is_shutdown() {
84-
let (mut writer, mut reader) = pipe();
85-
writer.shutdown().await.unwrap();
86-
let mut buf = [0u8; 8];
87-
let bytes_read = reader.read(&mut buf).await.unwrap();
88-
assert_eq!(bytes_read, 0);
89-
}
90-
91-
#[tokio::test]
92-
async fn broken_pipe_when_reader_is_dropped() {
93-
let (mut writer, reader) = pipe();
94-
drop(reader);
95-
let io_error = writer.write_all(&[0u8; 8]).await.unwrap_err();
96-
assert_eq!(io_error.kind(), io::ErrorKind::BrokenPipe);
97-
}
98-
99-
#[tokio::test]
100-
async fn eof_when_writer_is_dropped() {
101-
let (writer, mut reader) = pipe();
102-
drop(writer);
103-
let mut buf = [0u8; 8];
104-
let bytes_read = reader.read(&mut buf).await.unwrap();
105-
assert_eq!(bytes_read, 0);
106-
}
107-
108-
#[tokio::test]
109-
async fn drop_read_exact() {
110-
let (mut writer, mut reader) = pipe();
111-
const BUF_SIZE: usize = 8;
112-
113-
let write_handle = tokio::spawn(async move {
114-
writer.write_all(&[0u8; BUF_SIZE]).await.unwrap();
115-
});
116-
117-
let mut buf = [0u8; BUF_SIZE];
118-
reader.read_exact(&mut buf).await.unwrap();
119-
drop(reader);
120-
write_handle.await.unwrap();
62+
#[cfg(feature = "tokio")]
63+
mod test_tokio {
64+
use crate::*;
65+
use std::io;
66+
use tokio::io::{AsyncReadExt, AsyncWriteExt};
67+
68+
#[tokio::test]
69+
async fn read_write() {
70+
let (mut writer, mut reader) = pipe();
71+
let data = b"hello world";
72+
73+
let write_handle = tokio::spawn(async move {
74+
writer.write_all(data).await.unwrap();
75+
});
76+
77+
let mut read_buf = Vec::new();
78+
reader.read_to_end(&mut read_buf).await.unwrap();
79+
write_handle.await.unwrap();
80+
81+
assert_eq!(&read_buf, data);
82+
}
83+
84+
#[tokio::test]
85+
async fn eof_when_writer_is_shutdown() {
86+
let (mut writer, mut reader) = pipe();
87+
writer.shutdown().await.unwrap();
88+
let mut buf = [0u8; 8];
89+
let bytes_read = reader.read(&mut buf).await.unwrap();
90+
assert_eq!(bytes_read, 0);
91+
}
92+
93+
#[tokio::test]
94+
async fn broken_pipe_when_reader_is_dropped() {
95+
let (mut writer, reader) = pipe();
96+
drop(reader);
97+
let io_error = writer.write_all(&[0u8; 8]).await.unwrap_err();
98+
assert_eq!(io_error.kind(), io::ErrorKind::BrokenPipe);
99+
}
100+
101+
#[tokio::test]
102+
async fn eof_when_writer_is_dropped() {
103+
let (writer, mut reader) = pipe();
104+
drop(writer);
105+
let mut buf = [0u8; 8];
106+
let bytes_read = reader.read(&mut buf).await.unwrap();
107+
assert_eq!(bytes_read, 0);
108+
}
109+
110+
#[tokio::test]
111+
async fn drop_read_exact() {
112+
let (mut writer, mut reader) = pipe();
113+
const BUF_SIZE: usize = 8;
114+
115+
let write_handle = tokio::spawn(async move {
116+
writer.write_all(&[0u8; BUF_SIZE]).await.unwrap();
117+
});
118+
119+
let mut buf = [0u8; BUF_SIZE];
120+
reader.read_exact(&mut buf).await.unwrap();
121+
drop(reader);
122+
write_handle.await.unwrap();
123+
}
121124
}
122125
}

0 commit comments

Comments
 (0)