diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 96fb0ab..dae489d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,4 +20,4 @@ jobs: run: cargo clippy --verbose - name: Run tests - run: cargo test --verbose + run: cargo test --verbose --all-features diff --git a/Cargo.toml b/Cargo.toml index ff53aa6..e0202fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,8 +2,8 @@ name = "async-pipe" version = "0.1.3" description = "Creates an asynchronous piped reader and writer pair using tokio.rs" -homepage = "https://github.com/rousan/async-pipe-rs" -repository = "https://github.com/rousan/async-pipe-rs" +homepage = "https://github.com/routerify/async-pipe-rs" +repository = "https://github.com/routerify/async-pipe-rs" keywords = ["pipe", "future", "async", "reader", "writer"] categories = ["asynchronous"] authors = ["Rousan Ali "] @@ -12,7 +12,7 @@ license = "MIT" edition = "2018" [features] -default = ["tokio"] +default = ["futures"] [dependencies] tokio = { version = "1", features= [], optional = true } @@ -24,3 +24,7 @@ tokio = { version = "1", features = ["full"] } [package.metadata.docs.rs] features = ["futures", "tokio"] + +[[example]] +name = "tokio" +required-features = ["tokio"] diff --git a/examples/main.rs b/examples/tokio.rs similarity index 100% rename from examples/main.rs rename to examples/tokio.rs diff --git a/src/lib.rs b/src/lib.rs index 1eac958..2cb53c7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,8 +24,8 @@ //! //! # Featues //! -//! * `tokio` (default) Implement `AsyncWrite` and `AsyncRead` from `tokio::io`. -//! * `futures` Implement `AsyncWrite` and `AsyncRead` from `futures::io` +//! * `futures` (default) Implement `AsyncWrite` and `AsyncRead` from `futures::io` +//! * `tokio` Implement `AsyncWrite` and `AsyncRead` from `tokio::io`. use state::State; use std::sync::{Arc, Mutex}; @@ -37,7 +37,7 @@ mod reader; mod state; mod writer; -/// 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). +/// 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). pub fn pipe() -> (PipeWriter, PipeReader) { let shared_state = Arc::new(Mutex::new(State { reader_waker: None, @@ -59,64 +59,67 @@ pub fn pipe() -> (PipeWriter, PipeReader) { #[cfg(test)] mod test { - use super::*; - use std::io; - use tokio::io::{AsyncReadExt, AsyncWriteExt}; - - #[tokio::test] - async fn read_write() { - let (mut writer, mut reader) = pipe(); - let data = b"hello world"; - - let write_handle = tokio::spawn(async move { - writer.write_all(data).await.unwrap(); - }); - - let mut read_buf = Vec::new(); - reader.read_to_end(&mut read_buf).await.unwrap(); - write_handle.await.unwrap(); - - assert_eq!(&read_buf, data); - } - - #[tokio::test] - async fn eof_when_writer_is_shutdown() { - let (mut writer, mut reader) = pipe(); - writer.shutdown().await.unwrap(); - let mut buf = [0u8; 8]; - let bytes_read = reader.read(&mut buf).await.unwrap(); - assert_eq!(bytes_read, 0); - } - - #[tokio::test] - async fn broken_pipe_when_reader_is_dropped() { - let (mut writer, reader) = pipe(); - drop(reader); - let io_error = writer.write_all(&[0u8; 8]).await.unwrap_err(); - assert_eq!(io_error.kind(), io::ErrorKind::BrokenPipe); - } - - #[tokio::test] - async fn eof_when_writer_is_dropped() { - let (writer, mut reader) = pipe(); - drop(writer); - let mut buf = [0u8; 8]; - let bytes_read = reader.read(&mut buf).await.unwrap(); - assert_eq!(bytes_read, 0); - } - - #[tokio::test] - async fn drop_read_exact() { - let (mut writer, mut reader) = pipe(); - const BUF_SIZE: usize = 8; - - let write_handle = tokio::spawn(async move { - writer.write_all(&[0u8; BUF_SIZE]).await.unwrap(); - }); - - let mut buf = [0u8; BUF_SIZE]; - reader.read_exact(&mut buf).await.unwrap(); - drop(reader); - write_handle.await.unwrap(); + #[cfg(feature = "tokio")] + mod test_tokio { + use crate::*; + use std::io; + use tokio::io::{AsyncReadExt, AsyncWriteExt}; + + #[tokio::test] + async fn read_write() { + let (mut writer, mut reader) = pipe(); + let data = b"hello world"; + + let write_handle = tokio::spawn(async move { + writer.write_all(data).await.unwrap(); + }); + + let mut read_buf = Vec::new(); + reader.read_to_end(&mut read_buf).await.unwrap(); + write_handle.await.unwrap(); + + assert_eq!(&read_buf, data); + } + + #[tokio::test] + async fn eof_when_writer_is_shutdown() { + let (mut writer, mut reader) = pipe(); + writer.shutdown().await.unwrap(); + let mut buf = [0u8; 8]; + let bytes_read = reader.read(&mut buf).await.unwrap(); + assert_eq!(bytes_read, 0); + } + + #[tokio::test] + async fn broken_pipe_when_reader_is_dropped() { + let (mut writer, reader) = pipe(); + drop(reader); + let io_error = writer.write_all(&[0u8; 8]).await.unwrap_err(); + assert_eq!(io_error.kind(), io::ErrorKind::BrokenPipe); + } + + #[tokio::test] + async fn eof_when_writer_is_dropped() { + let (writer, mut reader) = pipe(); + drop(writer); + let mut buf = [0u8; 8]; + let bytes_read = reader.read(&mut buf).await.unwrap(); + assert_eq!(bytes_read, 0); + } + + #[tokio::test] + async fn drop_read_exact() { + let (mut writer, mut reader) = pipe(); + const BUF_SIZE: usize = 8; + + let write_handle = tokio::spawn(async move { + writer.write_all(&[0u8; BUF_SIZE]).await.unwrap(); + }); + + let mut buf = [0u8; BUF_SIZE]; + reader.read_exact(&mut buf).await.unwrap(); + drop(reader); + write_handle.await.unwrap(); + } } }